Difference between revisions of "Advanced-C++/C2/More-On-Inheritance/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
Line 13: Line 13:
 
|-
 
|-
 
| 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="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="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 Multiple and Hierarchical Inheritance in C++.
+
| 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 '''Multiple''' and '''Hierarchical Inheritance''' in '''C++'''.
  
 
|-
 
|-
 
| 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="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="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,
 
| 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,
  
Multiple Inheritance.
+
*'''Multiple Inheritance'''.
 
+
*'''Hierarchical Inheritance'''.
Hierarchical Inheritnace.
+
*We will do this with the help of examples.
 
+
We will do this with the help of examples.
+
  
 
|-
 
|-
 
| 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:#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:#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,
 
| 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 OS '''version 11.10 and
+
*'''Ubuntu OS '''version 11.10 and
 
+
*'''g++ compiler '''v. 4.6.1
'''g++ compiler '''v. 4.6.1
+
  
 
|-
 
|-
Line 46: Line 37:
  
 
'''multiple.cpp'''
 
'''multiple.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;"| In multiple inheritance derived class inherits from more than one base class.
+
| 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'''.
  
Now, we will see an example on multiple inheritance.
+
Now, we will see an example on '''multiple inheritance'''.
  
 
I have already typed the code on the editor.
 
I have already typed the code on the editor.
Line 54: Line 45:
 
I will open it.
 
I will open it.
  
Note that I have saved the file with the name '''multiple.cpp'''
+
Note that our filename is '''multiple.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: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;"| '''In this program we will display the name, roll no, marks and average of the 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;"| In this program we will display the '''name, roll no, marks''' and '''average''' of the student.
  
 
Let me explain the code.
 
Let me explain the code.
Line 85: Line 73:
  
  
| 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 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;"| Then we have '''class "student".'''
  
 
This is the '''base class.'''
 
This is the '''base class.'''
Line 95: Line 83:
  
 
'''<nowiki>char name[20];</nowiki>'''
 
'''<nowiki>char name[20];</nowiki>'''
| 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 we have '''roll_no''' as integer variable and '''name '''as''' '''character variable.
+
| 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 we have '''roll_no''' as '''integer variable''' and '''name '''as''' character variable'''.
  
These are declared as protected.
+
These are declared as '''protected'''.
  
 
|-
 
|-
Line 103: Line 91:
  
 
'''class exam_inherit'''
 
'''class exam_inherit'''
| 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 another''' exam_inherit'''
+
| 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 another '''class "exam_inherit"'''
  
'''This is also a base''' '''class.'''
+
This is also a '''base class.'''
  
Hence we have two base class here''' student and exam_inherit.'''
+
Hence we have two '''base class''' here- ''' student''' and '''exam_inherit.'''
  
In this we have declared 3 private variables '''sub1, sub2, sub3''' as protected.
+
In this we have 3 '''variables'''-  '''sub1, sub2, sub3''' as '''protected'''.
  
This is because protected variables can be accessed by derived class.
+
This is because '''protected variables''' can be accessed by '''derived clas'''s.
  
 
|-
 
|-
Line 119: Line 107:
  
 
'''{'''
 
'''{'''
| 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;"| Now here we have class '''grade''' which is the derived class.
+
| 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;"| Now here we have '''class "grade"''' which is the '''derived cla'''ss.
 
+
This inherits the base classes.
+
 
+
Class '''student''' and class '''exam_inherit.'''
+
 
+
 
+
  
 +
This inherits the '''base classes''' - '''class "student"''' and '''class "exam_inherit"'''.
  
 
|-
 
|-
Line 134: Line 117:
  
 
'''int avg;'''
 
'''int avg;'''
| 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 we have '''avg''' as integer variable declared as private.
+
| 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 we have '''avg''' as '''integer variable''' declared as '''private'''.
  
 
|-
 
|-
Line 155: Line 138:
  
  
| 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 function
+
| 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 '''function'''
  
 
'''input()'''
 
'''input()'''
Line 167: Line 150:
 
'''and display_exam()'''
 
'''and display_exam()'''
  
as public functions.
+
as '''public functions'''.
  
In this we have total as integer variable declared as public.
+
In this we have '''"total"''' as '''integer variable''' declared as '''public'''.
  
 
|-
 
|-
Line 187: Line 170:
  
 
'''} '''
 
'''} '''
| 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 use '''input''' function to accept the roll_no and name of the student.
+
| 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 use '''input function''' to accept the '''roll_no''' and '''name''' of the student.
 
+
 
+
 
+
  
 
|-
 
|-
Line 204: Line 184:
  
 
'''}'''
 
'''}'''
| 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''' display '''function we display the '''roll_no''' and '''name '''of the student'''.'''
+
| 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''' display function''', we display the '''roll_no''' and '''name '''of the student.
  
 
|-
 
|-
Line 226: Line 206:
  
 
'''}'''
 
'''}'''
| 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 function '''input_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''' function input_exam.'''
  
 
In this we accept the marks of three subjects as '''sub1, sub2 and sub3.'''
 
In this we accept the marks of three subjects as '''sub1, sub2 and sub3.'''
 
 
 
  
 
|-
 
|-
Line 245: Line 222:
  
 
'''}'''
 
'''}'''
| 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 in '''display_exam''' function we calculate the total of three subjects.
+
| 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 in '''display_exam function''', we calculate the total of three subjects.
  
 
And print the total.
 
And print the total.
Line 261: Line 238:
  
 
'''}'''
 
'''}'''
| 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 in function average we calculate the 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;"| And in '''function average''' we calculate the average.
 
+
 
+
 
+
  
 
|-
 
|-
Line 298: Line 272:
  
 
'''return 0;'''
 
'''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;"| This is the '''return''' statement.
+
| 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 the '''return statement'''.
  
 
|-
 
|-
 
| 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: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;"|Now Let us execute the program
+
| 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;"|Now let us execute the program
  
 
|-
 
|-
| 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  
+
| 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''' by pressing '''Ctrl, Alt and T '''keys simultaneously  
 
+
| 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;"| Open the terminal window by pressing '''Ctrl, Alt and T '''keys simultaneously, on your keyboard.
'''Ctrl, Alt and T '''keys simultaneously
+
| 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 on your keyboard
+
  
 
|-
 
|-
Line 320: Line 292:
  
 
'''./mult'''
 
'''./mult'''
| 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  
+
| 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, type  
  
 
'''g++ space multiple.cpp space -o space mult'''
 
'''g++ space multiple.cpp space -o space mult'''
Line 343: Line 315:
  
 
'''84'''
 
'''84'''
| 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,
+
| 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.:'''
 
+
'''Enter Roll no.:'''
+
  
'''I will enter as 3'''
+
I will enter as '''3'''
  
 
'''Enter Name:'''
 
'''Enter Name:'''
  
'''I will enter as Pratham'''
+
I will enter as '''Pratham'''
  
 
'''Enter marks of subject1'''
 
'''Enter marks of subject1'''
  
'''I will enter as 67'''
+
I will enter as '''67'''
  
'''Enter marks of subject2'''
+
'''subject2''' as '''78'''
  
'''I will enter as 78'''
+
'''subject3''' as '''84'''
 
+
'''Enter marks of subject3'''
+
 
+
'''I will enter as 84'''
+
  
 
|-
 
|-
Line 369: Line 335:
  
 
'''Output'''
 
'''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  
+
| 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 output is displayed as  
  
 
'''Roll no is: 3'''
 
'''Roll no is: 3'''
Line 379: Line 345:
 
'''Average is: 76'''
 
'''Average is: 76'''
  
This was multiple inheritance.
+
This was '''multiple inheritance'''.
  
Now we will see hierarchical inheritance.
+
Now we will see '''hierarchical inheritance'''.
  
 
|-
 
|-
Line 387: Line 353:
 
| 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;"| Come back to our 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;"| Come back to our program.
  
In hierarchical inheritance multiple derived classes inherits from one base class.
+
In '''hierarchical inheritance multiple derived classes''' inherits from one '''base class'''.
  
 
|-
 
|-
Line 402: Line 368:
 
| 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: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;"| Here we have used the '''std namespace'''
 
| 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 used the '''std namespace'''
 
|-
 
| 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;"|
 
  
 
|-
 
|-
Line 425: Line 387:
 
| 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 '''class 'student'''' which is the '''base class.'''
 
| 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 '''class 'student'''' which is the '''base class.'''
  
'''In this we''' have '''roll_no''' as integer variable.
+
In this, we have '''roll_no''' as '''integer variable'''.
  
Sub1, sub2, sub3 and total as integer variables.
+
'''Sub1, sub2, sub3''' and '''total''' as '''integer variables'''.
  
Then name as character variable.
+
Then '''name''' as '''character varia'''ble.
  
These are declared protected.
+
These are declared '''protected'''.
  
 
|-
 
|-
Line 439: Line 401:
  
 
'''{ '''
 
'''{ '''
| 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 ''''show'.'''
+
| 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 'show'.'''
  
This is the derived class.
+
This is the '''derived class'''.
  
 
It inherits the properties of''' class student.'''
 
It inherits the properties of''' class student.'''
 
 
  
  
Line 458: Line 418:
  
 
'''};'''
 
'''};'''
| 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 this we have two function as input and display.
+
| 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 this we have two '''functions: "input"''' and '''"display".'''
  
These are declared as public functions.
+
These are declared as '''public functions'''.
  
 
|-
 
|-
Line 478: Line 438:
  
 
'''}'''  
 
'''}'''  
| 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 function '''input''' we accept the '''roll_no''' and '''name''' of the student.
+
| 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 '''function input''' we accept the '''roll_no''' and '''name''' of the student.
  
 
|-
 
|-
Line 492: Line 452:
  
 
'''}'''
 
'''}'''
| 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 function '''display''' we display '''roll_no''' and '''name''' of the student.
+
| 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 '''function display''' we display the '''roll_no''' and '''name''' of the student.
  
 
|-
 
|-
Line 500: Line 460:
  
 
'''{ '''
 
'''{ '''
| 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 another derived class as class '''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;"| Then we have another '''derived class''' as class '''exam.'''
  
 
This also inherits '''class student.'''
 
This also inherits '''class student.'''
  
'''We can see that there are two derived class'''
+
We can see that there are two '''derived class'''- '''class exam''' and '''class show.'''
 
+
'''Class exam and class show.'''
+
 
+
'''Both the classes inherits the class student.'''
+
 
+
 
+
  
 +
Both the '''classes''' inherits the '''class "student".'''
  
 
|-
 
|-
Line 523: Line 478:
  
 
'''};'''
 
'''};'''
| 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 class exam we have two functions as input_exam and total marks declared as public.
+
| 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 '''class exam''' we have two '''functions''' as '''"input_exam"''' and '''"total marks"''' declared as '''public'''.
 
+
 
+
 
+
  
 
|-
 
|-
Line 548: Line 500:
  
 
'''}'''  
 
'''}'''  
| 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 access the function '''input_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 access the '''function "input_exam"'''  
 
+
It accepts the marks of three subjects.
+
 
+
'''sub1, sub2 and sub3'''
+
 
+
 
+
  
 +
It accepts the marks of three subjects- '''sub1, sub2 and sub3'''
  
 
|-
 
|-
Line 569: Line 516:
  
 
'''}'''  
 
'''}'''  
| 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 function '''total_marks.'''
+
| 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 ''' "total_marks" function.'''
  
It calculate the total of three subjects.
+
It calculates the total of three subjects and print the total.
 
+
And print the total.
+
  
 
|-
 
|-
Line 587: Line 532:
  
 
'''exam em; '''
 
'''exam em; '''
| 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 '''main''' function.
+
| 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 '''main function'''.
  
In this we create objects of three '''classes'''
+
In this we create '''objects''' of three '''classes'''
  
 
as''' st, sw '''and''' ex.'''
 
as''' st, sw '''and''' ex.'''
Line 619: Line 564:
  
 
'''}'''
 
'''}'''
| 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:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| And this is our '''return statement'''.
  
 
|-
 
|-
Line 625: Line 570:
 
| 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.
 
| 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 the terminal.
+
Come back to the '''terminal'''.
Let me clear the prompt.
+
Let me clear the '''prompt'''.
  
 
|-
 
|-
Line 664: Line 609:
  
 
'''97'''
 
'''97'''
| 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 What?it is displayed as
+
| 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;"|  
  
 
'''Enter Roll no.:'''
 
'''Enter Roll no.:'''
  
'''I will enter as 4'''
+
I will enter as '''4'''
  
 
'''Enter Name:'''
 
'''Enter Name:'''
  
'''I will enter as Ashwini'''
+
I will enter as '''Ashwini'''
  
 
'''Enter marks of subject1'''
 
'''Enter marks of subject1'''
  
'''I will enter as 87'''
+
I will enter as '''87'''
 
+
'''Enter marks of subject2'''
+
 
+
'''I will enter as 67'''
+
 
+
'''Enter marks of subject3'''
+
  
'''I will enter as 97'''
+
'''subject2''' as '''67''' and '''subject3''' as '''97'''
  
 
|-
 
|-
Line 708: Line 647:
 
| 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
 
| 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,
+
In this tutorial, we learnt,
  
Multiple Inheritance.
+
*'''Multiple Inheritance'''.
  
Hierarchical Inheritance.
+
*'''Hierarchical Inheritance'''.
  
 
|-
 
|-
Line 720: Line 659:
 
| 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  
 
| 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 area and perimeter.
+
Create a '''class area''' and '''perimeter'''.
  
Find the area and perimeter of a rectangle.
+
Find the '''area''' and '''perimeter''' of a '''rectangle'''.
  
 
|-
 
|-

Latest revision as of 23:16, 12 December 2013

Title of script: Multiple and Hierarchical Inheritance in C++

Author: Ashwini Patil

Keywords: Multiple Inheritance, Hierarchical Inheritance, Video tutorial.


Visual Cue
Narration
Slide 1 Welcome to the spoken tutorial on Multiple and Hierarchical Inheritance in C++.
Slide 2 In this tutorial we will learn,
  • Multiple Inheritance.
  • Hierarchical Inheritance.
  • We will do this with the help of examples.
Slide 3 To record this tutorial, I am using,
  • Ubuntu OS version 11.10 and
  • g++ compiler v. 4.6.1
Open multiple.cpp


Point the cursor

multiple.cpp

In multiple inheritance, derived class inherits from more than one base class.

Now, we will see an example on multiple inheritance.

I have already typed the code on the editor.

I will open it.

Note that our filename is multiple.cpp

In this program we will display the name, roll no, marks and average of the student.

Let me explain the code.

Highlight

#include<iostream>

This is our headerfile as iostream
Highlight


Here we are using std namespace
Highlight

class student


Then we have class "student".

This is the base class.

Highlight

int roll_no;

char name[20];

In this we have roll_no as integer variable and name as character variable.

These are declared as protected.

Highlight

class exam_inherit

Then we have another class "exam_inherit"

This is also a base class.

Hence we have two base class here- student and exam_inherit.

In this we have 3 variables- sub1, sub2, sub3 as protected.

This is because protected variables can be accessed by derived class.

Highlight

class grade:public student, public exam_inherit

{

Now here we have class "grade" which is the derived class.

This inherits the base classes - class "student" and class "exam_inherit".

Highlight

private:

int avg;

In this we have avg as integer variable declared as private.
Highlight

public:

void input();

void display();

void average();

int total;

void input_exam();

void display_exam();


Then we have function

input()

display()

average()

input_exam()

and display_exam()

as public functions.

In this we have "total" as integer variable declared as public.

Highlight

void grade::input()

{

cout<<"Enter Roll no:";

cin>>roll_no;

cout<<"Enter Name:";

cin>>name;

}

Then we use input function to accept the roll_no and name of the student.
Highlight

void grade::display()

{

cout<<"Roll no is:"<<roll_no<<"\n";

cout<<"Name is:"<<name <<"\n";

}

In display function, we display the roll_no and name of the student.
Highlight

void grade::input_exam()

{

cout<<"Enter marks of subject1:";

cin>>sub1;

cout<<"Enter marks of subject2:";

cin>>sub2;

cout<<"Enter marks of subject3:";

cin>>sub3;

}

Here we have function input_exam.

In this we accept the marks of three subjects as sub1, sub2 and sub3.

Highlight

void exam::display_exam()

{

total=sub1+sub2+sub3;

cout<<"Total is :" <<total<<"\n";

}

Then in display_exam function, we calculate the total of three subjects.

And print the total.

Highlight

void grade::average()

{

avg=total/3;

cout<<"Average is :"<<avg<<"\n";

}

And in function average we calculate the average.
Highlight

int main()

This is our main function.
Highlight

exam ex;

In this we create an object of class grade which is the derived class as gd.
Highlight

gd.input();

gd.input_exam();

gd.display();

gd.display_exam();

gd.average();

}

Then we call all the above functions
Highlight

return 0;

This is the return statement.
Now let us execute the program
Open the terminal by pressing Ctrl, Alt and T keys simultaneously Open the terminal window by pressing Ctrl, Alt and T keys simultaneously, on your keyboard.
Type

g++ multiple.cpp -o mult

To execute

Type

./mult

To compile, type

g++ space multiple.cpp space -o space mult Press Enter


Type

./mult Press Enter

Type:

3

Pratham

67

78

84

Here we see, Enter Roll no.:

I will enter as 3

Enter Name:

I will enter as Pratham

Enter marks of subject1

I will enter as 67

subject2 as 78

subject3 as 84

Highlight

Output

The output is displayed as

Roll no is: 3

Name is: Pratham

Total is: 229

Average is: 76

This was multiple inheritance.

Now we will see hierarchical inheritance.

Come back to our program.

In hierarchical inheritance multiple derived classes inherits from one base class.

Open file hierarchical.cpp Note that our filename is hierarchical.cpp

I will explain the code now.

This is our header file as iostream.
Here we have used the std namespace
Highlight

protected:

int roll_no;

char name[20];

int sub1;

int sub2;

int sub3;

int total;

Then we have class 'student' which is the base class.

In this, we have roll_no as integer variable.

Sub1, sub2, sub3 and total as integer variables.

Then name as character variable.

These are declared protected.

Highlight

class show:public student

{

Here we have another class 'show'.

This is the derived class.

It inherits the properties of class student.


Highlight

public:

void input();

void display();

};

In this we have two functions: "input" and "display".

These are declared as public functions.

Highlight

void show::input()

{

cout<<"Enter Roll no:";

cin>>roll_no;

cout<<"Enter Name:";

cin>>name;

}

In function input we accept the roll_no and name of the student.
Highlight

void show::display()

{

cout<<"Roll no is:"<<roll_no<<"\n";

cout<<"Name is:"<<name <<"\n";

}

In function display we display the roll_no and name of the student.
Highlight

class exam:public student

{

Then we have another derived class as class exam.

This also inherits class student.

We can see that there are two derived class- class exam and class show.

Both the classes inherits the class "student".

Highlight

public:

void input_exam();

void total_marks();

};

In class exam we have two functions as "input_exam" and "total marks" declared as public.
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;

}

Here we access the function "input_exam"

It accepts the marks of three subjects- sub1, sub2 and sub3

Highlight

void exam::total_marks()

{

total=sub1+sub2+sub3;

cout<<"Total is :" <<total<<"\n";

}

Then we have "total_marks" function.

It calculates the total of three subjects and print the total.

Highlight

int main()

{

student st;

show sw;

exam em;

This is our main function.

In this we create objects of three classes

as st, sw and ex.

Highlight

sw.input();

em.input_exam();

sw.display();

em.total_marks();

Then we call all the above functions.

sw.input();

em.input_exam();

sw.display();

em.total_marks();

Highlight

return 0;

}

And this is our return statement.
On the terminal Now let us execute the program.

Come back to the terminal. Let me clear the prompt.

Type

g++ hierarchical.cpp -o hier

Type

./hier

Let us compile.

Type:

g++ space hierarchical.cpp space-o space hier

Press Enter

Type

./hier

Press Enter

On the terminal

Type:

4

Ashwini

87

67

97

Enter Roll no.:

I will enter as 4

Enter Name:

I will enter as Ashwini

Enter marks of subject1

I will enter as 87

subject2 as 67 and subject3 as 97

On the terminal The output is displayed as

Roll no is: 4

Name is: Ashwini

Total is: 251

Switch back to the slides This brings us to the end of this tutorial.

Come back to our slides.

Slide 7

Summary

Let us summarize

In this tutorial, we learnt,

  • Multiple Inheritance.
  • Hierarchical Inheritance.
Slide 6

Assignment

As an assignment

Create a class area and perimeter.

Find the area and perimeter of a rectangle.

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


Acknowledgement

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.

Contributors and Content Editors

Ashwini, Nancyvarkey