Table of Contents
Back Button
Back to Chapter
C++ Setting Up
C++ Introduction
No items found.
C++ Basics
No items found.
C++ Control Statements
No items found.
C++ Logical Operators
No items found.
C++ Procedural Programming
No items found.
C++ Structural Programming
No items found.
C++ Implementation of OOPS
No items found.
C++ Arrays and Vectors
No items found.
C++ Error Handling
No items found.
C++ File Handling
No items found.

C++ Inheritance

Inheritance

During programming we come across certain situations where we are required to make two different classes, majority of class members are same and only a few are different from each other or one of the two classes has a few extra members. Suppose you own a mobile company and you wish to release a new model with a few new features in this situation instead of building a design from scratch you simply add a couple of new features to the existing model. This is made possible using inheritance.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/ac4f635c-74d6-4c83-b1d3-8e43d26da68f/Untitled_Diagram_(25).png

Inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically. In C++, the class which inherits the members of another class is called the derived or child class and the class whose members are inherited is called the base or parent class.

Syntax for Inheritance in classes

class derived_className :: visibility-mode base_className { ...... // body of the derived class. }


Where,

derived_className: It is the name of the derived class.

visibility mode: The visibility mode specifies whether the properties of the base class are publicly inherited or privately inherited. It can be public or private.

base_className: It is the name of the base class.

Let us talk about the different types of inheritance in C++.

Single Inheritance

In single inheritance, a class inherits features from only one class. Single inheritance is like asexual reproduction in plants. In asexual reproduction, part of the parent plant is used to generate a new plant. So in single inheritance there is only one base class and one derived class

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/38781362-478e-47b7-9cc8-8fb1d76e41f7/Untitled_Diagram_(26).png

Example to demonstrate single inheritance.

#include using namespace std; // baseclass class Vehicle { public: Vehicle() { cout << "This is a automobile" << endl; } }; // subclass derived from a single 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 an automobile

Multiple Inheritance

Multiple Inheritance in C++ allows a class to inherit from more than one class. Basically, one sub-class is inherited from more than one base class. Think of this as bisexual reproduction in animals where there are at least two or more parents and a single offspring. The offspring is the derived class whereas the parents are the base classes.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/e1524e90-4dcf-4b8b-9405-5ee1f96b7bec/Untitled_Diagram_(27).png

Syntax:

class subclassName : access_mode base_class1Name, access_mode base_class2Name, .... { .........//body of subclass };
Note: The number of base classes will be separated by a comma (‘, ‘) and access mode for every base class must be specified.

Demonstration of Multiple inheritance

#include using namespace std; // first base class class Vehicle { public: Vehicle() { cout << "This is an automobile" << endl; } }; // second base class class FourWheeler { public: FourWheeler() { cout << "This is a 4 wheeler" << endl; } }; // subclass 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 an automobile This is a 4 wheeler

Multi-level Inheritance

In this type of inheritance, a derived class is created from another derived class.

Let's say your grandfather gave a watch to your father and your father gave this same watch to you after a couple of years and you pass it on to your children. This is an example of multi-level inheritance

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/a1b016a9-f51b-44a6-98ad-6d282f519db8/Untitled_Diagram_(28).png

Demonstration of Multilevel Inheritance

#include using namespace std; // base class class Vehicle { public: Vehicle() { cout << "This is a Vehicle" << endl; } }; // first sub_class derived from class vehicle class fourWheeler: public Vehicle { public: fourWheeler() { cout<<"Objects with 4 wheels are vehicles"<
Output: This is a Vehicle Objects with 4 wheels are vehicles The car has 4 Wheels

Hierarchical Inheritance

In this type of inheritance, more than one sub-class is inherited from a single base class. i.e. more than one derived class is created from a single base class.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/ecdd447b-712c-4756-98d0-6d8847ee238e/Untitled_Diagram_(29).png

Demonstration of hierarchical inheritance.

#include using namespace std; // base class class Vehicle { public: Vehicle() { cout << "This is a Vehicle" << endl; } }; // first subclass class Car: public Vehicle { }; // second subclass class Bus: public Vehicle { }; // main function int main() { // creating object of sub class 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 implemented by combining more than one type of inheritance. For example: Combining Hierarchical inheritance and Multiple Inheritance.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/96f167ec-17f1-4976-a23d-9ef39d755cab/Untitled_Diagram_(30).png

Demonstration for Hybrid Inheritance

#include 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

A special case in hybrid inheritance is Multipath inheritance

In the given diagram class 6 is derived from class 3 and class 2 which share the same parent class 1.

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

Demonstration of hybrid inheritance

#include #include 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

In the above example, both Class B & Class C inherit Class A, they both have single copy of Class A. However Class D inherit both Class B & Class C, therefore Class D have two copies of Class A, one from Class B and another from Class C.

If we need to access the data member an of Class A through the object of Class D, we must specify the path from which a will be accessed, whether it is from Class B or Class C, because the compiler can’t differentiate between two copies of Class A in Class D.

We can avoid this ambiguity using scope resolution operator

Using scope resolution operator we can manually specify the path from which data member a will be accessed, as shown in statements below.

obj.ClassB::a = 10; //Statement 1 obj.ClassC::a = 100; //Statement 2
Ask queries
Contact Us on Whatsapp
Hi, How Can We Help You?