Inheritance in C++
Inheritance is one of the four pillars of OOP. It allows a child class (derived class) to inherit properties and methods from a parent class (base class). This promotes code reuse — common attributes and behavior are defined once and shared by many classes.
Real-World Analogy
Think of inheritance like a family tree. A child inherits traits from parents. In C++, a Car class can inherit from a Vehicle class — it gets all of Vehicle's features (like speed and fuel) and can add its own (like air conditioning or GPS).
Basic Syntax
class DerivedClass : access_specifier BaseClass {
// additional members
};
Simple Inheritance Example
#include <iostream>
#include <string>
using namespace std;
class Animal {
public:
string name;
void eat() {
cout << name << " is eating." << endl;
}
};
class Dog : public Animal {
public:
void bark() {
cout << name << " is barking." << endl;
}
};
int main() {
Dog d;
d.name = "Buddy";
d.eat(); // inherited from Animal
d.bark(); // Dog's own method
return 0;
}
Output:
Buddy is eating.
Buddy is barking.Types of Inheritance in C++
1. Single Inheritance
One base class, one derived class.
class A { };
class B : public A { };
2. Multilevel Inheritance
A chain of inheritance — derived class becomes the base for another class.
class Animal { };
class Dog : public Animal { };
class GoldenRetriever : public Dog { };
class Animal {
public:
void breathe() { cout << "Breathing..." << endl; }
};
class Dog : public Animal {
public:
void bark() { cout << "Barking..." << endl; }
};
class GoldenRetriever : public Dog {
public:
void fetch() { cout << "Fetching ball..." << endl; }
};
int main() {
GoldenRetriever gr;
gr.breathe(); // from Animal
gr.bark(); // from Dog
gr.fetch(); // own method
return 0;
}
Output:
Breathing...
Barking...
Fetching ball...3. Multiple Inheritance
A derived class inherits from more than one base class.
class Flyable {
public:
void fly() { cout << "Flying..." << endl; }
};
class Swimmable {
public:
void swim() { cout << "Swimming..." << endl; }
};
class Duck : public Flyable, public Swimmable {
public:
void quack() { cout << "Quack!" << endl; }
};
int main() {
Duck d;
d.fly(); // from Flyable
d.swim(); // from Swimmable
d.quack(); // own method
return 0;
}
Output:
Flying...
Swimming...
Quack!4. Hierarchical Inheritance
Multiple classes inherit from the same base class.
class Shape { };
class Circle : public Shape { };
class Square : public Shape { };
class Triangle: public Shape { };
5. Hybrid Inheritance
A combination of multiple and multilevel inheritance.
Access Control in Inheritance
| Base Member | public inheritance | protected inheritance | private inheritance |
|---|---|---|---|
| public | public | protected | private |
| protected | protected | protected | private |
| private | Not inherited | Not inherited | Not inherited |
Constructor Calling in Inheritance
When a derived class object is created, the base class constructor runs first:
class Base {
public:
Base() { cout << "Base constructor" << endl; }
};
class Derived : public Base {
public:
Derived() { cout << "Derived constructor" << endl; }
};
int main() {
Derived d;
return 0;
}
Output:
Base constructor
Derived constructorCalling Base Class Parameterized Constructor
class Person {
public:
string name;
Person(string n) : name(n) {}
};
class Employee : public Person {
public:
int empID;
Employee(string n, int id) : Person(n), empID(id) {}
void display() {
cout << name << " - ID: " << empID << endl;
}
};
int main() {
Employee e("Karan", 1001);
e.display();
return 0;
}
Output:
Karan - ID: 1001Key Takeaways
- Inheritance allows a child class to reuse code from a parent class.
- C++ supports single, multilevel, multiple, hierarchical, and hybrid inheritance.
- Use
publicinheritance to keep the relationship "is-a" (a Dog IS-A Animal). - The base class constructor is always called before the derived class constructor.
- Private members of the base class are NOT directly accessible in derived classes.
