![Page 1: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/1.jpg)
1
Inheritance in C++
By Avani M. Sakhapara
IT Dept, KJSCE
![Page 2: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/2.jpg)
2
Content
Base and Derived Classes
Single Inheritance
Declaration of derived classes
Order of Constructor and Destructor Execution
Inherited member accessibility
Multiple Inheritance
Virtual Base Classes
![Page 3: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/3.jpg)
3
Base and Derived Classes
A base class is a previously defined class that is used to define new classes
A derived class inherits all the data and function members of a base class (in addition to its explicitly declared members.)
![Page 4: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/4.jpg)
4
Types of Inheritance
![Page 5: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/5.jpg)
5
Types of Inheritance
![Page 6: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/6.jpg)
6
Declaring Derived Classes
visibility-mode =
public|protected|private(default)
![Page 7: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/7.jpg)
7
Example
![Page 8: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/8.jpg)
8
Order of Constructor and
Destructor Execution
Base class constructors are always
executed first.
Destructors are executed in exactly
the reverse order of constructors
The following example, shows you the
ordering of constructors.
![Page 9: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/9.jpg)
9
Example Class Employee{
Public:
Employee();
//…
};
Class SalariedEmployee:public Employee{
Public:
SalariedEmployee();
//…
};
Class ManagementEmployee:public SalariedEmployee{
Public:
ManagementEmployee();
//…
};
ManagementEmployee M;
![Page 10: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/10.jpg)
10
Types of Class Members
private
protected
public
![Page 11: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/11.jpg)
11
Types of Inheritance
public
private
protected
![Page 12: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/12.jpg)
12
Public Inheritance
Public and protected members of the
base class become respectively
public and protected members of the
derived class.
![Page 13: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/13.jpg)
13
Private Inheritance
Public and protected members of the
base class become private members
of the derived class.
![Page 14: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/14.jpg)
14
Protected Inheritance
Public and protected members of the
base class become protected
members of the derived class.
![Page 15: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/15.jpg)
15
Visibility of inherited members
![Page 16: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/16.jpg)
16
Virtual Base Class
![Page 17: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/17.jpg)
17
Virtual Base Class
![Page 18: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/18.jpg)
18
Execution of Constructors
![Page 19: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/19.jpg)
19
Destructor Function
Destructors are called implicitly
starting with the last derived class and
moving in the direction of the base
class.
![Page 20: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/20.jpg)
20
Compatibility Between
Base and Derived Classes
An object of a derived class can be
treated as an object of its base class.
The reverse is not true.
![Page 21: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/21.jpg)
21
Polymorphism
![Page 22: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/22.jpg)
22
Overriding
A function in the derived class with
the same function name will override
the function’s variables in the base
class.
You can still retrieve the overridden
functions variables by using the scope
resolution operator ”::”.
![Page 23: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/23.jpg)
23
Overriding #include <iostream.h>
#include <stdlib.h>
class A
{ int x;
public:
A(){x = 5;}
int get(){return x;}
};
class B: public A
{ int y;
public:
B(){y = 10;}
int get(){return y;}
};
void main()
{ B b;
cout << b.get()<<endl;
cout << b.A::get()<<endl;
}
![Page 24: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/24.jpg)
24
Need of Virtual
Function #include <iostream.h>
#include <stdlib.h>
class A
{ int x;
public:
A(){x = 5;}
int get(){return x;}
};
class B: public A
{ int y;
public:
B(){y = 10;}
int get(){return y;}
};
void main()
{
A *ptr;
A m;
B q;
ptr=&m;//point to base class
cout <<ptr->get()<<endl; //5
ptr=&q;//point to derived class
cout << ptr->get()<<endl; //5
}
![Page 25: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/25.jpg)
25
Need of Virtual Function
In the previous example, the
statement ptr->get() calls the get()
function of base class A every time.
This is because C++ determines
which function to use based on the
type of the pointer instead of based
on the type of the object pointed to by
the base pointer.
![Page 26: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/26.jpg)
26
Virtual Function
Run time polymorphism is achieved
using virtual function
When we have overridden functions
in the base and derived classes, the
function in base class is declared as
virtual using the keyword virtual
before its declaration.
![Page 27: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/27.jpg)
27
Virtual Function
When a function is made virtual, C++
determines which function to use at
run time based on the type of object
pointed to by the base pointer, rather
than the type of pointer.
![Page 28: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/28.jpg)
28
Virtual Function
Example
#include <iostream.h>
#include <stdlib.h>
class A
{ int x;
public:
A(){x = 5;};
virtual int get(){
return x;};
};
class B: public A
{ int y;
public:
B(){y = 10;};
int get(){return y;};
};
void main()
{
A *ptr;
A m;
B q;
ptr=&m;//point to base class
cout <<ptr->get()<<endl; //5
ptr=&q;//point to derived class
cout << ptr->get()<<endl; //10
}
![Page 29: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/29.jpg)
29
Pure Virtual Function
A pure virtual function is a function
that has no definition relative to base.
class.
It is defined as
virtual int get()=0;
In such case each derived class
should define the function or
redeclare it as a pure virtual function.
![Page 30: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/30.jpg)
30
Abstract class A class having pure virtual function is
called abstract class
An object of an abstract class cannot
be created
A base class which is abstract is
called abstract base class.
Abstract base class is used to provide
some traits to the derived classes and
to create a base pointer required for
run time polymorphism.
![Page 31: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/31.jpg)
31
Pure Virtual
Function Example
#include <iostream.h>
class A //abstract class
{ int x;
public:
A(){x = 5;}
virtual int get()=0;
//pure virtual function
};
class B: public A
{ int y;
public:
B(){y = 10;};
int get(){return y;}
//function defined in derived class
};
void main()
{
A *ptr;
A m;//compiler error
B q;
ptr=&q;//point to derived class
cout << ptr->get()<<endl; //10
}
![Page 32: Inheritance in C++ · 3 Base and Derived Classes A base class is a previously defined class that is used to define new classes A derived class inherits all the data and function members](https://reader036.vdocument.in/reader036/viewer/2022081522/5f01bfcc7e708231d400da35/html5/thumbnails/32.jpg)
32
Thank You