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

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title of script''': Constructors and Destructors '''Author: '''Ashwini R. Patil '''Keywords: Constructors, Destructors, Video tutorial''' {| style="border-spacing:0;" ! <…')
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
'''Title of script''': Constructors and Destructors
+
'''Title of script''': Objects and Classes in C++
  
'''Author: '''Ashwini R. Patil
+
'''Author: '''Ashwini Patil
  
'''Keywords: Constructors, Destructors, Video tutorial'''
+
'''Keywords: Inheritance, Single level Inheritance, Multilevel Inheritance, Multiple Inheritance, Video tutorial.'''
  
  
  
 
{| style="border-spacing:0;"
 
{| style="border-spacing:0;"
! <center>Visual Cue</center>
+
| 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>
! <center>Narration</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:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| SPreferable to have the title of the slide like Introduction/prerequisite etc...lide 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="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on Constructors and Destructors 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 Inheritance in C++.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| 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="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we will learn about,
+
| 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,
  
'''Constructors.'''
+
Inheritance.
  
'''Destructors.'''
+
Types of inheritance.
  
We will do this with the help of example.
+
We will do this with the help of examples.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| 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:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| 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 Operating System''' version 11.04,
+
'''Ubuntu OS '''version 11.10
  
'''g++''' '''Compiler''' version 4.6.1.
+
'''g++ compiler '''v. 4.6.1
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 4
+
| 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:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us start with the introduction to '''Constructors and Destructors.'''
+
| 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
  
A '''constructor''' is a member function.
+
When one object acquires the property of another it is called as inheritance.
  
It has the same name as the '''class name'''.
+
It is the process of reusing the existing class without modifying them.
  
'''Constructors''' cannot return values.
+
|-
 +
| 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-\
  
'''Constructor''' is automatically called when an object is created.
 
  
Types of Constructors are:
+
| 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
  
'''Parameterized Constructors.'''
+
Single level inheritance
  
'''Copy Constructors.'''
+
Multiple level inheritance
  
'''Default Constructors.'''
+
Hierarchical Inheritance
  
We will discuss about them in another tutorial.No details about destructors. Pls change the opening statement or provide an additional slide for destructors.
+
Multilevel inheritance
 +
 
 +
Hybrid Inheritance
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
| 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:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Destructors'''
+
| 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.
  
'''Destructors''' are used to deallocate memory.
+
The base class has its own properties and functionality.
  
They are called when an '''object''' is destroyed.
+
It is also called as parent class.
  
A '''destructor''' takes no arguments and has no '''return types'''.
+
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:none;padding:0.097cm;"|  
+
| 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:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us look at an example of '''Constructors''',
+
| 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 on the editor,
 
  
So i will open it.
+
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.097cm;"| Point the cursor
+
| 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;"|  
 +
 
 +
 
  
'''constructor.cpp'''
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I have saved the file with the name '''constructor.cpp'''
 
  
Let me explain the code now.
+
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.097cm;"| Highlight
+
| 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:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our '''header file as iostream.'''
+
| 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:none;padding:0.097cm;"| Highlight
+
| 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 std;'''
+
'''using std namespace'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here is 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 are using the '''std namespace'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
| 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 Addition'''
+
'''class''' '''student'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have defined a class '''Addition.'''
+
  
  
  
 +
| 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:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
| 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 a, b ;'''
+
'''int width;'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''a''' and '''b''' are the integer variables.
+
 
 +
'''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:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
| 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.
 +
 
  
'''public'''
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Public''' is the '''access specifier.'''
 
  
It allows the data to be accessible by the other parts of our program.
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
| 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.
  
'''Addition(int, int)'''
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Addition''' is the construtor here.
 
  
A '''constructor''' has the same name as the class name.
 
  
We have passed two arguements here.
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
| 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 add( )'''
+
'''void student::display()'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we have defined a function '''add.'''
+
 
 +
 
 +
 
 +
| 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:none;padding:0.097cm;"| Highlight
+
| 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(a + b);'''
+
'''class exam:public student '''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| It returns sum of a and b.  
+
 
 +
'''{ '''
 +
 
 +
 
 +
 
 +
| 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:none;padding:0.097cm;"| Highlight
+
| 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; '''
  
'''Addition :: Addition (int a, int b)'''
+
'''int msub2; '''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here, we specify that function '''Addition''' is not a global function.
+
  
It is a member function of the class '''Addition.'''  
+
'''int msub3;'''
  
Here we have passed two arguments.
+
'''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.'''
  
'''int x''' and '''int y.'''  
+
As private variables of '''class exam_inherit.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
| 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
  
'''a = x;'''
+
'''public: '''
  
'''b = y;'''
+
'''void input_exam(); '''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now the value of '''x''' will be stored in variable '''a'''.
+
  
And the value of '''y''' will be stored in variable '''b'''.
+
'''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:none;padding:0.097cm;"| Highlight
+
| 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.
 +
 
 +
 
  
'''int main()'''
 
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our '''main function.'''
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
| 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
  
'''Addition obj (3, 4);'''
+
'''void exam::display_exam() '''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''obj''' is the object of class '''Addition'''.
+
  
Here we pass two values as '''3''' and '''4'''.
+
'''{'''
  
'''3''' will be stored in '''x''' and '''4''' will be stored in '''y'''.
+
'''total=sub1+sub2+sub3; '''
  
Which means a's value will be 3 and b's vaue will be 4.
+
'''<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:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
| 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
  
'''<nowiki>cout << “Addition of a and b is ” <<obj.add() <<“\n”</nowiki>'''
+
'''int main()'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Then we print the result.  
+
| 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
  
Here we call the function '''add '''through''' '''the object''' obj'''.
+
'''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:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
| 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:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our '''return''' statement.
+
 
 +
'''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:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on save
+
| 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:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now click on '''Save'''.
+
 
 +
'''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:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
| 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:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| 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;"| Let us execute the program
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| 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
  
'''Ctrl, Alt and T''' keys simultaneously
+
'''Ctrl, Alt and T '''keys simultaneously
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| 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;"| Please open the terminal by pressing '''Ctrl, Alt and T '''keys simultaneously
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type  
+
| 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++ constructor.cpp -o cons'''
+
'''g++ exam_inherit.cpp -o exam'''
  
 
To execute
 
To execute
Line 224: Line 356:
 
Type
 
Type
  
'''./cons'''
+
'''./exam'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| 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 the program type  
  
'''g++ constructor.cpp -o cons'''
+
'''g++ exam_inherit.cpp (space)-o (space) exam'''
  
 
To execute
 
To execute
Line 233: Line 365:
 
Type
 
Type
  
'''./cons'''
+
'''./exam'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
| 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:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here the output 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;"| Here we see,
  
'''Sum is 7.'''
+
'''Enter Roll no.:'''
  
'''And'''
+
'''I will enter as 1'''
  
'''Memory Deallocation'''
+
'''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:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| On the editor
+
| 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:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us see an example on '''Default constructors.'''
+
  
Come back ot our program.
+
'''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
  
I have already typed the code.
+
'''Roll no is: 1'''
  
Note that our filename is '''default.cpp'''
+
'''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.097cm;"| Go to '''File''' menu
+
| 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.
  
Click on '''Save as''' option
+
|-
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| If a constructor is not declared in the class then the compiler assumes a default constructor for the class.
+
| 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:none;padding:0.097cm;"|  
+
| 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:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is our '''header file as iostream.'''
+
 
 +
'''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:none;padding:0.097cm;"|  
+
| 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:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here is the '''std''' namespace.
+
 
 +
'''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:none;padding:0.097cm;"| Type:
+
| 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'''.
  
'''Addition::~Addition();'''
+
|-
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here type '''Addition::~Addition()'''
+
| 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
  
Opening curly bracket '''{ '''
+
'''private: '''
  
then type '''<nowiki>cout << "Memory Deallocation\n"; </nowiki>'''
+
'''int avg; '''
  
and closing surly bracket '''}'''
+
'''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:none;padding:0.097cm;"| On the 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;"| Highlight
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute.
+
 
 +
 
 +
'''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.
 +
 
 +
 
  
Come back to the terminal.
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
+
| 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++ destructor.cpp -o dest'''
+
'''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
  
'''./dest'''
+
'''./multilevel'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Compile and execute as before.
+
| 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++ destructor.cpp -o dest, '''
+
'''g++ multilevel.cpp -o mul'''
  
and press''' Enter.'''
+
'''Press Enter'''
  
To execute,
+
Type
  
Type,
+
'''./multilevel'''
  
'''./dest'''
+
'''Press Enter'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight
+
| 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'''
  
'''output'''
+
'''Enter marks of subject1'''
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here the output is displayed as,
+
  
'''Addition of a and b is 7.'''
+
'''I will enter as 67'''
  
'''Memory Deallocation.'''
+
'''Enter marks of subject1'''
  
This brings us to the end of this tutorial.
+
'''I will enter as 82'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 6
+
| 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:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us summarize,
+
  
In this tutorial we learned,
+
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:
  
'''Constructors'''
+
'''Roll no is: 2 '''
  
eg. '''Addition'''
+
'''Name is: Pratham '''
  
'''Destructors'''
+
'''Total is: 214 '''
  
eg. '''~Addition'''
+
'''Average is: 71'''  
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 7
+
| 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:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| As an assignment,
+
| 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.
  
Write a program to calculate the area of a triangle using constructor.
+
Let us come back to the slides
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 8
+
| 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
  
About the Spoken Tutorial Project
+
Summary
| style="background-color:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Watch the video avail able at the link shown .
+
| 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:
  
It summarises the Spoken Tutorial project.
+
In this tutorial, we have seen,
  
If you do not have good bandwidth, you can download and watch it.
+
Inheritance.
 +
 
 +
Types of inheritance.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 9
+
| 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
  
Spoken Tutorial Workshops
+
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:transparent;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The Spoken Tutorial Project Team,
+
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:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide Number 10
+
| 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:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Spoken Tutorial Project is a part of the Talk to a Teacher 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;"| 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 India.
+
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.
+
More information on this Mission is available at: [http://spoken-tutorial.org/NMEICT-Intro http://spoken-tutorial.org\NMEICT-Intro]
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
| 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:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is Ashwini Patil from IIT Bombay signing off
+
| 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.


Visual Cue
Narration
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


In single level inheritance only one base class and one derived class is needed.

Slide 8



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

Slide 9 In Hierarchical Inheritance multiple derived classes inherits from one base
Slide 10





In Multilevel inheritance the subclass acts as the base class for other classes.

Slide 11 In hybrid inheritance more than one form of inheritance is combined.
Open exam_inherit.cpp


Point the cursor

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


void grade::average()

{

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


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