Inheritance
Inheritance
Inheritance
// Driver code
int main()
{
// accessing the static data member using scope
// resultion operator
cout << "Accessing static data member: " << A::x
<< endl;
return 0;
}
Inheritance in C++
The capability of a class to derive properties and characteristics from
another class is called Inheritance. Inheritance is one of the most
important features of Object Oriented Programming
Syntax of Inheritance in C++
class derived_class_name : access-specifier base_class_name
{
// body ....
};
where,
class: keyword to create a new class
derived_class_name: name of the new class, which will inherit the
base class
access-specifier: Specifies the access mode which can be either of
private, public or protected. If neither is specified, private is taken
as default.
base-class-name: name of the base class.
Example:
class ABC : private XYZ {...} // private derivation
class ABC : public XYZ {...} // public derivation
class ABC : protected XYZ {...} // protected derivation
class ABC: XYZ {...} // private derivation by
default
Demonstrate the Simple Inheritance of a Class
// C++ program to demonstrate how to inherit a class
#include <iostream>
using namespace std;
// main function
int main()
{
// creating a child class object
Child obj1;
return 0;
}
Output
Base ID: 7
Child ID: 91
Modes of Inheritance in C++
Mode of inheritance controls the access level of the inherited members
of the base class in the derived class. In C++, there are 3 modes of
inheritance:
Public Mode
Protected Mode
Private Mode
Public Inheritance Mode
If we derive a subclass from a public base class. Then the public
member of the base class will become public in the derived class and
protected members of the base class will become protected in the
derived class.
Example:
class ABC : public XYZ {...} // public derivation
Protected Inheritance Mode
If we derive a subclass from a Protected base class. Then both public
members and protected members of the base class will become
protected in the derived class.
Example:
class ABC : protected XYZ {...} // protected derivation
Private Inheritance Mode
If we derive a subclass from a Private base class. Then both public
members and protected members of the base class will become private
in the derived class. They can only be accessed by the member
functions of the derived class.
Private mode is the default mode that is applied when we don’t specify
any mode.
Example:
class ABC : private XYZ {...} // private derivation
class ABC: XYZ {...} // private derivation by default
Types Of Inheritance in C++
The inheritance can be classified on the basis of the relationship
between the derived class and the base class. In C++, we have 5 types
of inheritances:
1. Single inheritance
2. Multilevel inheritance
3. Multiple inheritance
4. Hierarchical inheritance
5. Hybrid inheritance
1. Single Inheritance
In single inheritance, a class is allowed to inherit from only one class.
i.e. one base class is inherited by one derived class only.
Syntax
class subclass_name : access_mode base_class
{
// body of subclass
};
Example:
Single Inheritance
class A
{
... .. ...
};
class B: public A
{
... .. ...
};
Implementation:
CPP
// C++ program to demonstrate how to implement the Single
// inheritance
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
// 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 Vehicle is Car
2. Multiple Inheritance
Multiple Inheritance is a feature of C++ where 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_mode base_class1, access_mode
base_class2, ....
{
// body of subclass
};
Here, the number of base classes will be separated by a comma (‘, ‘)
and the access mode for every base class must be specified and can be
different.
Example:
class B
{
... .. ...
};
class C
{
... .. ...
};
class A: public B, public C
{
... ... ...
};
Implementation:
CPP
// C++ program to illustrate the multiple inheritance
#include <iostream>
using namespace std;
// 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
This 4 Wheeler Vehical is a Car
MUST READ – Multiple Inheritance in C++
3. Multilevel Inheritance
In this type of inheritance, a derived class is created from another
derived class and that derived class can be derived from a base class or
any other derived class. There can be any number of levels.
Syntax
class derived_class1: access_specifier base_class
{
... .. ...
}
class derived_class2: access_specifier derived_class1
{
... .. ...
}
.....
Example:
Multilevel Inheritance in C++
class C
{
... .. ...
};
class B : public C
{
... .. ...
};
class A: public B
{
... ... ...
};
Implementation
CPP
// C++ program to implement Multilevel Inheritance
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
// 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
4 Wheeler Vehicles
This 4 Wheeler Vehical is a Car
MUST READ – Multilevel Inheritance in C++
4. Hierarchical Inheritance
In this type of inheritance, 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.
Syntax
class derived_class1: access_specifier base_class
{
... .. ...
}
class derived_class2: access_specifier base_class
{
... .. ...
}
Example:
class A
{
// body of the class A.
}
class B : public A
{
// body of class B.
}
class C : public A
{
// body of class C.
}
class D : public A
{
// body of class D.
}
Implementation
CPP
// C++ program to implement Hierarchical Inheritance
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
Output
This is a Vehicle
This Vehicle is Car
This is a Vehicle
This Vehicle is Bus
5. Hybrid Inheritance
Hybrid Inheritance is implemented by combining more than one type
of inheritance. For example: Combining Hierarchical inheritance and
Multiple Inheritance will create hybrid inheritance in C++
There is no particular syntax of hybrid inheritance. We can just
combine two of the above inheritance types.
Example:
Below image shows one of the combinations of hierarchical and
multiple inheritances:
Hybrid Inheritance in C++
class F
{
... .. ...
}
class G
{
... .. ...
}
class B : public F
{
... .. ...
}
class E : public F, public G
{
... .. ...
}
class A : public B {
... .. ...
}
class C : public B {
... .. ...
}
Implementation:
CPP
// C++ program to illustrate the implementation of Hybrid Inheritance
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
// base class
class Fare {
public:
Fare() { cout << "Fare of Vehicle\n"; }
};
// 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
This Vehicle is a Bus with Fare
Virtual base class in C++
Virtual base classes are used in virtual inheritance in a way of
preventing multiple “instances” of a given class appearing in an
inheritance hierarchy when using multiple inheritances.
Need for Virtual Base Classes: Consider the situation where we have
one class A . This class A is inherited by two other classes B and C.
Both these class are inherited into another in a new class D as shown in
figure below.
As we can see from the figure that data members/function of
class A are inherited twice to class D. One through class B and second
through class C. When any data / function member of class A is
accessed by an object of class D, ambiguity arises as to which
data/function member would be called? One inherited through B or the
other inherited through C. This confuses compiler and it displays
error.
Example: To show the need of Virtual Base Class in C++
#include <iostream>
using namespace std;
class A {
public:
void show()
{
cout << "Hello from A \n";
}
};
class B : public A {
};
class C : public A {
};
int main()
{
D object;
object.show();
}
Compile Errors:
prog.cpp: In function 'int main()':
prog.cpp:29:9: error: request for member 'show' is ambiguous
object.show();
^
prog.cpp:8:8: note: candidates are: void A::show()
void show()
^
prog.cpp:8:8: note: void A::show()
How to resolve this issue?
To resolve this ambiguity when class A is inherited in both class B and
class C, it is declared as virtual base class by placing a
keyword virtual as :
Syntax for Virtual Base Classes:
Syntax 1:
class B : virtual public A
{
};
Syntax 2:
class C : public virtual A
{
};
Note:
virtual can be written before or after the public. Now only one copy
of data/function member will be copied to class C and class B and
class A becomes the virtual base class. Virtual base classes offer a way
to save space and avoid ambiguities in class hierarchies that use
multiple inheritances. When a base class is specified as a virtual base,
it can act as an indirect base more than once without duplication of its
data members. A single copy of its data members is shared by all the
base classes that use virtual base.
Example 1
CPP14
#include <iostream>
using namespace std;
class A {
public:
int a;
A() // constructor
{
a = 10;
}
};
int main()
{
D object; // object creation of class d
cout << "a = " << object.a << endl;
return 0;
}
Output
a = 10
Explanation :
The class A has just one data member a which is public. This class is
virtually inherited in class B and class C. Now class B and class C use
the virtual base class A and no duplication of data member a is done;
Classes B and C share a single copy of the members in the virtual base
class A.