4. INHERITANCE
Ranjana Thakuria, CSE Department
The capability of a class to derive properties and characteristics from another
class.
New classes are created from the existing classes.
Reusability can be achieved through inheritance.
The new class created is called derived class or child class or Sub Class
The existing class is known as the base class or parent class or Super class.
The derived class now is said to be inherited from the base class.
6. When to use inheritance?
Ranjana Thakuria, CSE Department
BASE CLASS
DERIVED CLASSes
INHERITANCE
7. Implementing Inheritance in C++:
Ranjana Thakuria, CSE Department
Syntax:
class <derived_class_name> : <access-specifier> <base_class_name> {
//body
}
keyword to create a new class
name of the base class
Public/
Private/
Protected;
private by default
new class name, which will
inherit the base class
Example:
class B: protected A{
………
};
8. Implementing Inheritance
Ranjana Thakuria, CSE Department
Example:
class A{
int a;
………..
};
class B: protected A{
int b;
………
};
A
A
B
B
Does not inherit a full parent object
9. Implementing Inheritance
Ranjana Thakuria, CSE Department
Accessibility:
class A{
private:
int x;
protacted:
int y;
public:
int z;
};
class derived: A{
//y, z accessible
};
class general{
// z accessible
};
10. Modes of Inheritance
Ranjana Thakuria, CSE Department
Public Mode: If derive a subclass from a public base class,
public members public
protected members protected
Protected Mode: If we derive a subclass from a Protected base class
public member protected
protected members protected
Private Mode:
public member private
protected members private
A derived class doesn’t inherit access
to private data members.
class ABC : private XYZ
{ };
Or
class ABC: XYZ
{ }
class ABC : protected XYZ
{ };
class ABC : public XYZ
{ };
12. Example: public Derivation Accessibility
Ranjana Thakuria, CSE Department
class A
{
private:
int a;
protected:
int b;
public:
int c;
};
class B: public A
{
public:
void set()
{
//a=1; //not allowed
b=2; //accessible in further derivation only
c=3; //accessible in further derivation and others
}
};
main()
{
B obj;
//obj.a=1//not allowed
//obj.b=2;//not allowed
obj.c=3;
return 0;
}
13. Ex: protected Derivation Accessibility
Ranjana Thakuria, CSE Department
class A
{
private:
int a;
protected:
int b;
public:
int c;
};
class B: protected A
{
public:
void set()
{
//a=1; //not allowed
b=2; //accessible in further derivation only
c=3; //accessible in further derivation only
}
};
main()
{
B obj;
//obj.a=1//not allowed
//obj.b=2;//not allowed
//obj.c=3 ;//not allowed
return 0;
}
14. Ex: private Derivation Accessibility
Ranjana Thakuria, CSE Department
class A
{
private:
int a;
protected:
int b;
public:
int c;
};
class B: private A
{
public:
void set()
{
//a=1; //not allowed
b=2; //not accessible in further derivation
c=3; //not accessible in further derivation
}
};
main()
{
B obj;
//obj.a=1//not allowed
//obj.b=2;//not allowed
//obj.c=3 ;//not allowed
return 0;
}
15. Types of Inheritances in C++
Ranjana Thakuria, CSE Department
1. Single inheritance:
3. Multilevel inheritance:
5.Hybrid inheritance:
2. Multiple inheritance:
4. Hierarchical inheritance:
Five Types of Inheritances:
16. Single Inheritance:
Ranjana Thakuria, CSE Department
A class is allowed to inherit from only one class.
Syntax:
class subclass_name : access_mode base_class {
// body of subclass
};
Example:
class A { ... .. ...
};
class B: public A {
... .. ...
};
17. Single Inheritance:
Ranjana Thakuria, CSE Department
A class is allowed to inherit from only one class.
Syntax:
class subclass_name : access_mode base_class {
// body of subclass
};
Example:
class A { ... .. ...
};
class B: public A {
... .. ...
};
18. Single Inheritance- Example :
Ranjana Thakuria, CSE Department
class A
{
protected:
int a;
public:
void set_A(int x) {
a=x;
}
void disp_A() {
cout<<endl<<"Value of A="<<a;
}
};
class B: public A
{
int b;
public:
void set_B(int x, int y) {
set_A(x);
b=y;
}
void disp_B() {
disp_A();
cout<<endl<<"Value of B="<<b;
}
};
main()
{
B b;
b.set_B(5,10);
b.disp_B();
return 0;
}
OUTPUT
Value of A=5
Value of B=10
19. Multiple Inheritance:
Ranjana Thakuria, CSE Department
A class can inherit from more than one class.
i.e one subclass is inherited from more than one base class.
Syntax:
class subclass_name : access_mode1 base_class1, access_mode2 base_class2 {
// body of subclass
};
20. Multiple Inheritance:
Ranjana Thakuria, CSE Department
Example:
class A { ... .. ...
};
class B { ... .. ...
};
class C: public A, protected B {
... .. ...
};
21. Multiple Inheritance- Example :
Ranjana Thakuria, CSE Department
// first base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehiclen"; }
};
// second base class
class FourWheeler {
public:
FourWheeler(){
cout << "This is a 4 wheeler Vehiclen";
}
};
// sub class derived from two base classes
class Car : public Vehicle, public FourWheeler {
public:
Car(){cout<<"nConstructed Car";}
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes.
Car obj;
return 0;
}
OUTPUT
This is a Vehicle
This is a 4 wheeler Vehicle
Constructed Car
22. MultiLevel Inheritance:
Ranjana Thakuria, CSE Department
A class is derived from another derived
class.
or one subclass is inherited from
another sub class.
i.e, two levels of single inheritance
24. MultiLevel Inheritance:
Ranjana Thakuria, CSE Department
Example:
#include<iostream>
using namespace std;
class A
{
protected:
int a;
public:
void set_A(){
cout<<"Enter the Value of A=";
cin>>a;
}
void disp_A(){
cout<<endl<<"Value of A="<<a;
}
};
class B: public A
{
protected:
int b;
public:
void set_B()
{
cout<<"Enter the Value of B=";
cin>>b;
}
void disp_B()
{
cout<<endl<<"Value of B="<<b;
}
};
class C: public B
{
int c,p;
public:
void set_C()
{
cout<<"Enter the Value of C=";
cin>>c;
}
void disp_C()
{
cout<<endl<<"Value of C="<<c;
}
void cal_product()
{
p=a*b*c;
cout<<endl<<"Product of "<<a<<" * "<<b<<" *
"<<c<<" = "<<p;
}
};
main()
{
C _c;
_c.set_A();
_c.set_B();
_c.set_C();
_c.disp_A();
_c.disp_B();
_c.disp_C();
_c.cal_product();
return 0;
}
OUTPUT:
Value of A=10
Value of B=20
Value of C=30
Product of 10 * 20 * 30 = 6000
25. Hierarchical Inheritance:
Ranjana Thakuria, CSE Department
More than one subclass is inherited from a single base class.
i.e. more than one derived class is created from a single base class.
27. Hierarchical Inheritance:
Ranjana Thakuria, CSE Department
Example:
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehiclen"; }
};
// first sub class
class Car : public Vehicle {
};
// second sub class
class Bus : public Vehicle {
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base class.
Car obj1;
Bus obj2;
return 0;
}
Output
This is a Vehicle
This is a Vehicle
28. Hybrid Inheritance:
Ranjana Thakuria, CSE Department
Implemented by combining more than one type of inheritance.
Example: Combining Hierarchical inheritance and Multiple Inheritance.
Below image shows the combination of hierarchical and multiple inheritances:
29. Hybrid Inheritance:
Ranjana Thakuria, CSE Department
Example:
// C++ program for Hybrid Inheritance
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehiclen"; }
};
// base class
class Fare {
public:
Fare() { cout << "Fare of Vehiclen"; }
};
// first sub class
class Car : public Vehicle {
};
// second sub class
class Bus : public Vehicle, public Fare {
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base class.
Bus obj2;
return 0;
}
OUTPUT:
This is a Vehicle
Fare of Vehicle
30. Multipath inheritance:
Ranjana Thakuria, CSE Department
A special case of hybrid inheritance:
A derived class with two base classes and these two base classes have one
common base class .
Ambiguity can arise in this type of inheritance.
Ambiguity can be resolved in two ways:
using scope resolution operator
using virtual base class
31. Multipath Ambiguity resolve:
Ranjana Thakuria, CSE Department
Avoiding ambiguity using the scope resolution operator:
obj.ClassB::a = 10; // Statement 3
obj.ClassC::a = 100; // Statement 4
32. Multipath Ambiguity resolve:
Ranjana Thakuria, CSE Department
2) Avoiding ambiguity using the virtual base class:
Class-D has only one copy of ClassA
class ClassA{
public:
int a;
};
class ClassB : virtual public ClassA{
public:
int b;
};
class ClassC : virtual public ClassA{
public:
int c;
};
class ClassD : public ClassB, public ClasC{
public:
int d;
};
int main()
{
ClassD obj;
obj.a = 10; // Statement 3
obj.a = 100; // Statement 4
obj.b = 20;
obj.c = 30;
obj.d = 40;
cout << "n a : " << obj.a;
cout << "n b : " << obj.b;
cout << "n c : " << obj.c;
cout << "n d : " << obj.d << 'n';
}
33. Inheritance and Constructors:
Ranjana Thakuria, CSE Department
Constructors can be inherited.
The constructors of inherited classes are called in the same order in which
they are inherited.
Example Constructors in inheritance:
class A {
A(){}
... .. ...
};
class B: public A {
B():A(){}
... .. ...
};
34. Order of Inheritance- Constructors :
Ranjana Thakuria, CSE Department
class A1{
public:
A1(){
cout << "Constructor of the base class A1 n";
}
};
class A2{
public:
A2(){
cout << "Constructor of the base class A2 n";
}
}; 3 1 2
class S: public A1, virtual A2
{
public:
S(): A1(), A2(){
cout << "Constructor of the derived class S n";
}
};
// Driver code
int main()
{
S obj;
return 0;
}