Inheritance in C++
0 3515
Inheritance is known as one of the most essential concepts in object-oriented programming. It permits the users to define a class on the base of another class so that it becomes very simpler for creating and maintaining of any application and reuse of the functionality of the code and also improve the implementation time.
While class creation, instead of writing entire new member functions and data members, the programmer can label that the new class should inherit the member functions of an existing class. And that existing class is known as the base class. The derived class represents the new class.
Syntax of Inheritance
We need to follow the following syntax while creating a sub-class that is inherited from the base class.
A class derived class name: access mode base class name
{
};
Where access mode is one of the access-specifier i.e. public, protected, or private. If the access-specifier is not used, then it is private by default.
A simple program illustrating inheritance implementation
// C++ program of implementation
// of Inheritance
#include <bits/stdc++.h>
using namespace std;
// Base class
class guardian
{
public:
int iden_a;
};
// Sub class inheriting from Base Class(guardian)
class children : public guardian
{
public:
int iden_b;
};
int main()
{
children object1;
// An object of class child has all data members
// and member functions of class parent
object1.iden_a = 21;
object1.iden_b = 25;
cout << "Children identity is " << object1.iden_a << endl;
cout << "Guardian id is " << object1.iden_b << endl;
return 0;
}
Output
Children identity is 21
Guardian id is 25
Reusability concept
The classes of the C++ language can be reused in a number of methods. In other words, once a class has been written and executed in C++, this class can be used by another programmer to reuse it according to their requirements. The most common method is to create a new class from the existing classes. This mechanism is known as inheritance. The new class is referred to as a base and the new one is known as a derived class. A derived class contains entire features of the generic base class and then integrate features specific to the derived class.
Modes of Inheritance
There are three modes of inheritance known as public, private and protected. Let's understand with the help of a simple program example that is given below:
class P
{
public:
int p;
protected:
int q;
private:
int r;
};
class Q : public P
{
// p is public
// q is protected
// r is not accessible from Q
};
class R : protected P
{
// p is protected
// q is protected
// r is not accessible from R
};
class S : private P // 'private' is default for classes
{
// p is private
// q is private
// r is not accessible from S
};
Types of Inheritance
Single Level Inheritance
Multiple Inheritance
Hierarchical Inheritance
Multilevel Inheritance
Hybrid Inheritance
Single Level Inheritance
A class is permitted to inherit features from only one class. In other words, we can say one subclass is inherited by one base class only.
// C++ program to explain
// Single inheritance
#include <iostream>
using namespace std;
// base class
class website {
public:
website()
{
cout << "This is a website" << endl;
}
};
// sub class derived from two base classes
class coding: public website{
};
// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base classes
coding obj;
return 0;
}
Output
This is a website
Multiple Inheritance
A derived class with multiple base class is the concept of multiple inheritance
C++ program to explain explaining multiple inheritance
#include <iostream>
using namespace std;
// first base class
class website {
public:
website()
{
cout << "This is a website" << endl;
}
};
// second base class
class codingsite {
public:
codingsite()
{
cout << "This is coding website" << endl;
}
};
// sub class derived from two base classes
class tutorialsite: public website, public codingsite {
};
// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base classes
tutorialsite obj;
return 0;
}
Output
This is a website
This is coding website
Multilevel Inheritance
In this there is a derived class with one base class and that base class become a derived class of another class is called Multilevel Inheritance.
#include<iostream>
using namespace std;
// base class
class website
{
public:
website()
{
cout << "This is a website" << endl;
}
};
class codingsite: public website
{
public:
codingsite()
{
cout<<"objects in codingsite are coding tutorials"<<endl;
}
};
// sub class derived from two base classes
class tutorialsite: public codingsite{
public:
tutorialsite()
{
cout<<"This site has 5 web designing tutorials"<<endl;
}
};
// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base classes
tutorialsite obj;
return 0;
}
Output
This is a website
objects in codingsite are coding tutorials
This site has 5 web designing tutorials
Hierarchical Inheritance
When there is a concept found where there are multiple derived classes with same base class is known as hierarchical inheritance.
#include <iostream>
using namespace std;
// base class
class website
{
public:
website()
{
cout << "This is a website" << endl;
}
};
// first sub class
class codingsite: public website
{
};
// second sub class
class tutorialsite: public website
{
};
int main()
{
// creating object of sub class will
// invoke the constructor of base class
codingsite object1;
tutorialsite object2;
return 0;
}
Output
This is a website
This is a website
Virtual Inheritance
A derived class with two base classes and these two base classes have one common base class is called multipath or hybrid inheritance.
include <iostream>
sing namespace std;
// base class
class website
{
public:
website()
{
cout << "This is a website" << endl;
}
};
// base class
class ranking
{
public:
ranking()
{
cout<<"rank of the website\n";
}
};
// first sub class
class codingsite: public website
{
};
// second sub class
class tutorialsite: public website, public ranking
{
};
// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base class
tutorialsite obj2;
return 0;
}
Output
This is a website
rank of the website
Share:
Comments
Waiting for your comments