C++ Inheritance

Navneeth Menon
5 min readJul 6, 2021

--

Inheritance is a major component of C++’s Object-oriented programming. It enables us to generate a new (derived) class from an existing one. Inheritance refers to a class’s capacity to derive features and traits from another class. One of the most significant features of Object-Oriented Programming is inheritance.

Inheritance in C++: The following syntax must be used to create a sub-class that is inherited from the base class.

class subclass_name : access_mode base_class_name
{
//body of subclass
};

Subclass name is the name of the subclass, and access mode is the mode in which you wish to inherit this subclass, such as public, private, and so on. Where base-class is the name of a previously specified class and access-specifier is one of public, protected, or private. It is private by default if the access specifier is not used.

A C++ class can inherit members from multiple classes, and the extended syntax is as follows:

class derived-class: access baseA, access baseB....

Inheritance is the capacity of a class to inherit all of the characteristics of another class, as the name suggests.
It is one of the most important aspects of C++ object-oriented programming.

Single Inheritance:

A class can only inherit from one other class in single inheritance. i.e. only one base class inherits one subclass.

Class B just extends Class A in the diagram above. A is a superclass, while a subclass is a subclass.

// C++ program to explain

// Single inheritance

#include <iostream>

using namespace std;

// base class class Vehicle {

public: Vehicle(){

cout << “This is a Vehicle” << endl;

}

};

// sub class derived from two base classes class Car:

public Vehicle{

};

// main function

int main()

{

// creating object of sub class will

// invoke the constructor of base classes

Car obj;

return 0;

}

Output: This is a vehicle

Multiple Inheritance:

Multiple Inheritance is a C++ feature that allows a class to inherit from multiple classes. i.e. one subclass inherits from multiple base classes

Class C, as shown in the diagram, extends both Class A and Class B.

// C++ program to explain

// multiple inheritance

#include <iostream>

using namespace std;

// first base class

class Vehicle

{

public: Vehicle()

{

cout << “This is a Vehicle” << endl;

}

};

// second base class

class FourWheeler {

public:

FourWheeler() {

cout << “This is a 4 wheeler Vehicle” << endl;

}

};

// sub class derived from two base classes

class Car: public Vehicle, public FourWheeler {

};

// main function

int main() {

// creating object of sub class will

// invoke the constructor of base classes

Car obj;

return 0;

}

Output: This is a Vehicle This is a 4 wheeler Vehicle

Multilevel Inheritance:

A derived class is formed in this type of inheritance.

Class C is a subclass of B, while B is a subclass of Class A, as illustrated in the diagram.

// C++ program to implement

// Multilevel Inheritance

#include <iostream>

using namespace std;

// base class

class Vehicle {

public: Vehicle(){

cout << “This is a Vehicle” << endl;

}

};

class fourWheeler: public Vehicle {

public: fourWheeler()

{

cout<<”Objects with 4 wheels are vehicles”<<endl;

}

};

// sub class derived from two base classes class Car:

public fourWheeler{

public: car()

{

cout<<”Car has 4 Wheels”<<endl;

}

};

// main function int main()

{

//creating object of sub class will

//invoke the constructor of base classes

Car obj;

return 0;

}

output: This is a Vehicle Objects with 4 wheels are vehicles Car has 4 Wheel

Hierarchical Inheritance:

More than one sub-class is inherited from a single base class in this sort of inheritance. i.e. from a single base class, multiple derived classes are produced.

Class B, C, and D all inherit the same class A, as seen in the example above.

// C++ program to implement

// Hierarchical Inheritance

#include <iostream>

using namespace std;

// base class

class Vehicle

{

public: Vehicle()

{

cout << “This is a Vehicle” << endl;

}

};

// first sub class

class Car: public Vehicle

{

};

// second sub class

class Bus: public Vehicle

{

};

// main function int main()

{

// creating object of subclass will

// invoke the constructor of base class

Car obj1;

Bus obj2;

return 0;

}

Output:

This is a Vehicle

This is a Vehicle

Hybrid Inheritance:

Hybrid inheritance is created by merging multiple inheritance types.

Eg: Combining Hierarchical Inheritance and Multiple Inheritance.

As shown in the example above, all of Class A’s public and protected members are inherited into Class D, first via Class B and then via Class C.

// C++ program for Hybrid Inheritance

#include <iostream>

using namespace std;

// base class

class Vehicle

{

public:

Vehicle()

{

cout << “This is a Vehicle” << endl;

}

};

//base class

class Fare

{

public: Fare()

{

cout<<”Fare of Vehicle\n”;

}

};

// first sub class

class Car:

public Vehicle

{

};

// second sub class

class Bus: public Vehicle, public Fare { };

// main function int main()

{

// creating object of sub class will

// invoke the constructor of base class

Bus obj2;

return 0;

}

Output:

This is a Vehicle

Fare of Vehicle

The virtual base class in C++

When multiple inheritances are employed, virtual base classes are used to avoid numerous “instances” of a given class from appearing in an inheritance hierarchy.

A special case of hybrid inheritance: Multipath inheritance:

A derived class with two base classes and these two base classes have one common base class is called multipath inheritance. An ambiguity can arrise in this type of inheritance

Consider the following program:

// C++ program demonstrating ambiguity in Multipath Inheritance

#include<iostream.h>

#include<conio.h>

class ClassA

{

public: int a;};

class ClassB :

public ClassA {

public: int b;

};

class ClassC : public ClassA

{

public: int c;

};

class ClassD :

public ClassB,

public ClassC { public: int d; };

void main() {

ClassD obj;

//obj.a = 10;

//Statement 1,

Error

//obj.a = 100;

//Statement 2,

Error obj.ClassB::a = 10;

//Statement 3 obj.ClassC::a = 100;

//Statement 4 obj.b = 20;

obj.c = 30;

obj.d = 40;

cout<< “\n A from ClassB :

“<< obj.ClassB::a;

cout<< “\n A from ClassC :

“<< obj.ClassC::a; cout<< “\n B : “<< obj.b;

cout<< “\n C : “<< obj.c;

cout<< “\n D : “<< obj.d;

}

Output:

A from ClassB : 10

A from ClassC : 100

B : 20

C : 30

D : 40

--

--

Navneeth Menon
0 Followers

I’m Navneeth Menon. And I’m a Developer. Currently, I am a Student. I believe that a person should work on developing new things of all time.