Inheritance in C++
Introduction
Inheritance is one of the important concepts of Object-Oriented Programming (OOP) in C++.
Inheritance allows a new class to acquire the properties and behaviors of an existing class.
The existing class is called the Base Class or Parent Class, and the new class is called the Derived Class or Child Class.
Definition
Inheritance is the mechanism by which one class acquires the properties and functions of another class.
Simple Structure
Base Class | ↓ Derived Class
For example:
Animal | ↓ Dog
A Dog can inherit common features from Animal.
1. Basic Syntax
class DerivedClass : accessSpecifier BaseClass { // Members of derived class };
Example:
class Dog : public Animal { // Dog inherits Animal };
Here:
Animalis the base class.Dogis the derived class.publicis the inheritance mode.
2. Simple Inheritance Example
#include <iostream> using namespace std; class Animal { public: void eat() { cout << "Animal is eating" << endl; } }; class Dog : public Animal { public: void bark() { cout << "Dog is barking" << endl; } }; int main() { Dog d; d.eat(); d.bark(); return 0; }
Output
Animal is eating Dog is barking
Here, Dog inherits the eat() function from Animal.
3. Types of Inheritance
C++ supports several types of inheritance:
- Single Inheritance
- Multilevel Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
4. Single Inheritance
In single inheritance, one derived class inherits from one base class.
Structure
Animal | ↓ Dog
Example
#include <iostream> using namespace std; class Animal { public: void eat() { cout << "Eating" << endl; } }; class Dog : public Animal { public: void bark() { cout << "Barking" << endl; } }; int main() { Dog d; d.eat(); d.bark(); return 0; }
5. Multilevel Inheritance
In multilevel inheritance, a class is derived from another derived class.
Structure
Animal | ↓ Dog | ↓ Puppy
Example
#include <iostream> using namespace std; class Animal { public: void eat() { cout << "Animal eats" << endl; } }; class Dog : public Animal { public: void bark() { cout << "Dog barks" << endl; } }; class Puppy : public Dog { public: void play() { cout << "Puppy plays" << endl; } }; int main() { Puppy p; p.eat(); p.bark(); p.play(); return 0; }
Output
Animal eats Dog barks Puppy plays
6. Multiple Inheritance
In multiple inheritance, one derived class inherits from more than one base class.
Structure
Father Mother \ / \ / ↓ ↓ Child
Example
#include <iostream> using namespace std; class Father { public: void fatherProperty() { cout << "Father's property" << endl; } }; class Mother { public: void motherProperty() { cout << "Mother's property" << endl; } }; class Child : public Father, public Mother { public: void childProperty() { cout << "Child's property" << endl; } }; int main() { Child c; c.fatherProperty(); c.motherProperty(); c.childProperty(); return 0; }
Output
Father's property Mother's property Child's property
7. Hierarchical Inheritance
In hierarchical inheritance, multiple derived classes inherit from a single base class.
Structure
Animal / \ / \ ↓ ↓ Dog Cat
Example
#include <iostream> using namespace std; class Animal { public: void eat() { cout << "Animal eats" << endl; } }; class Dog : public Animal { public: void bark() { cout << "Dog barks" << endl; } }; class Cat : public Animal { public: void meow() { cout << "Cat meows" << endl; } }; int main() { Dog d; Cat c; d.eat(); d.bark(); c.eat(); c.meow(); return 0; }
Output
Animal eats Dog barks Animal eats Cat meows
8. Hybrid Inheritance
Hybrid inheritance is a combination of two or more types of inheritance.
For example, multiple and multilevel inheritance can be combined.
Structure
A / \ B C \ / D
This type of structure can create the diamond problem, which can be handled using virtual inheritance.
9. Access Modes in Inheritance
C++ provides three inheritance access modes:
publicprotectedprivate
Example:
class Dog : public Animal { };
Public Inheritance
class Dog : public Animal { };
Public members of the base class remain public in the derived class.
Protected Inheritance
class Dog : protected Animal { };
Public and protected members inherited from the base class become protected in the derived class.
Private Inheritance
class Dog : private Animal { };
Public and protected members inherited from the base class become private in the derived class.
10. Protected Members
The protected access specifier allows members to be accessed inside the class and its derived classes.
Example
#include <iostream> using namespace std; class Parent { protected: int value = 100; }; class Child : public Parent { public: void display() { cout << "Value = " << value << endl; } }; int main() { Child c; c.display(); return 0; }
Output
Value = 100
11. Constructor and Destructor in Inheritance
When an object of a derived class is created:
- Base class constructor is called first.
- Derived class constructor is called second.
When the object is destroyed:
- Derived class destructor is called first.
- Base class destructor is called second.
Example
#include <iostream> using namespace std; class Parent { public: Parent() { cout << "Parent constructor" << endl; } ~Parent() { cout << "Parent destructor" << endl; } }; class Child : public Parent { public: Child() { cout << "Child constructor" << endl; } ~Child() { cout << "Child destructor" << endl; } }; int main() { Child c; return 0; }
Output
Parent constructor Child constructor Child destructor Parent destructor
12. Advantages of Inheritance
1. Code Reusability
Existing code can be reused in derived classes.
2. Reduces Code Duplication
Common features can be placed in a base class.
3. Easy Maintenance
Changes to common functionality can be made in the base class.
4. Supports Polymorphism
Inheritance is commonly used with runtime polymorphism.
5. Improves Program Structure
Classes can be organized into logical relationships.
13. Real-Life Example
Consider a university system.
Person / \ / \ Student Teacher
The Person class may contain common information:
Name Age Address
The Student class may contain:
Roll Number Course Marks
The Teacher class may contain:
Employee ID Subject Salary
The common properties can be inherited from Person.
14. Inheritance vs Encapsulation
| Inheritance | Encapsulation |
|---|---|
| Allows one class to acquire features of another class. | Combines data and functions inside a class. |
| Promotes code reuse. | Provides controlled access to data. |
| Uses base and derived classes. | Uses classes and access specifiers. |
| Represents an "is-a" relationship. | Protects and organizes data. |
Key Points
- Inheritance is an important OOP feature.
- It supports code reusability.
- The existing class is called the base class.
- The new class is called the derived class.
- C++ supports single, multilevel, multiple, hierarchical, and hybrid inheritance.
- Inheritance can be
public,protected, orprivate. - Constructors execute from base to derived.
- Destructors execute from derived to base.
Summary
Inheritance | ┌──────────────┼──────────────┐ ↓ ↓ ↓ Single Multilevel Multiple | ├──────────────┐ ↓ ↓ Hierarchical Hybrid
Simple Definition
Inheritance = Acquiring the properties and functions of an existing class into a new class.