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>
usingnamespacestd;
// The Father class i.e. the base class
classFather{
public:
voiddemo1(){
cout<<"This is the Parent class.";
}
};
// The Kid class i.e. the derived class
classKid:publicFather{
public:
voiddemo2(){
cout<<"\nThis is the Child class.";
}
};
intmain(){
// The Kid class object
Kid obj;
// Accessing members of the base and derived class
obj.demo1();
obj.demo2();
return0;
}
The output is as follows:
1
2
3
4
Thisisthe Parentclass.
Thisisthe Childclass.
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>
usingnamespacestd;
// The Father class i.e. the base class
classFather{
protected:// The protected access specifier
intage;
public:// The public access specifier
voiddemo1(){
cout<<"This is the Parent class.";
}
};
// The Kid class i.e. the derived class
classKid:publicFather{
public:
intweight;// The public access specifier
voiddemo2(){
cout<<"\nThis is the Child class.";
}
/* The Child class accessing the inherited
protected members of the parent class */
voidsetAge(inta){// The public access specifier
age=a;
}
intgetAge(){// The public access specifier
returnage;
}
};
intmain(){
// 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";
return0;
}
Output
1
2
3
4
5
6
Thisisthe Parentclass.
Thisisthe Childclass.
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>
usingnamespacestd;
// The Father class i.e. the base class
classFather{
public:
voiddemo1(){
cout<<"This is the Parent class.";
}
};
// The Kid class i.e. the derived class
classKid:publicFather{
public:
voiddemo2(){
cout<<"\nThis is the Child class.";
}
};
// The GrandKid class i.e. the class derived from a derived class
classGrandKid:publicKid{
public:
voiddemo3(){
cout<<"\nThis is the GrandChild class.";
}
};
intmain(){
// The GrandKid class object
GrandKid obj;
// Accessing members of all the classes
obj.demo1();
obj.demo2();
obj.demo3();
return0;
}
The output is as follows:
1
2
3
4
5
Thisisthe Parentclass.
Thisisthe Childclass.
Thisisthe 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
classKid:publicFather,publicMother{
// 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>
usingnamespacestd;
// The Father class i.e. the base class
classFather{
public:
voiddemo1(){
cout<<"This is the Parent class i.e. Father class (base class).";
}
};
// The Mother class i.e. another base class
classMother{
public:
voiddemo2(){
cout<<"\nThis is the Parent class i.e. Mother class (base class).";
}
};
// The Derived class
classKid:publicFather,publicMother{
public:
voiddemo3(){
cout<<"\nThis is the Derived class.";
}
};
intmain(){
// The Kid class object
Kid obj;
// Accessing members of the base and derived class
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok
No Comments