Difference between revisions of "Advanced-C++/C2/Inheritance/English"
Line 1: | Line 1: | ||
− | '''Title of script''': | + | '''Title of script''': Objects and Classes in C++ |
− | '''Author: '''Ashwini | + | '''Author: '''Ashwini Patil |
− | '''Keywords: | + | '''Keywords: Inheritance, Single level Inheritance, Multilevel Inheritance, Multiple Inheritance, Video tutorial.''' |
{| style="border-spacing:0;" | {| style="border-spacing:0;" | ||
− | + | | style="background-color:#ffffff;border-top:1pt solid #000000;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| <center>'''Visual Cue'''</center> | |
− | + | | style="background-color:#ffffff;border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| <center>'''Narration'''</center> | |
|- | |- | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 1 |
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Welcome to the spoken tutorial on Inheritance in C++. |
|- | |- | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 2 |
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| In this tutorial we will learn, |
− | + | Inheritance. | |
− | + | Types of inheritance. | |
− | We will do this with the help of | + | We will do this with the help of examples. |
|- | |- | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 3 |
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| To record this tutorial, I am using |
− | '''Ubuntu | + | '''Ubuntu OS '''version 11.10 |
− | '''g++''' | + | '''g++ compiler '''v. 4.6.1 |
|- | |- | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 4 |
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us start with an ontroduction to Inheritance |
− | + | When one object acquires the property of another it is called as inheritance. | |
− | It | + | It is the process of reusing the existing class without modifying them. |
− | + | |- | |
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| # Slide 5-\ | ||
− | |||
− | Types of | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Types of Inheritance |
− | + | Single level inheritance | |
− | + | Multiple level inheritance | |
− | + | Hierarchical Inheritance | |
− | + | Multilevel inheritance | |
+ | |||
+ | Hybrid Inheritance | ||
|- | |- | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 6 |
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| First let us know about the base class and the derived class. |
− | + | The base class has its own properties and functionality. | |
− | + | It is also called as parent class. | |
− | + | It has the common qualities that all the objects can inherit. | |
+ | |||
+ | The derived class is the child class. | ||
+ | |||
+ | Derived class inherits the properties and functionality of the base class. | ||
+ | |||
+ | Let us see what is single level inheritance. | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 7 |
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| |
− | |||
− | + | In single level inheritance only one base class and one derived class is needed. | |
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0. | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 8 |
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| | ||
+ | |||
+ | |||
− | |||
− | |||
− | + | In multiple inheritance derived class inherits from more than one base class. | |
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0. | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 9 |
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| In Hierarchical Inheritance multiple derived classes inherits from one base | ||
+ | |||
+ | |- | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 10 | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | In Multilevel inheritance the subclass acts as the base class for other classes. | ||
+ | |||
+ | |- | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 11 | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| In hybrid inheritance more than one form of inheritance is combined. | ||
+ | |||
+ | |- | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Open '''exam_inherit.cpp''' | ||
+ | |||
+ | |||
+ | Point the cursor | ||
+ | |||
+ | '''exam_inherit.cpp''' | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Now let us see an example on single level inheritance. | ||
+ | |||
+ | I have already typed the code on the editor. | ||
+ | |||
+ | I will explain it. | ||
+ | |||
+ | Note that I have saved the file with the name '''exam_inherit.cpp''' | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |- | ||
+ | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| | ||
+ | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| The example involves to display the name, roll no and marks of the student. | ||
+ | |||
+ | |- | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''Highlight''' | ||
'''<nowiki>#include <iostream></nowiki>''' | '''<nowiki>#include <iostream></nowiki>''' | ||
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is our header file as iostream |
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | '''using namespace | + | '''using std namespace''' |
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we are using the '''std namespace''' |
|- | |- | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | '''class | + | '''class''' '''student''' |
− | + | ||
+ | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we have class '''student.''' | ||
|- | |- | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | '''int | + | '''int width;''' |
− | | style="background-color: | + | |
+ | '''int height;''' | ||
+ | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Then we have integer variable '''roll_no''' and '''character array''' as '''name.''' | ||
+ | |||
+ | As private members of '''class student'''. | ||
|- | |- | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
+ | |||
+ | '''public:''' | ||
+ | |||
+ | '''void input(); ''' | ||
+ | |||
+ | '''void display(); ''' | ||
+ | |||
+ | '''};''' | ||
+ | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Function '''input '''and function''' display '''are public functions of class student. | ||
+ | |||
− | |||
− | |||
− | |||
|- | |- | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
+ | |||
+ | '''void student :: input()''' | ||
+ | |||
+ | '''{''' | ||
+ | |||
+ | '''<nowiki>cout << “Enter Roll no.”;</nowiki>''' | ||
+ | |||
+ | '''cin>>roll_no; ''' | ||
+ | |||
+ | '''<nowiki>cout<<"Enter Name:"; </nowiki>''' | ||
+ | |||
+ | '''cin>>name; ''' | ||
+ | |||
+ | '''}''' | ||
+ | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we are using the '''input''' function to accept the roll no. and name of the student. | ||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | ''' | + | '''void student::display()''' |
− | | style="background-color: | + | |
+ | |||
+ | |||
+ | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Then we have the '''display''' function to display the roll_no and name. | ||
Line 142: | Line 218: | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | ''' | + | '''class exam:public student ''' |
− | | style="background-color: | + | |
+ | '''{ ''' | ||
+ | |||
+ | |||
+ | |||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we have another class as exam_inherit. | ||
+ | |||
+ | This is the derived class. | ||
+ | |||
+ | It inherits the the data and function of class student. | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
+ | |||
+ | '''private: ''' | ||
+ | |||
+ | '''int msub1; ''' | ||
− | ''' | + | '''int msub2; ''' |
− | + | ||
− | + | '''int msub3;''' | |
− | + | '''int total;''' | |
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Then we have declared four variables as''' sub1, sub2, sub3 and total.''' | ||
− | ''' | + | As private variables of '''class exam_inherit.''' |
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | ''' | + | '''public: ''' |
− | ''' | + | '''void input_exam(); ''' |
− | + | ||
− | + | '''void display_exam(); ''' | |
+ | |||
+ | '''};''' | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we have two functions '''input_exam''' and '''display_exam as public functions.''' | ||
+ | |||
+ | Here we close the class. | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
+ | |||
+ | '''void exam::input_exam() ''' | ||
+ | |||
+ | '''{ ''' | ||
+ | |||
+ | '''<nowiki>cout<<"Enter marks of subject1:"; </nowiki>''' | ||
+ | |||
+ | '''cin>>sub1; ''' | ||
+ | |||
+ | '''<nowiki>cout<<"Enter marks of subject2:"; </nowiki>''' | ||
+ | |||
+ | '''cin>>sub2; ''' | ||
+ | |||
+ | '''<nowiki>cout<<"Enter marks of subject3:"; </nowiki>''' | ||
+ | |||
+ | '''cin>>sub3; ''' | ||
+ | |||
+ | '''}''' | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Now we are using function '''input_exam '''function to accept the marks of three''' '''subjects. | ||
+ | |||
+ | |||
− | |||
− | |||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | ''' | + | '''void exam::display_exam() ''' |
− | + | ||
− | + | '''{''' | |
− | ''' | + | '''total=sub1+sub2+sub3; ''' |
− | + | '''<nowiki>cout<<"Total is :" <<total<<"\n"; </nowiki>''' | |
+ | |||
+ | '''}''' | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here, we are using display_exam function to calculate the total of three subjects. | ||
+ | |||
+ | Then we print the total. | ||
|- | |- | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | ''' | + | '''int main()''' |
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is our '''main function.''' |
+ | |||
+ | |- | ||
+ | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight | ||
− | + | '''exam ex;''' | |
+ | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we create an object of class exam_inherit as '''ex''' | ||
|- | |- | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | '''return 0;''' | + | '''ex.input();''' |
− | | style="background-color: | + | |
+ | '''ex.input_exam(); ''' | ||
+ | |||
+ | '''ex.display(); ''' | ||
+ | |||
+ | '''ex.display_exam(); ''' | ||
+ | |||
+ | '''return 0; ''' | ||
+ | |||
+ | '''}''' | ||
+ | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Then we call all the functions using the object '''ex''' | ||
|- | |- | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | | style="background-color: | + | |
+ | '''return 0;''' | ||
+ | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| And this is the return statement. | ||
|- | |- | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| |
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us execute the program |
|- | |- | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Open the terminal |
− | '''Ctrl, Alt and T''' keys simultaneously | + | '''Ctrl, Alt and T '''keys simultaneously |
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Please open the terminal by pressing '''Ctrl, Alt and T '''keys simultaneously |
|- | |- | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Type |
− | '''g++ | + | '''g++ exam_inherit.cpp -o exam''' |
To execute | To execute | ||
Line 224: | Line 356: | ||
Type | Type | ||
− | '''./ | + | '''./exam''' |
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| To compile the program type |
− | '''g++ | + | '''g++ exam_inherit.cpp (space)-o (space) exam''' |
To execute | To execute | ||
Line 233: | Line 365: | ||
Type | Type | ||
− | '''./ | + | '''./exam''' |
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
'''Output''' | '''Output''' | ||
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we see, |
− | ''' | + | '''Enter Roll no.:''' |
− | ''' | + | '''I will enter as 1''' |
− | ''' | + | '''Enter Name:''' |
+ | |||
+ | '''I will enter as Arya''' | ||
+ | |||
+ | '''Enter marks of subject1''' | ||
+ | |||
+ | '''I will enter as 60''' | ||
+ | |||
+ | '''Enter marks of subject1''' | ||
+ | |||
+ | '''I will enter as 70''' | ||
+ | |||
+ | '''Enter marks of subject1''' | ||
+ | |||
+ | '''I will enter as 80''' | ||
|- | |- | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | + | ||
− | + | '''Output''' | |
+ | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here the output is displayed as | ||
− | + | '''Roll no is: 1''' | |
− | + | '''Name is: Arya''' | |
+ | |||
+ | '''Total is: 210''' | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0. | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Multilevel Inheritance |
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Now we will see multilevel inheritance in the same example. | ||
− | + | |- | |
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Point the cursor to |
+ | |||
+ | '''multilevel.cpp''' | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| I have already typed the code. | ||
+ | |||
+ | Let us switch back to our ediotr. | ||
+ | |||
+ | Note that our filename is '''multilevel.cpp''' | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| '''public:''' |
− | | style="background-color: | + | |
+ | '''int total;''' | ||
+ | |||
+ | |||
+ | |||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we have declared the variable total. | ||
+ | |||
+ | As public variable of class exam_inheri. | ||
+ | |||
+ | This is because the private members are not accessed by the derived class. | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight: |
− | | style="background-color: | + | |
+ | '''class grade:public exam_inherit''' | ||
+ | |||
+ | '''{''' | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we have another class as '''grade.''' | ||
+ | |||
+ | This inherits the class '''exam_inherit.''' | ||
+ | |||
+ | Class '''grade''' is the derived class | ||
+ | |||
+ | And class '''exam_inherit''' is the base class for class '''grade'''. | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| |
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| All the functions and data of class '''exam_inherit''' will be inherited to class '''grade'''. | ||
− | + | |- | |
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | + | '''private: ''' | |
− | + | '''int avg; ''' | |
− | + | '''public: ''' | |
+ | |||
+ | '''void average(); ''' | ||
+ | |||
+ | '''};''' | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we have declared '''avg''' as private member of class '''grade.''' | ||
+ | |||
+ | And function '''average''' as public function. | ||
+ | |||
+ | Then we close the class. | ||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | | style="background-color: | + | |
+ | |||
+ | '''void grade::average() ''' | ||
+ | |||
+ | '''{ ''' | ||
+ | |||
+ | '''avg=total/3; ''' | ||
+ | |||
+ | '''<nowiki>cout<<"Average is :"<<avg<<"\n"; </nowiki>''' | ||
+ | |||
+ | '''}''' | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we use the average function to calculate the average. | ||
+ | |||
+ | Then we print the average. | ||
+ | |||
+ | |||
− | |||
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | '''g++ | + | '''int main() ''' |
+ | |||
+ | '''{ ''' | ||
+ | |||
+ | '''grade gd; ''' | ||
+ | |||
+ | '''gd.input(); ''' | ||
+ | |||
+ | '''gd.input_exam(); ''' | ||
+ | |||
+ | '''gd.display(); ''' | ||
+ | |||
+ | '''gd.display_exam(); ''' | ||
+ | |||
+ | '''gd.average();''' | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Inside the main function we create an object of class grade as '''gd.''' | ||
+ | |||
+ | Then we call all the functions usign '''gd'''. | ||
+ | |||
+ | gd.input() | ||
+ | |||
+ | input_exam | ||
+ | |||
+ | display | ||
+ | |||
+ | display_exam | ||
+ | |||
+ | and the average function. | ||
+ | |||
+ | |- | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight | ||
+ | |||
+ | '''return 0;''' | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| And this is our return statement. | ||
+ | |||
+ | |- | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Show in the program | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Now you can see that class grade is the derived class. | ||
+ | |||
+ | And exam_inherit is the base class for grade. | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |- | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Show in the program | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| And here class exam_inherit is the derived class. | ||
+ | |||
+ | And class student is the base class for class exam_inherit. | ||
+ | |||
+ | |- | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| On the terminal | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Now let us execute the program. | ||
+ | |||
+ | Come back to our terminal. | ||
+ | |||
+ | |- | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Type | ||
+ | |||
+ | '''g++ multilevel.cpp -o multilevel''' | ||
Type | Type | ||
− | '''./ | + | '''./multilevel''' |
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| To compile type |
− | '''g++ | + | '''g++ multilevel.cpp -o mul''' |
− | + | '''Press Enter''' | |
− | + | Type | |
− | + | '''./multilevel''' | |
− | ''' | + | '''Press Enter''' |
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
+ | |||
+ | '''Type:''' | ||
+ | |||
+ | '''2''' | ||
+ | |||
+ | '''Pratham''' | ||
+ | |||
+ | '''65''' | ||
+ | |||
+ | '''67''' | ||
+ | |||
+ | '''82''' | ||
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here the output is displayed: | ||
+ | |||
+ | '''Enter Roll no.:''' | ||
+ | |||
+ | '''I will enter as 2''' | ||
+ | |||
+ | '''Enter Name:''' | ||
+ | |||
+ | '''I will enter as Pratham''' | ||
+ | |||
+ | '''Enter marks of subject1''' | ||
+ | |||
+ | '''I will enter as 65''' | ||
− | ''' | + | '''Enter marks of subject1''' |
− | + | ||
− | ''' | + | '''I will enter as 67''' |
− | ''' | + | '''Enter marks of subject1''' |
− | + | '''I will enter as 82''' | |
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Highlight |
− | + | ||
− | + | Output | |
+ | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| The output is displayed as: | ||
− | ''' | + | '''Roll no is: 2 ''' |
− | + | '''Name is: Pratham ''' | |
− | ''' | + | '''Total is: 214 ''' |
− | + | '''Average is: 71''' | |
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Switch back to the slides |
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This brings us to the end of this tutorial. |
− | + | Let us come back to the slides | |
|- | |- | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 7 |
− | + | Summary | |
− | | style="background-color: | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us summarize: |
− | + | In this tutorial, we have seen, | |
− | + | Inheritance. | |
+ | |||
+ | Types of inheritance. | ||
|- | |- | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 6 |
− | + | Assignment | |
+ | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| As an assignment | ||
+ | Create a class Shape. | ||
+ | Create two functions of the class as Area and Perimeter. | ||
− | | style="background-color: | + | Find the area and perimeter of various shapes like square, rectangle and circle. |
+ | |||
+ | |- | ||
+ | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 8 | ||
+ | |||
+ | About the Spoken Tutorial Project | ||
+ | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Watch the video available at the link shown | ||
+ | |||
+ | It summarizes the Spoken Tutorial project | ||
+ | |||
+ | If you do not have good bandwidth, you can download and watch it | ||
+ | |||
+ | |- | ||
+ | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 9 | ||
+ | |||
+ | Spoken Tutorial Workshops | ||
+ | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| The Spoken Tutorial Project Team | ||
− | Conducts workshops using spoken tutorials | + | Conducts workshops using spoken tutorials |
− | Gives certificates to those who pass an online test | + | Gives certificates to those who pass an online test |
For more details, please write to, | For more details, please write to, | ||
Line 369: | Line 672: | ||
|- | |- | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide Number 10 |
Acknowledgement | Acknowledgement | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Spoken Tutorial Project is a part of the Talk to a Teacher project |
− | It is supported by the National Mission on Education through ICT, MHRD, Government of | + | It is supported by the National Mission on Education through ICT, MHRD, Government of Indiaabout:startpage |
− | More information on this Mission is available at: http://spoken-tutorial.org | + | More information on this Mission is available at: [http://spoken-tutorial.org/NMEICT-Intro http://spoken-tutorial.org\NMEICT-Intro] |
|- | |- | ||
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:0.25pt solid #c0c0c0;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| |
− | | style="border-top: | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:0.25pt solid #c0c0c0;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is Ashwini Patil from IIT Bombay signing off |
Thank You for joining. | Thank You for joining. | ||
|} | |} |
Latest revision as of 22:37, 9 May 2013
Title of script: Objects and Classes in C++
Author: Ashwini Patil
Keywords: Inheritance, Single level Inheritance, Multilevel Inheritance, Multiple Inheritance, Video tutorial.
|
|
Slide 1 | Welcome to the spoken tutorial on Inheritance in C++. |
Slide 2
|
In this tutorial we will learn,
Inheritance. Types of inheritance. We will do this with the help of examples. |
Slide 3
|
To record this tutorial, I am using
Ubuntu OS version 11.10 g++ compiler v. 4.6.1 |
Slide 4 | Let us start with an ontroduction to Inheritance
When one object acquires the property of another it is called as inheritance. It is the process of reusing the existing class without modifying them. |
# Slide 5-\
|
Types of Inheritance
Single level inheritance Multiple level inheritance Hierarchical Inheritance Multilevel inheritance Hybrid Inheritance |
Slide 6 | First let us know about the base class and the derived class.
The base class has its own properties and functionality. It is also called as parent class. It has the common qualities that all the objects can inherit. The derived class is the child class. Derived class inherits the properties and functionality of the base class. Let us see what is single level inheritance. |
Slide 7 |
|
Slide 8 |
|
Slide 9 | In Hierarchical Inheritance multiple derived classes inherits from one base |
Slide 10 |
|
Slide 11 | In hybrid inheritance more than one form of inheritance is combined. |
Open exam_inherit.cpp
exam_inherit.cpp |
Now let us see an example on single level inheritance.
I have already typed the code on the editor. I will explain it. Note that I have saved the file with the name exam_inherit.cpp
|
The example involves to display the name, roll no and marks of the student. | |
Highlight
#include <iostream> |
This is our header file as iostream |
Highlight
using std namespace |
Here we are using the std namespace |
Highlight
class student
|
Here we have class student. |
Highlight
int width; int height; |
Then we have integer variable roll_no and character array as name.
As private members of class student. |
Highlight
public: void input(); void display(); }; |
Function input and function display are public functions of class student.
|
Highlight
void student :: input() { cout << “Enter Roll no.”; cin>>roll_no; cout<<"Enter Name:"; cin>>name; } |
Here we are using the input function to accept the roll no. and name of the student.
|
Highlight
void student::display()
|
Then we have the display function to display the roll_no and name.
|
Highlight
class exam:public student {
|
Here we have another class as exam_inherit.
This is the derived class. It inherits the the data and function of class student. |
Highlight
private: int msub1; int msub2; int msub3; int total; |
Then we have declared four variables as sub1, sub2, sub3 and total.
As private variables of class exam_inherit. |
Highlight
public: void input_exam(); void display_exam(); }; |
Here we have two functions input_exam and display_exam as public functions.
Here we close the class. |
Highlight
void exam::input_exam() { cout<<"Enter marks of subject1:"; cin>>sub1; cout<<"Enter marks of subject2:"; cin>>sub2; cout<<"Enter marks of subject3:"; cin>>sub3; } |
Now we are using function input_exam function to accept the marks of three subjects.
|
Highlight
void exam::display_exam() { total=sub1+sub2+sub3; cout<<"Total is :" <<total<<"\n"; } |
Here, we are using display_exam function to calculate the total of three subjects.
Then we print the total. |
Highlight
int main() |
This is our main function. |
Highlight
exam ex; |
Here we create an object of class exam_inherit as ex |
Highlight
ex.input(); ex.input_exam(); ex.display(); ex.display_exam(); return 0; } |
Then we call all the functions using the object ex |
Highlight
return 0; |
And this is the return statement. |
Let us execute the program | |
Open the terminal
Ctrl, Alt and T keys simultaneously |
Please open the terminal by pressing Ctrl, Alt and T keys simultaneously |
Type
g++ exam_inherit.cpp -o exam To execute Type ./exam |
To compile the program type
g++ exam_inherit.cpp (space)-o (space) exam To execute Type ./exam |
Highlight
Output |
Here we see,
Enter Roll no.: I will enter as 1 Enter Name: I will enter as Arya Enter marks of subject1 I will enter as 60 Enter marks of subject1 I will enter as 70 Enter marks of subject1 I will enter as 80 |
Highlight
Output |
Here the output is displayed as
Roll no is: 1 Name is: Arya Total is: 210 |
Multilevel Inheritance | Now we will see multilevel inheritance in the same example. |
Point the cursor to
multilevel.cpp |
I have already typed the code.
Let us switch back to our ediotr. Note that our filename is multilevel.cpp |
public:
int total;
|
Here we have declared the variable total.
As public variable of class exam_inheri. This is because the private members are not accessed by the derived class. |
Highlight:
class grade:public exam_inherit { |
Here we have another class as grade.
This inherits the class exam_inherit. Class grade is the derived class And class exam_inherit is the base class for class grade. |
All the functions and data of class exam_inherit will be inherited to class grade. | |
Highlight
private: int avg; public: void average(); }; |
Here we have declared avg as private member of class grade.
And function average as public function. Then we close the class. |
Highlight
{ avg=total/3; cout<<"Average is :"<<avg<<"\n"; } |
Here we use the average function to calculate the average.
Then we print the average.
|
Highlight
int main() { grade gd; gd.input(); gd.input_exam(); gd.display(); gd.display_exam(); gd.average(); |
Inside the main function we create an object of class grade as gd.
Then we call all the functions usign gd. gd.input() input_exam display display_exam and the average function. |
Highlight
return 0; |
And this is our return statement. |
Show in the program | Now you can see that class grade is the derived class.
And exam_inherit is the base class for grade.
|
Show in the program | And here class exam_inherit is the derived class.
And class student is the base class for class exam_inherit. |
On the terminal | Now let us execute the program.
Come back to our terminal. |
Type
g++ multilevel.cpp -o multilevel Type ./multilevel |
To compile type
g++ multilevel.cpp -o mul Press Enter Type ./multilevel Press Enter |
Highlight
Type: 2 Pratham 65 67 82 |
Here the output is displayed:
Enter Roll no.: I will enter as 2 Enter Name: I will enter as Pratham Enter marks of subject1 I will enter as 65 Enter marks of subject1 I will enter as 67 Enter marks of subject1 I will enter as 82 |
Highlight
Output |
The output is displayed as:
Roll no is: 2 Name is: Pratham Total is: 214 Average is: 71 |
Switch back to the slides | This brings us to the end of this tutorial.
Let us come back to the slides |
Slide 7
Summary |
Let us summarize:
In this tutorial, we have seen, Inheritance. Types of inheritance. |
Slide 6
Assignment |
As an assignment
Create a class Shape. Create two functions of the class as Area and Perimeter. Find the area and perimeter of various shapes like square, rectangle and circle. |
Slide 8
About the Spoken Tutorial Project |
Watch the video available at the link shown
It summarizes the Spoken Tutorial project If you do not have good bandwidth, you can download and watch it |
Slide 9
Spoken Tutorial Workshops |
The Spoken Tutorial Project Team
Conducts workshops using spoken tutorials Gives certificates to those who pass an online test For more details, please write to, contact@spoken-tutorial.org |
Slide Number 10
|
Spoken Tutorial Project is a part of the Talk to a Teacher project
It is supported by the National Mission on Education through ICT, MHRD, Government of Indiaabout:startpage More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro |
This is Ashwini Patil from IIT Bombay signing off
Thank You for joining. |