The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the information about how it affects different properties of the class.
Syntax C++
class DerivedClass : mode_of_inheritance BaseClass {
// Body of the Derived Class
};
where mode of inheritance controls the access level of the inherited members of the base class in the derived class. In C++, there are 3 modes of inheritance:
Mode
Description
Public Inheritance Mode
Public member of the base class will become public in the derived class and protected members of the base class will become protected in the derived class.
Protected Inheritance Mode
Both public and protected members of the base class will become protected in the derived class.
Private Inheritance Mode
Both public members and protected members of the base class will become private in the derived class. Private mode is the default mode that is applied when we don't specify any mode.
Access Base Class MembersMembers of the base class can be accessed in the derived class by simply using their name.
C++
class Base {
public:
int n;
void printN() {
cout << n << endl;
}
};
// Inheriting Base class publicly
class Derived : public Base {
public:
void func () {
// Accessing Base class members
n = 22;
}
};
The public members of the Base class can be accessed through the objects of the Derived class if the Base class is inherited publicly as in the above example.
C++
#include <bits/stdc++.h>
using namespace std;
class Base {
public:
int n;
void printN() {
cout << n << endl;
}
};
// Inheriting Base class publicly
class Derived : public Base {
public:
void func () {
// Accessing Base class members
n = 22;
}
};
int main() {
// Creating objects of derived
Derived d;
// Accessing Derived class member
d.func();
// Accessing Base class member
d.printN();
return 0;
}
Example of Inheritance CPPThe private members in the base class cannot be directly accessed in the derived class, while protected and public members can be directly accessed. To access or update the private members of the base class in derived class, we have to use the corresponding getter and setter functions of the base class or declare the derived class as friend class.
#include <bits/stdc++.h>
using namespace std;
// Base class that is to be inherited
class Parent {
public:
int id_p;
Parent(int x = 22) : id_p(x) {}
void printID_p() {
cout << "Base ID: " << id_p << endl;
}
};
// Derived publicly inheriting from Base
// Class
class Child : public Parent {
public:
int id_c;
Child(int x = 22) : id_c(x) {}
void printID_c() {
cout << "Child ID: " << id_c << endl;
}
};
int main() {
Child obj1;
// An object of class child has all data members
// and member functions of class parent
// so we try accessing the parents method and data from
// the child class object.
obj1.id_p = 7;
obj1.printID_p();
// finally accessing the child class methods and data
// too
obj1.id_c = 91;
obj1.printID_c();
return 0;
}
Base ID: 7 Child ID: 91
Explanation: In the above program, the 'Child' class is publicly inherited from the 'Parent' class so the public data members of the class 'Parent' will also be inherited by the class 'Child'.
Types Of Inheritance in C++The inheritance can be classified on the basis of the relationship between the derived class and the base class. In C++, we have 5 types of inheritances:
In single inheritance, a class is allowed to inherit from only one class. i.e. one base class is inherited by one derived class only.
Single InheritanceExample:
CPP
#include <bits/stdc++.h>
using namespace std;
class Vehicle {
public:
Vehicle() {
cout << "This is a Vehicle"<< endl;
}
};
// Sub class derived from a single base classes
class Car : public Vehicle {
public:
Car() {
cout << "This Vehicle is Car"<< endl;
}
};
int main() {
// Creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
}
This is a Vehicle This Vehicle is Car2. Multiple Inheritance
Multiple Inheritance is a feature of C++ where a class can inherit from more than one class. i.e one subclass is inherited from more than one base class.
Multiple InheritanceExample:
CPP
#include <bits/stdc++.h>
using namespace std;
class LandVehicle {
public:
LandVehicle() {
cout << "This is a LandVehicle"<< endl;
}
};
class WaterVehicle {
public:
WaterVehicle() {
cout << "This is a WaterVehicle"<< endl;
}
};
// sub class derived from two base classes
class AmphibiousVehicle : public WaterVehicle, public LandVehicle {
public:
AmphibiousVehicle() {
cout << "This is an AmphibiousVehicle"<< endl;
}
};
int main() {
// Creating object of sub class will
// invoke the constructor of base classes.
AmphibiousVehicle obj;
return 0;
}
This is a WaterVehicle This is a LandVehicle This is an AmphibiousVehicle3. Multilevel Inheritance
In multilevel inheritance, a derived class is created from another derived class and that derived class can be derived from a base class or any other derived class. There can be any number of levels. For example, a vehicle can be a four-wheeler, and a four-wheeler vehicle can be a car.
Multilevel InheritanceExample:
CPP
#include <bits/stdc++.h>
using namespace std;
class Vehicle {
public:
Vehicle() {
cout << "This is a Vehicle"<< endl;
}
};
class fourWheeler : public Vehicle {
public:
fourWheeler() {
cout << "4 Wheeler Vehicles"<< endl;
}
};
class Car : public fourWheeler {
public:
Car() {
cout << "This 4 Wheeler Vehical is a Car";
}
};
int main() {
// Creating object of sub class will
// invoke the constructor of base classes.
Car obj;
return 0;
}
This is a Vehicle 4 Wheeler Vehicles This 4 Wheeler Vehical is a Car4. Hierarchical Inheritance
In hierarchical inheritance, more than one subclass is inherited from a single base class. i.e. more than one derived class is created from a single base class. For example, cars and buses both are vehicle.
Hierarchical InheritanceExample:
CPP
#include <bits/stdc++.h>
using namespace std;
class Vehicle {
public:
Vehicle() {
cout << "This is a Vehicle"<< endl;
}
};
class Car : public Vehicle {
public:
Car() {
cout << "This Vehicle is Car"<< endl;
}
};
class Bus : public Vehicle {
public:
Bus() {
cout << "This Vehicle is Bus"<< endl;
}
};
int main() {
// Creating object of sub class will
// invoke the constructor of base class.
Car obj1;
Bus obj2;
return 0;
}
This is a Vehicle This Vehicle is Car This is a Vehicle This Vehicle is Bus5. Hybrid Inheritance
Hybrid Inheritance is implemented by combining more than one type of inheritance. For example: Combining Hierarchical inheritance and Multiple Inheritance will create hybrid inheritance in C++.
There is no particular syntax of hybrid inheritance. We can just combine two of the above inheritance types. Below image shows one of the combinations of hierarchical and multiple inheritances:
Hybrid InheritanceExample:
CPP
#include <bits/stdc++.h>
using namespace std;
class Vehicle {
public:
Vehicle() {
cout << "This is a Vehicle"<< endl;
}
};
class Fare {
public:
Fare() {
cout << "Fare of Vehicle"<< endl;
}
};
class Car : public Vehicle {
public:
Car() {
cout << "This Vehical is a Car"<< endl;
}
};
class Bus : public Vehicle, public Fare {
public:
Bus() {
cout << "This Vehicle is a Bus with Fare";
}
};
int main() {
// Creating object of sub class will
// invoke the constructor of base class.
Bus obj2;
return 0;
}
This is a Vehicle Fare of Vehicle This Vehicle is a Bus with Fare
Multipath Inheritance
This is the special case of special case of hybrid inheritance. In multipath inheritance, a class is derived from two base classes and these two base classes in turn are derived from one common base class. An ambiguity can arise in this type of inheritance in the most derived class. This problem is also called diamond problem due to the diamond shape formed in the UML inheritance diagram.
Effects of InheritanceLet's see how different components of class are affected in inheritance:
Static Members and InheritanceIn C++, static members belong to the class itself, not to any object. This means static variables and methods are shared across all instances of the class. When it comes to inheritance, static members from the base class are not inherited by the derived class in the traditional way. However, they can still be accessed using the class name like className::staticMember.
Friend Function and Class in InheritanceFriend functions and classes in inheritance provides functions or classes to access private and protected members of a class, providing flexibility and better control over class interactions. In inheritance, friend function and classes are not inherited by the base class. It means that the classes and functions declared as friends for the base class does not automatically become a friend for derived class.
Constructors and Destructors in InheritanceConstructors and Destructors are generally defined by the programmer and if not, the compiler automatically creates them, so they are present in every class in C++. Now, the question arises what happens to the constructor and destructor when a class is inherited by another class.
In C++ inheritance, the constructors and destructors are not inherited by the derived class, but we can call the constructor of the base class in derived class.
Example:
C++
#include <iostream>
using namespace std;
class Parent {
public:
// base class constructor
Parent() { cout << "Inside base class" << endl; }
};
// sub class
class Child : public Parent {
public:
// sub class constructor
Child() { cout << "Inside sub class" << endl; }
};
int main() {
// creating object of sub class
Child obj;
return 0;
}
Inside base class Inside sub classPolymorphism in Inheritance
In Inheritance, we can redefine the base class member function in the derived class. This type of inheritance is called Function Overriding. Generally, in other programming languages, function overriding is runtime polymorphism but in C++, we can do it at both runtime and compile time. For runtime polymorphism, we have to use the virtual functions.
Example:
C++
#include <bits/stdc++.h>
using namespace std;
class Parent {
public:
void GeeksforGeeks_Print() {
cout << "Base Function" << endl;
}
};
class Child : public Parent {
public:
void GeeksforGeeks_Print() {
cout << "Derived Function";
}
};
int main() {
Child Child_Derived;
Child_Derived.GeeksforGeeks_Print();
return 0;
}
Inheritance vs Polymorphism
Inheritance and Polymorphism both works differently. Inheritance allows a new class to inherit properties from an existing class, promoting code reuse, while polymorphism enables a class to perform tasks in different ways, depending on the method used. Inheritance focuses on class relationships, and polymorphism focuses on method behaviour.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4