C++ Inheritance
What Is Inheritance?
Inheritance in C++ is a technique that enables one class to acquire the characteristics (data and behavior) of another. It allows you to build a hierarchy where new classes can extend existing ones, avoiding code repetition and promoting reusability.
In simpler terms, it’s like a child receiving traits from a parent. The new class (called the derived class) reuses or enhances features of the existing class (called the base class).
Why Use Inheritance?
- Helps create new classes without starting from zero
- Builds on existing logic while adding more specific functionality
- Encourages cleaner, structured, and modular programs
- Avoids rewriting the same code in multiple places
Basic Inheritance Example
#includeusing namespace std; class Animal { public: void speak() { cout << "Some generic sound" << endl; } }; class Dog : public Animal { public: void bark() { cout << "Dog barks!" << endl; } }; int main() { Dog d; d.speak(); // Inherited from Animal d.bark(); // Defined in Dog return 0; }
Explanation
- Animal is the origin class, offering the speak() function.
- Dog is a child class, expanding the capabilities by adding bark().
- The Dog object has access to both its own behavior and that of Animal.
Syntax of Inheritance
class DerivedClass : accessModifier BaseClass {
// additional members
};
The accessModifier can be:
- public: Keeps inherited members accessible
- protected: Inherited members stay protected in the derived class
- private: Inherited members become private in the new class
Types of Inheritance in C++
1. Single Inheritance
One subclass is formed from one parent class.
#includeusing namespace std; class Parent { public: void greet() { cout << "Hello from Parent!" << endl; } }; class Child : public Parent { public: void welcome() { cout << "Welcome from Child!" << endl; } }; int main() { Child c; c.greet(); c.welcome(); return 0; }
2. Multiple Inheritance
One class receives features from more than one parent.
#includeusing namespace std; class A { public: void showA() { cout << "Inside A" << endl; } }; class B { public: void showB() { cout << "Inside B" << endl; } }; class C : public A, public B { public: void showC() { cout << "Inside C" << endl; } }; int main() { C obj; obj.showA(); obj.showB(); obj.showC(); return 0; }
3. Multilevel Inheritance
A chain of inheritance—class B from class A, class C from B.
#includeusing namespace std; class Grandparent { public: void sayHello() { cout << "Hello from Grandparent!" << endl; } }; class Parent : public Grandparent { public: void sayHi() { cout << "Hi from Parent!" << endl; } }; class Child : public Parent { public: void sayHey() { cout << "Hey from Child!" << endl; } }; int main() { Child c; c.sayHello(); c.sayHi(); c.sayHey(); return 0; }
4. Hierarchical Inheritance
Several classes stem from a single base.
#includeusing namespace std; class Animal { public: void eat() { cout << "Eating food" << endl; } }; class Dog : public Animal { public: void bark() { cout << "Barking" << endl; } }; class Cat : public Animal { public: void meow() { cout << "Meowing" << endl; } }; int main() { Dog d; d.eat(); d.bark(); Cat c; c.eat(); c.meow(); return 0; }
5. Hybrid Inheritance
A blend of multiple forms; might need virtual inheritance to resolve conflicts.
#includeusing namespace std; class Base { public: void baseFunc() { cout << "Base function" << endl; } }; class A : public Base { public: void funcA() { cout << "Function A" << endl; } }; class B : public Base { public: void funcB() { cout << "Function B" << endl; } }; class C : public A, public B { public: void funcC() { cout << "Function C" << endl; } }; int main() { C obj; obj.A::baseFunc(); // Resolving ambiguity obj.funcA(); obj.funcB(); obj.funcC(); return 0; }
Constructor Behavior in Inheritance
When an object of the derived class is created:
- The base class constructor runs before the derived class constructor.
- The destructor runs in the reverse order—derived class first, then base.
Real-World Analogy (Fresh Explanation)
Imagine a smartphone brand that produces several models. The base class is the "Phone" which includes common parts like screen and battery. Different models like "GamingPhone" or "CameraPhone" (derived classes) inherit these essentials and then add their unique features.
Summary
C++ inheritance offers a way to extend existing code by forming logical relationships between classes. It simplifies complex systems by dividing them into base functionality and extended behaviors. With inheritance, you can build smarter class trees, reduce repetition, and promote scalability in your code—similar to how family traits are passed across generations, but with precise control and flexibility.
Prefer Learning by Watching?
Watch these YouTube tutorials to understand C++ Tutorial visually:
What You'll Learn:
- 📌 Inheritance in C++
- 📌 C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming With Example | Simplilearn