The concept of inheritance in C++ is discussed in this article. Additionally, it also discusses the various C++ inheritance types. It is called inheritance when a class has the capacity to take on properties from another class. The class whose properties are inherited is known as the base class, while the class that inherits the properties is known as the derived class.
Imagine a class called "Car," under which there are subclasses like "BMW," "Mercedes," and so on. Under the name "Car," such as "Mercedes," there are other subclasses titled "models of the vehicles." This is a typical example in order to understand the concept of inheritance.
As in the example above, the Mercedes class inherits the property of the Car class, this ability of a class to take properties from another class is known as inheritance.
One of the key fundamentals of Object Oriented Programming is inheritance, which is also super beneficial. It facilitates the reuse of code, which saves a great deal of time, thereby making it an essential concept in order to understand in the world of programming.
A class is a template that outlines the variables as well as the methods that are shared by all objects of a particular kind. Car is a class, just like in the aforementioned example
In inheritance, there are two categories of classes:
Parent Class or Base Class: A class from which a subclass inherits its properties. Car is a parent class, just like in the example mentioned before.
Child Class or Derived Class: A class that inherits a property from another class is known as a "child class" or "derived class." BMW and Mercedes belong to the same child class as in the aforementioned example.
The ":" symbol is used to inherit from a class.
The Car class (child) in the following example inherits the following methods as well as the properties just from the Vehicle class (parent):
#include <iostream>
#include <string>
using namespace std;
// Base class
class Vehicle {
public:
string brand = "BMW";
void honk() {
cout << "hoo, huut! \n" ;
}
};
// Derived class
class Car: public Vehicle {
public:
string model = "BMW X5";
};
int main() {
Car myCar;
myCar.honk();
cout << myCar.brand + " " + myCar.model;
return 0;
}
Output:
hoo, huut! BMW BMW X5
In C++, inheritance is used to allow us to reuse code from pre-existing classes. Strongly promoting the idea of reuse is C++. Reusability is another crucial aspect of OOP (Object Oriented Programming).
In order to save time and improve reliability, it is always preferable to reuse something that already exists rather than trying to construct something similar that does not exist.
In C++, we utilize inheritance when we want a class to use both the properties of its superclass and its own classes in addition to their own properties, and when the two classes in the program share the same logical domain.
For example, there is a base class or parent class named "Animal," and a child class named "Dog." Because the dog is an animal, all of the common attributes of the "Animal" class should be present in the "Dog class," as well as the property of the dog animal.
mainly there are 3 modes of inheritance:
The following table lists the three kinds of inheritance mentioned above and displays the Base class member access specifier in C++ when all three modes of inheritance are used:
There are at least two and perhaps more classes involved in multi-level inheritance. A subclass that has just been formed becomes the base class for a new class, and one class inherits the features from its parent class.
A class might also be derived just from another class that has already been derived from another class.
MyGrandChild is derived from MyChild in the following example (which is derived from MyClass).
#include <iostream>
using namespace std;
// Parent class
class MyClass {
public:
void myFunction() {
cout << "a few items in the parent class." ;
}
};
// Child class
class MyChild: public MyClass {
};
// Grandchild class
class MyGrandChild: public MyChild {
};
int main() {
MyGrandChild myObj;
myObj.myFunction();
return 0;
}
Output:
a few items in the parent class.
A class could be derived from multiple base classes by using a comma-separated list:
A class may inherit from many classes due to C++'s Multiple Inheritance features. Inherited classes' constructors are called in the same order as they were inherited.
For example, in the code below, constructor B is invoked before constructor A.
#include <iostream>
using namespace std;
// Base class
class MyClass {
public:
void myFunction() {
cout << " content in the parent class.\n" ;
}
};
// Another base class
class MyOtherClass {
public:
void myOtherFunction() {
cout <<" content with in the another class.\n" ;
}
};
// Derived class
class MyChildClass: public MyClass, public MyOtherClass {
};
int main() {
MyChildClass myObj;
myObj.myFunction();
myObj.myOtherFunction();
return 0;
}
Output:
content in the parent class. content with in the another class.