22 Jan C++ Inheritance
The Inheritance concept in C++ brings the code reuse functionality by using the inherit feature. This allows a class to inherit the members of a class already present. Therefore, the concept of base and derived class is what Inheritance is all about in C++.
Before moving further, let us first understand the concept of Inheritance with the base an derived class:
- Base class: The base class is the parent class.
- Derived class: The derived class is the child class since it is derived from the parent class.
Let us now see an example of Inheritance wherein we will create a Derived class from a Base class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <iostream> using namespace std; // The Father class i.e. the base class class Father { public: void demo1() { cout << "This is the Parent class." ; } }; // The Kid class i.e. the derived class class Kid: public Father { public: void demo2() { cout << "\nThis is the Child class." ; } }; int main() { // The Kid class object Kid obj; // Accessing members of the base and derived class obj.demo1(); obj.demo2(); return 0; } |
The output is as follows:
1 2 3 4 |
This is the Parent class. This is the Child class. |
In the above example, we have accessed the method of the base class using the derived class object.
The protected Access Specifier
C++ has three access specifiers to access the members of a class. It includes the private, public, and protected. The private and public access specifier, we discussed in the Access Specifiers lesson. Let us now discuss the role of the Protected Access Specifier in C++.
The class members declared as Protected Access Specifiers can be accessed by any derived class of that class. The Kid class can easily access the inherited protected members of the Father class i.e. the base class.
In the below example, the Kid class accesses the inherited protected member age of the Father class i.e. the base class. Let us see the example now:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
#include <iostream> using namespace std; // The Father class i.e. the base class class Father { protected: // The protected access specifier int age; public: // The public access specifier void demo1() { cout << "This is the Parent class." ; } }; // The Kid class i.e. the derived class class Kid: public Father { public: int weight; // The public access specifier void demo2() { cout << "\nThis is the Child class." ; } /* The Child class accessing the inherited protected members of the parent class */ void setAge(int a) { // The public access specifier age = a; } int getAge() { // The public access specifier return age; } }; int main() { // The Kid class object Kid obj; // Calling the methods of both the classes using the Derived class object obj.demo1(); obj.demo2(); // Accessing the protected class member obj.setAge(15); cout << "\nAge = " << obj.getAge() << "\n"; // Accessing the public class member obj.weight = 50; cout << "Weight = " << obj.weight << "\n"; return 0; } |
Output
1 2 3 4 5 6 |
This is the Parent class. This is the Child class. Age = 15 Weight = 50 |
Multilevel Inheritance
We can further inherit a class i.e., the multilevel inheritance concept. For example,
1 2 3 |
FATHER -> KID -> GRANDKID |
Above, the GRANDKID class is derived from the KID class, which is derived from the FATHER class.
Remember, the last derived class will have access to all the base class members i.e., the GRANDKID class will have to access to the members of the FATHER and KID classes (base classes).
Let us now see an example to understand Multilevel Inheritance. In this, we have a base class Father. The derived class is the Kid class, which further derives a class Grandkid. Therefore, we have 3 classes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include <iostream> using namespace std; // The Father class i.e. the base class class Father { public: void demo1() { cout << "This is the Parent class." ; } }; // The Kid class i.e. the derived class class Kid: public Father { public: void demo2() { cout << "\nThis is the Child class." ; } }; // The GrandKid class i.e. the class derived from a derived class class GrandKid: public Kid { public: void demo3() { cout << "\nThis is the GrandChild class." ; } }; int main() { // The GrandKid class object GrandKid obj; // Accessing members of all the classes obj.demo1(); obj.demo2(); obj.demo3(); return 0; } |
The output is as follows:
1 2 3 4 5 |
This is the Parent class. This is the Child class. This is the GrandChild class. |
In the above example, we have accessed the method of the base class using the last derived class object.
Multiple Inheritance
We can easily derive a class from more than a single base class. This defines the concept of Multiple Inheritance in C++. A comma-separated list separates multiple base classes as shown below:
1 2 3 4 5 |
class Kid: public Father, public Mother { // code } |
Let us now see an example to understand Multiple Inheritance. In this, we have two base classes Father and Mother. Therefore, a single derived class Kid is having two base classes i.e., Father and Mother:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include <iostream> using namespace std; // The Father class i.e. the base class class Father { public: void demo1() { cout << "This is the Parent class i.e. Father class (base class)." ; } }; // The Mother class i.e. another base class class Mother { public: void demo2() { cout << "\nThis is the Parent class i.e. Mother class (base class)." ; } }; // The Derived class class Kid: public Father, public Mother { public: void demo3() { cout << "\nThis is the Derived class." ; } }; int main() { // The Kid class object Kid obj; // Accessing members of the base and derived class obj.demo1(); obj.demo2(); obj.demo3(); return 0; } |
The output is as follows:
1 2 3 4 5 |
This is the Parent class i.e. Father class (base class). This is the Parent class i.e. Mother class (base class). This is the Derived class. |
No Comments