If your class’s concept is clear, you can easily understand inheritance in C++. the following topics we will discuss here,
inheritance in C++
This is an important concept of OOPs. As we know that we can reuse the code to perform the same task in C++. The same happens in inheritance.
In C++ inheritance , we add or inherit the property of one class to another class without modifying any of the classes. The class whose members inherit is called base or parent class and the one who inherits is called derive or child class.
you can understand it in this way, like in the real world, a child inherits some character or property from his parents, some of this character or property can be his own, and some inherit from parents. as the name of the child is its own and the surname receives it in heritage.
similarly, in OOPs, some character or property of one class is inherited in another class, by doing so the properties already exist in the first class whereas by inheriting, those properties are also passed to another class.
A program can have more than one base class and derive classes, in such a way, a base class can be derived for someone else and that derive class is base class for someone else.
It can understand something like this, in the way that the grandfather will be the base for father, and the father will be the base for the child while the father will derive for the grandfather, and the child will derive for the father.
Let’s more about inheritance,
How to inherit a base class into derive-class in C++?
syntax
class derive-class-name : visibility-mode base-class-name
The visibility-mode represents here, that the class is inherited from public , private mode or protected mode.
means, in C++ we can inherit a class in three ways. so before starting inheritance, we discuss about these ways which are called access specifier or visibility-mode.
let’s know about them,
Access specifiers for inheritance in C++
- C++ inheritance Private specifier
- C++ inheritance Public specifier
- C++ inheritance Protected specifier
C++ inheritance Private Mode
When inheriting a class from private mode, all members of the base class become private members for the derive class, which can only be accessed by members of derive -class (public of derive class and public members of base class From, not from the object of the derive class).
Note that private members will not be accessed from anyone’s object nor from derive class members (private or public).
Since the private specifier is by default, the private keyword here will be optional. Like,
class derive class-name : private base-class-name
{
derive class members;
}
Remember, if any type of access specifier is not present, then it will by default private mode. such as,
class derive-class-name : base-class-name
{
derive class members;
}
NOTE
You can understand this with the help of the following diagram –
C++ inheritance Public Mode
When a class inherits publicly. So only the public members of the base class become public for the derived class, which can be accessed from both, members and objects of the derived class and can also be inherited in another class.
NOTE
Its syntax is given below –
class derive-class-name : public base-class-name
{
derive class members;
}
But remember that private members of the class will not be inherited. The following diagram explains it –
But if we want to access the private members of the Base class, then we have to declare those members in a protected mode.
C++ inheritance Protected Mode
As we know that private members of a class cannot be inherited. i.e. a member of a derived class cannot access the private member of the base class. If we have to access private members, then we have to declare all member publicly, but doing so will affect the data hiding.
To overcome this problem, there is a third way to declare members in C++ which is called protected mode. In this, the declared member can be accessed from the derived class members.
syntax
class derive-class-name : protected base-class-name
Here in brief
class class-name
{
private:
private member;. // Accessible only same class member
protected:
protected member; // accessible derive class
public:
public member; //accessible own class and derive
};
NOTE
compare the above three diagram to each other,
Now you are ready to learn inheritance,
Type of inheritance in C++
There are 5 types of inheritance available in C++, these are follows-
- C++ Single level inheritance
- C++ multilevel inheritance
- C++ multiple inheritance
- C++ Hierarchical inheritance
- C++ Hybrid inheritance
C++ Single Level Inheritance
There is only one base class of a derive class. In this type of inheritance, we take only two classes. In which the property of the first class (base class) is inherited in the second class
syntax
class derive-class-name : base-class-name
{
member of derive class;
}
Example
In this, the first class is taking two numbers from the user and the second class is calculating these numbers and printing the total
Here is the program,
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class first //base class
{
protected:
int a,b;
public:
void get_num(int x,int y)
{
a = x;
b = y;
}
}; // first class terminate
class second:public first // class first inherit
{
public:
void get_sum()
{
int sum;
sum = a+b;
cout<<"Total: "<<sum;
}
}; // second class terminate
void main() // main function start here
{
int a,b;
clrscr();
second obj; //class second object declare
cout<<"Enter two number: ";
cin>>a>>b;
obj.get_num(a,b); // class first member call
obj.get_sum(); // class second member call
getch();
}
OUTPUT
Enter two number: 5 6
Total: 11
C++ multi-Level Inheritance
In this type of inheritance, there can be two or more classes, in which the first class is inherited in second class and second class is inherited in the third class and so on if more class available.
in this, the first class is derived class for the base class and the second class whose member inherits it for the next class (in which its members are inherited).
Here, we are work with three-class, see below
Example
In this, the first class is taking two numbers from the user and the second class will calculate these numbers while the third class is printing the total of these numbers.
Here is the program,
#include<iostream>
#include<stdio.h>
using namespace std;
class first // base class
{
protected:
int a,b;
public:
void get_num(int x,int y)
{
a = x;
b = y;
}
}; // first class terminate
class second:public first // class first inherit in class second
{
protected:
int sum;
public:
void get_sum()
{
sum = a+b;
}
}; //second class terminate
class third:public second // class second inherit in class third
{
public:
void show_sum()
{
cout<<"Total: "<<sum;
}
}; // third class terminate
int main() // main function start here
{
int a,b;
third obj; // class third object declare
cout<<"Enter two number: ";
cin>>a>>b;
obj.get_num(a,b); // class first member call
obj.get_sum(); // class second member call
obj.show_sum(); // class third member call
return 0;
}
OUTPUT
Enter two number: 5 6
Total: 11
C++ multiple Inheritance
It has one or more base classes while only one derive class. That is, the property of all base classes is inherited in one single derive class.
syntax
class derive-class-name : mode base1-class_name,mode base1-class_name, ….n
{
member of derive class;
}
Example:-
In this, both, the first class and the second class take one number from the user while the drive class is counting these numbers and printing their total-
Here is the program,
#include<iostream>
#include<stdio.h>
using namespce std;
class first // base class first
{
protected:
int a;
public:
void get_num(int x)
{
a = x;
}
}; // first class terminate
class second // base class second
{
protected:
int b;
public:
void get_num1(int y)
{
b = y;
}
}; //second class terminate
class third:public first,public second // class first and class second inherit in class third
{
int sum;
public:
void show_sum()
{
sum = a+b; // access first and second class member
cout<<"Total: "<<sum;
}
}; // third class terminate
int main() // main function start here
{
int a,b,c;
third obj; // class third object declare
cout<<"Enter two number: ";
cin>>a>>b;
obj.get_num(a); // class first member call
obj.get_num1(b); // class second member call
obj.show_sum(); // class third member call
return 0;
}
OUTPUT
Enter two number: 5 6
Total: 11
Example:-
In this, both, the first class and the second class take one number from the user while the drive class is counting these numbers and printing their total-
C++ Hierarchical Inheritance
This is unlike to multiple inheritance, it has one or more derived classes, whereas only one is base class, that is, the property of a base class is inherited in all other derived classes.
In this, the first-class user is taking two numbers and second class is printing a total of these numbers while the third class is printing multiplication of these number-
Here is the program,
#include<iostream>
#include<stdio.h>
using namespace std;
class first // base class first
{
protected:
int a,b;
public:
void get_num(int x, int y)
{
a = x;
b = y;
}
}; // first class terminate
class second:public first // class first inherit in class second
{
int sum;
public:
void show_sum()
{
sum = a+b; // access first and second class member
cout<<“\nSecond class:”;
cout<<“\nTotal: “<<a<<“+”<<b<<” = “<<sum<<endl;
}
}; //second class terminate
class third:public first // class first inheit in class third
{
int mul;
public:
void show_mul()
{
mul = a*b; // access first and second class member
cout<<“\nThird class:”;
cout<<“\nMultiplicatoin: “<<a<<“x”<<b<<” = “<<mul;
}
}; // third class terminate
int main() // main function start here
{
int a,b,c;
second obj1; // class second obj1 declare
third obj2; // class third obj2 declare
cout<<“Enter two number: “;
cin>>a>>b;
obj1.get_num(a,b); // class first member calling
obj2.get_num(a,b); // class first member call again
cout<<“First class : “;
cout<<"\nNumber are: "<<a<<” “<<b<<endl;
obj1.show_sum(); // class second member call
obj2.show_mul(); // class third member call
return 0;
}
OUTPUT
Enter two number: 5 6
First class:
Number are: 5 6
Second class:
Total: 5+6 = 11
Third class:
Multiplication: 5x6 = 30
C++ Hybrid Inheritance
Hybrid inheritance is a collection of multiple and multi-level inheritance. It has more than one base classes and more than one derive classes
Here is the program, below
#include<iostream>
#include<stdio.h>
using namespace std;
class first //base class first
{
protected:
int a,b;
public:
void get_num(int x, int y)
{
a = x;
b = y;
}
}; // first class terminate
class second:public first // first class inherit in second
{
protected:
int sum;
public:
void get_sum()
{
sum = a+b;
}
}; //second class terminate
class third //base class thirst
{
protected:
int m,n,mul;
public:
void get(int x,int y)
{
m = x;
n = y;
mul = m+n;
}
}; // third class terminate
class fourth:public second,public third // second and third class inherit
{
public:
void show_all()
{
cout<<"\nTotal: "<<a<<"+"<<b<<" = "<<b<<sum;
cout<<"\nMultiplicatoin: "<<m<<"x"<<n<<" = "<<mul;
}
}; //fourth class terminate
int main() // main function start here
{
int a,b;
fourth obj; // fourth class object declare
cout<<"Enter two number: ";
cin>>a>>b;
obj.get_num(a,b); // class first member
obj.get(a,b); // class second member call
obj.get_sum(); // class second member call
obj.show_all(); // class fourth member call
return 0;
}
OUTPUT
Enter two number: 5 6
Total: 5+6 = 11
Multiplication: 5x6 = 30
Did you notice
If you have noticed, then you must have noticed that here only the object of the derive class (except the program of hierarchical inheritance) is created in inheritance, but it is not necessarily that the object of base class is not created. Because here, the program is easily presented. It means to say that here we have inherited all the properties of the base class in the derive class so that there is no need to create the object of the base class.
We know that, in inheritance we inherit the property of one class in another class but also from a nested class we can add the property of one class to another class, but the two differ slightly. In inheritance, we add some specific property of a base class to a derive class as per our requirement, that too without using the object of the base class means, direct access whereas in the nested class, only the property of an entire class Is added to another class, and also to access the members of that class, we have to use the object of that class.
Previous – destructor in C++
Next – File handling concept in C++