C++ Inheritance C-PLUS-PLUS
- C++ Introduction
- C++ Syntax
- C++ Output (cout <<)
- C++ Comments
- C++ Variables
- C++ User Input
- C++ Data Types
- C++ Operators
- C++ Strings
- C++ Math
- C++ Booleans
- C++ If Else
- C++ Switch
- C++ While Loop
- C++ Do/While Loop
- C++ For Loop
- C++ Break and Continue
- C++ Arrays
- C++ References
- C++ Pointers
- C++ Functions
- C++ Functions Parameters
- C++ Function Overloading
- C++ OOP
- C++ Classes and Objects
- C++ Class Methods
- C++ Constructors
- Destructors in C++
- C++ Access Specifiers
- C++ Encapsulation
- C++ Inheritance
- C++ Polymorphism
- C++ Files
- C++ Exception Handling
C++ Inheritance
C++ Inheritance
Inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically. In such way, you can reuse, extend or modify the attributes and behaviors which are defined in other class.
In C++, the class which inherits the members of another class is called derived class and the class whose members are inherited is called a base class. The derived class is the specialized class for the base class.
We group the "inheritance concept" into two categories:
- derived class (child) - the class that inherits from another class
- base class (parent) - the class being inherited from
To inherit from a class, use the :
symbol.
Types Of Inheritance
C++ supports five types of inheritance:
- Single inheritance
- Multiple inheritance
- Hierarchical inheritance
- Multilevel inheritance
- Hybrid inheritance
C++ Single Inheritance
Single inheritance is defined as the inheritance in which a derived class is inherited from the only one base class.
Example
#include <iostream>
#include <string>
using namespace std;// Base class
class Vehicle {
public:
string brand = "Tesla";
void honk() {
cout << "Pooooooo! \n" ;
}
};// Derived class
class Car: public Vehicle {
public:
string model = "cybertruck";
};int main() {
Car myCar;
myCar.honk();
cout << myCar.brand + " " + myCar.model;
return 0;
}
Output:
Pooooooo!
Tesla cybertruck
C++ Multi Level Inheritance
When one class inherits another class which is further inherited by another class, it is known as a multi-level inheritance in C++. Inheritance is transitive so the last derived class acquires all the members of all its base classes.
Example
#include <iostream>
using namespace std;// Parent class
class MyClass {
public:
void myFunction() {
cout << "Some content in parent class." ;
}
};// Child class
class MyChild: public MyClass {
};// Grandchild class
class MyGrandChild: public MyChild {
};int main() {
MyGrandChild myObj;
myObj.myFunction();
return 0;
}
Output:
Some content in parent class.
C++ Multiple Inheritance
Multiple inheritance is the process of deriving a new class that inherits the attributes from two or more classes.
Example
#include <iostream>
using namespace std;// Base class
class MyClass {
public:
void myFunction() {
cout << "Some content in parent class.\n" ;
}
};// Another base class
class MyOtherClass {
public:
void myOtherFunction() {
cout << "Some content in another class.\n" ;
}
};// Derived class
class MyChildClass: public MyClass, public MyOtherClass {
};int main() {
MyChildClass myObj;
myObj.myFunction();
myObj.myOtherFunction();
return 0;
}
Output:
Some content in parent class.
Some content in another class.
Other Types of C++ Inheritance
Hybrid inheritance is a combination of more than one type of inheritance.
C++ Hybrid Inheritance
Hybrid inheritance is a combination of more than one type of inheritance.
C++ Hierarchical Inheritance
Hierarchical inheritance is defined as the process of deriving more than one class from a base class.
Example: A is base class and B, C, D is derived classes