Understanding Inheritance and Its Types In C++
Inheritance Definition
Inheritance is the concept in which a class derives the characters of another class similar to a child deriving characters from his/her parents.
The new class so formed is called the derived class or child class and the old class from which the characters are derived is the base class or parent class. The derived class derives the characters of the base class using ":" operator
- Inheritance helps in code reusability. This not only saves time but also the reliability of the program.
- Inheritance also reduces the complexity of a program.
How inheritance works in C++
In the below diagram, Class B derives all characteristics of class A. Class B may also have some additional characteristics in addition to the derived characteristics.
Consider the characteristics of a man and a woman. Both have a name, an age, height, and weight. But they also have some characteristics which are different from each other. While trying to implement the same in a program like saving the characteristics of a man and a woman, Inheritance can be used as below :
class Characters {
String name;
int age;
float height;
float weight;
}
This is the base class. Separate classes can be written for the other classes based on the usage like,
class Man : public Characters {
}
and
class Woman : public Characters{
}
Here both classes Man and Woman inherits/derives all the characteristics of the class Characters, thus Inheritance reuses and reduces the code.
Inheritance Types
- Single Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Multilevel Inheritance
- Hybrid Inheritance/Virtual Inheritance
Inheritance Example Programs in C++
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Multiple Inheritance Using C++ Programming
- Simple Inheritance Base Class Private Member Example Program
- Simple Inheritance Private Base Class Example Program
- Simple Multi Level Inheritance Example Program
- Simple Hierarchical Inheritance Example Program
Inheritance In C++
Read More Articles
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Example Program For Copy Constructor In C++
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Factorial Using Function Example Program In C++
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program For Constructor In C++
- Simple Example Program for Inline Function Using C++ Programming
- Simple Example Program For Constructor Overloading In C++
- Simple Program for Read user Input Using cin
- Factorial Using Loop Example Program In C++
- Simple Stack Program in C++ Programming
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Do While Loop Example Program In C++
- Simple Program for Multiple Inheritance Using C++ Programming
- Simple Copy Constructor Example Program For Find Factorial In C++
- Simple Program for Exception Handling Divide by zero Using C++ Programming