Inheritance is an object-oriented programming (OOP) concept that allows a new class (called the derived class) to acquire the properties (attributes) and behaviors (methods) of an existing class (called the base class). The derived class can then add its own attributes and methods or modify the inherited ones. This promotes code reusability and modularity.
Types of Inheritance
There are several types of inheritance in C++ based on how the derived class inherits from the base class.
1. Single Inheritance
In a single inheritance, a derived class inherits from only one base class.
Example:
#include <iostream>
class A {
public:
void show() { std::cout << "called Base class function\n"; }
};
class B : public A {
public:
void display() { std::cout << "called Derived class function\n"; }
};
int main() {
B obj1; // create the Object
// Write C++ code here
obj1.show(); // Inherited from base class
obj1.display(); // Inherited from Derived class
return 0;
}
Explanation:
- Derived class (B) inherits from Base class (A), so it can use the show() function.
- This is single inheritance because Child class (B) has only one base class (A).
Output:
called Derived class function
2. Multiple Inheritance
In multiple inheritance, a derived class inherits from two or more base classes.
#include <iostream>
using namespace std;
// Base class 1
class Animal {
public:
void eat() {
cout << "Animal Eats the food." << endl;
}
};
// Base class 2
class Bird {
public:
void fly() {
cout << "Bird Flies." << endl;
}
};
// Derived class inherits from both Animal and Bird
class Parrot : public Animal, public Bird {
public:
void speak() {
cout << "Parrot speak." << endl;
}
};
int main() {
Parrot parrot;
parrot.eat(); // From Animal
parrot.fly(); // From Bird
parrot.speak(); // From Parrot
return 0;
}
In this example, where the Parrot class inherits features (methods) from two base classes, Animal and Bird.
Output:
Bird Flies.
Parrot speak.
3. Multilevel Inheritance
Multilevel inheritance is a type of inheritance in which a derived class inherits from another derived class, forming a chain of inheritance. In other words, a class (child class) inherits from a base class, and another class (grandchild class) inherits from that child class. This creates a hierarchy or multi-level structure of classes.
#include <iostream>
using namespace std;
// Base class
class Automobile {
public:
void sound() {
cout << "Car sound is good.\n";
}
};
// Derived class 1 (inherits from Automobile)
class Tata : public Automobile {
public:
void about() {
cout << "Tata car is famous in India.\n";
}
};
// Derived class 2 (inherits from Tata)
class Nexon : public Tata {
public:
void run() {
cout << "Nexon speed is good for racing.\n";
}
};
int main() {
// Creating an object of the Nexon class
Nexon obj1;
// Accessing methods from all levels
obj1.sound(); // From Automobile class
obj1.about(); // From Tata class
obj1.run(); // From Nexon class
return 0;
}
Output:
Tata car is famous in India.
Nexon speed is good for racing.
4. Hierarchical Inheritance
Multiple derived classes inherit from a single base class.
#include <iostream>
using namespace std;
// Base class
class Automobile {
public:
void sound() {
cout << "Car sound is good.\n";
}
};
// Derived class 1 (inherits from Automobile)
class Tata : public Automobile {
public:
void about() {
cout << "Tata is Second selling car in India.\n";
}
};
// Derived class 2 (inherits from Automobile)
class Maruti : public Automobile {
public:
void about() {
cout << "Maruti is first selling car in India\n";
}
};
int main() {
Tata obj1;
Maruti obj2;
// Accessing methods from all levels
obj1.about(); // From Tata class
obj2.about(); // From Maruti class
return 0;
}
Output:
Maruti is first selling car in India
5. Hybrid Inheritance
A combination of two or more types of inheritance, such as multiple and multilevel inheritance.
Example:
#include <iostream>
using namespace std;
class Base {
public:
void showBase() {
cout << "Base class show\n";
}
};
class Derived1 : public Base {
public:
void showDerived1() {
cout << "Derived1 class show\n";
}
};
class Derived2 : public Derived1 {
public:
void showDerived2() {
cout << "Derived2 class show\n";
}
};
class Derived3 : public Base {
public:
void showDerived3() {
cout << "Derived3 class show\n";
}
};
int main() {
Derived1 obj1; // create the Object
Derived2 obj2; // create the Object
Derived3 obj3; // create the Object
// Accessing the static variable without an object
obj1.showDerived1();
obj2.showDerived2();
obj3.showDerived3();
return 0;
}
Output:
Derived2 class show
Derived3 class show