Inheritance in C++ programming language
- In C++, Inheritance is one of the most important key feature of the object oriented programming language in which one class is inherited by another class in such a way that the derived class object acquires all the properties, characteristics, members and member functions and behavior of its base class object.
- Because of the inheritance feature of the object oriented programming language in C++, it gives the opportunity to reuse the code and functionality that make it easier to create and maintain the hundred lines of code of an application.
- The derived class is referred to as the class which inherits the members of the base class.
What is base class?
- The base class is referred to as the class whose members are inherited by the derived class.
The main advantages of inheritance in C++ are readability of the program and code reusability.
Accessing mode in in C++ inheritance
The accessibility of the mode depend upon the base class from which it is derived by the child class.
So have a look on the modes of inheritance.
Modes of Inheritance
- Public mode: If a sub class is derived from a public base class, then the public members of the base class will automatically become public in derived class. If a sub class is derived from a protected base class, then the protected members of the base class will automatically become protected in derived class.
- Protected mode: If a sub class is derived from a Protected base class, then both public members and protected members of the base class will automatically become protected in derived class.
- Private mode: If a sub class is derived from a Private base class, then both public members and protected members of the base class will automatically become Private in derived class.
Note : The private members of the base class cannot be directly accessed in the derived class, while protected members of the base class can be directly accessed in the derived class.
It is clearly given in the below table, the access specifier of the members of the base class in the derived class when the derived class is derived in Public, Protected and Private modes-
Also read:- 26 differences between C programming language and C++ programming language
Types of inheritance
1. Single inheritance:-
In single inheritance, one base class is inherited by only one derived class.
Syntax of single inheritance:-
class derived_class_name : access_mode base_class_name{//body of derived class};
Let's understand with example Code:-
// SINGLE INHERITANCE PROGRAM
#include<iostream.h>
#include<conio.h>
class Parents
{
public:
void say()
{
cout<<"We are parents";
}
};
class Child : public Parents
{
public:
void speak()
{
cout<<"We are children";
}
};
void main()
{
Child cobj;
cobj.say();
cobj.speak();
getch();
};2. Multiple inheritance:-
In multiple inheritance, a derived class is inherited from more than one base classes.
Syntax of multiple inheritance:-
class derived_class_name : access_mode base_class1, access_mode base_class2, ....{//body of derived_class};
Let's understand with example Code:-
#include <iostream.h>
using namespace std;
// base class 1
class Father
{
public:
say()
{
cout << "I'm a father" << endl;
}
};
// base class 2
class Mother {
public:
speak()
{
cout << "I'm a mother" << endl;
}
};
// derived class from two base classes
class Children: public Father, public Mother
{
};
// main function
int main()
{
// creating object of derived class will
Children obj;
return 0;
};3. Multi-level inheritance:-
In multi-level inheritance, one derived class is is created from another derived class.
Syntax of Multi-level inheritance:-
class derived_class1 : access_mode base_class{//body of derived class1};class derived_class2 : access_mode derived_class1{//body of derived class2};
Let's understand with example Code:-
#include<iostream.h>
#include<conio.h>
class Parent
{
public:
void say()
{
cout<<"We are parents";
}
};
class Son : public Parents
{
public:
void speak()
{
cout<<"I am son";
}
};
class Grandson : public Son
{
public:
void speak()
{
cout<<"I am Grandson";
}
};
void main()
{
Grandson obj;
obj.say();
obj.speak();
obj.speak();
getch();
};
4. Hierarchical Inheritance:-
In hierarchical inheritance, more than two derived classes are inherited from single base class.
Let's understand with example Code:-
#include<iostream.h>
#include<conio.h>
class Parents
{
public:
void say()
{
cout<<"We are parents";
}
};
class Son : public Parents
{
public:
void speak()
{
cout<<"I am son";
}
};
class Daughter : public Parents
{
public:
void speak()
{
cout<<"I am Daughter";
}
};
class Grandson : public Daughter
{
public:
void speak()
{
cout<<"I am Grandson";
}
};
void main()
{
Grandson obj;
obj.say();
obj.speak();
obj.speak();
obj.speak();
getch();
};
5. Hybrid Inheritance / Virtual Inheritance:-
The implementation of hybrid inheritance is the combination of more than one type of inheritance, i.e., hierarchical inheritance and multiple inheritance.
Syntax of Hybrid inheritance:-
class derived_class1 : access_mode base_class1{//body of derived class1};class derived_class2 : access_mode base_class2{//body of derived class2};class derived_class3 : access_mode derived_class1, access_mode derived_class2{//body of derived class3};
Let's understand with example Code:-
#include <iostream.h>
using namespace std;
// base class 1
class Father
{
public:
say()
{
cout << "I'm a father" << endl;
}
};
// base class 2
class Mother {
public:
speak()
{
cout << "I'm a mother" << endl;
}
};
// derived class1 from two base classes
class Daughter: public Father, public Mother
{
};
// derived class2 from two base classes
class Son: public Father, public Mother
{
};
// main function
int main()
{
// creating object of derived class will
Son obj;
return 0;
};







0 Comments