Implementation of Inheritance in C++ – Syntax & Example Code, Learn how to implement inheritance in C++ with a practical example. Understand single, multiple, multilevel, and hierarchical inheritance in C++ programming.
Introduction to Inheritance in C++
Inheritance is one of the core principles of Object-Oriented Programming (OOP) in C++. It allows a class (derived class) to inherit properties and methods from another class (base class). This promotes code re usability and establishes a hierarchical relationship between classes.
In C++, there are four main types of inheritance:
- Single Inheritance
- Multiple Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
Let’s explore each type with examples.
1. Single Inheritance in C++
In single inheritance, a derived class inherits from only one base class.
#include using namespace std; // Base class class Animal { public: void eat() { cout << "Animal is eating..." << endl; } }; // Derived class class Dog : public Animal { public: void bark() { cout << "Dog is barking!" << endl; } }; int main() { Dog myDog; myDog.eat(); // Inherited from Animal myDog.bark(); // Defined in Dog return 0; }
Output
Animal is eating... Dog is barking!
2. Multiple Inheritance in C++
In multiple inheritance, a derived class inherits from more than one base class.
#include using namespace std; // First base class class Bird { public: void fly() { cout << "Bird is flying." << endl; } }; // Second base class class Fish { public: void swim() { cout << "Fish is swimming." << endl; } }; // Derived class inheriting from both Bird and Fish class FlyingFish : public Bird, public Fish { public: void action() { cout << "FlyingFish can fly and swim!" << endl; } }; int main() { FlyingFish ff; ff.fly(); // From Bird ff.swim(); // From Fish ff.action(); // From FlyingFish return 0; }
Output
Bird is flying. Fish is swimming. FlyingFish can fly and swim!
3. Multilevel Inheritance in C++
In multilevel inheritance, a derived class inherits from another derived class, forming a chain of inheritance.
#include using namespace std; // Base class class Vehicle { public: void drive() { cout << "Vehicle is driving." << endl; } }; // Derived class (inherits Vehicle) class Car : public Vehicle { public: void honk() { cout << "Car is honking!" << endl; } }; // Further derived class (inherits Car) class SportsCar : public Car { public: void speedUp() { cout << "SportsCar is speeding up!" << endl; } }; int main() { SportsCar sc; sc.drive(); // From Vehicle sc.honk(); // From Car sc.speedUp(); // From SportsCar return 0; }
Output
Vehicle is driving. Car is honking! SportsCar is speeding up!
4. Hierarchical Inheritance in C++
In hierarchical inheritance, multiple derived classes inherit from a single base class.
#include using namespace std; // Base class class Shape { public: void draw() { cout << "Drawing a shape." << endl; } }; // First derived class class Circle : public Shape { public: void drawCircle() { cout << "Drawing a circle." << endl; } }; // Second derived class class Square : public Shape { public: void drawSquare() { cout << "Drawing a square." << endl; } }; int main() { Circle c; Square s; c.draw(); // From Shape c.drawCircle(); // From Circle s.draw(); // From Shape s.drawSquare(); // From Square return 0; }
Output
Drawing a shape. Drawing a circle. Drawing a shape. Drawing a square.
Inheritance in C++ Conclusion
Inheritance in C++ enhances code reusability and logical structure by allowing classes to inherit properties from other classes. We covered:
- Single Inheritance (one base class)
- Multiple Inheritance (multiple base classes)
- Multilevel Inheritance (chain of inheritance)
- Hierarchical Inheritance (one base, multiple derived classes)
Understanding these concepts is crucial for efficient C++ programming and OOP design.
If you have and query or suggestion or need assistance then please contact me, I will reply to fix your problem, if you like our content then you can subscribe to our Youtube channel. If you want to hire me then reach us at our Fiverr.