Inheritance in C++
In this lesson: Single, multiple, multilevel, hierarchical and hybrid inheritance in C++ — examples with output. In Hindi & English.
What is inheritance?
Inheritance lets one class build on another. The existing class is the base; the new one is the derived class, which gains the base's members and can add or change behaviour. It captures an "is-a" relationship — a Dog is an Animal — so shared code lives in one place.
Base and derived classes
class Animal { // base
public:
void eat() { cout << "Eating\n"; }
};
class Dog : public Animal { // derived: Dog is-a Animal
public:
void bark() { cout << "Woof\n"; }
};The : public Animal part says Dog inherits publicly from Animal.
A working example
int main() {
Dog d;
d.eat(); // inherited from Animal
d.bark(); // Dog's own
return 0;
}Eating
Woof
A Dog object can use both its own bark() and the inherited eat().
Access and inheritance modes
A derived class sees the base's public and protected members, but not its private ones. The inheritance mode also matters:
| Mode | Public base members become |
|---|---|
public | public (models is-a) — most common |
protected | protected (rare) |
private | private (rare) |
Use protected in the base for data that subclasses should reach. See access specifiers.
Constructor order
When a derived object is created, the base constructor runs first, then the derived one. Destructors reverse this. You can pass arguments to the base constructor from the derived one:
class Animal {
public:
Animal(string n) { cout << n << " created\n"; }
};
class Dog : public Animal {
public:
Dog() : Animal("Dog") { } // call base constructor
};Overriding a function
A derived class can redefine an inherited function to change its behaviour:
class Animal { public: void sound() { cout << "Some sound\n"; } };
class Cat : public Animal {
public:
void sound() { cout << "Meow\n"; } // overrides
};For overriding that works through base-class pointers, you need virtual functions and polymorphism.
Common mistakes
- Trying to access the base's
privatemembers from the derived class. - Forgetting that the base constructor runs before the derived one.
- Using inheritance for a "has-a" relationship where composition fits better.
- Expecting an overridden function to be chosen through a base pointer without
virtual.
Create a Vehicle base with a start() function, and derive Car and Bike that each add their own function. Make objects and call both inherited and own functions.
Summary
- Inheritance lets a derived class reuse and extend a base class.
- It models an "is-a" relationship and avoids code duplication.
- Derived classes see public/protected base members, not private.
- Base constructor runs first; destructors run in reverse.
- Derived classes can override inherited functions.
Frequently Asked Questions
What is inheritance in C++?
What is the difference between a base class and a derived class?
What are the access modes of inheritance in C++?
public, protected or private, which controls how the base's members appear in the derived class. Public inheritance keeps public members public and models "is-a"; it is by far the most common, while protected and private inheritance are rarely used.In what order are constructors called with inheritance in C++?
Can a derived class access private members of the base class in C++?
private members; those stay hidden. It can access public and protected members of the base, so data meant for subclasses is usually declared protected.