1.- Inheritance and polymorphism
1.- Inheritance and polymorphism
Inheritance and
polymorphism
1/19
3. Inheritnce
Inheritance
Inheritance makes the code reusable; when we inherit an existing class, all
its methods and fields become available in the new class, hence the code is
reused
NOTE: All members of a class except the private ones, are inherited
2/19
3. Inheritance
Hierarchy:
3/19
3. Inheritance
Depending on the access modifier of the parent class, the type of inheritance
can be of one type or of another.
1) Public Inheritance
2) Protected Inheritance
3) Priavte Inheritance
4/19
3. Inheritance
1) Public Inheritance
A Public access specifier results in all public members of the base class becoming public members of
the derived class and all protected members of base class becoming protected members of the
derived class
5/19
3. Inheritance
2) Private Inheritance
A Private member access specifier results in all public and protected members
of the base class becoming private members in the derived class
Note: if we do not specify it, by default the visibility is Private for the inherited
class.
6/19
3. Inheritance
3) Protected Inheritance
7/19
3. Inheritance
output
8/19
3. Inheritance
Constructors
10
3. Inheritance
Destructors
11/19
3. Inheritance
Constructors:
• Constructors are not inherited.
12/19
3. Inheritance
• The destructors of base classes and members are called in the reverse order of the completion of
their constructor:
13/19
Code:
#include<iostream> Output
using namespace std;
int count=0;
class Test
{
public:
Test()
{
count++;
cout<<"\n No. of Object
created:\t"<<count;
}
~Test()
{
cout<<"\n No. of Object
destroyed:\t"<<count;
count--;
}
};
main()
{
Test t,t1,t2,t3;
return 0; 14
}
3. Inheritance
Polymorphism
• Sending the same message to the objects of different classes and each respond
differently depending upon its class
• It works only in the classes that are derived from the same base class
15/19
3. Inheritance
Types of Polymorphism
2. Runtime Polymorphism
16/19
3. Inheritance
• Function Overloading
• Operator Overloading
17/19
3. Inheritance
Function Overloading
• In the function overloading function will call at the time of program compilation.
It is an example of compile-time polymorphism.
18/19
Output
Code
19
3. Inheritance
Operator Overloading
20/19
Output
Code
Code
21
3. Inheritance
Runtime Polymorphism
• In a Runtime polymorphism, functions are called at the time the program execution. Hence, it is known as
late binding or dynamic binding.
• It provides slow execution as it is known at the run time. Thus, It is more flexible as all the things
executed at the run time.
22/19
3. Inheritance
Function overriding
• We give the new definition to base class function in the derived class
• In function overriding, we have two definitions of the same function, one in the superclass and one in the
derived class.
• The decision about which function definition requires calling happens at runtime
23/19
3. Inheritance
• Function in base class, which is overrided in a derived class, that tells the compiler to perform Late
Binding on this function
• They ensure that the correct function is called for an object, regardless of the type of reference or pointer
used for function call.
24/19
Output
Code
25
3. Inheritance
• They always start with virtual keyword and they are finished with “=0”.
26/19
3. Inheritance
Abstract classes
• if we do not override the pure virtual function in derived class, then derived class
also becomes abstract class.
27/19
3. Inheritance
Thank you!
28/19