Inheritance in C++
Introduction
Inheritance is one of the four pillars of Object-Oriented Programming (OOP). It allows a new class (called the derived or child class) to acquire the properties and behaviors of an existing class (called the base or parent class). This promotes code reusability, extensibility, and models real-world "is-a" relationships.
For example, a Car is a Vehicle, and a Bike is also a
Vehicle. Instead of rewriting speed, fuel, start() and stop() for each, we put
them once in Vehicle and let Car and Bike inherit them.
UML Class Diagram
Fig 3.1 β Vehicle is the base class; Car and Bike publicly inherit from it (is-a relationship).
Definition
Syntax in C++:
class DerivedClass : access-specifier BaseClass {
// additional members of DerivedClass
};
The access-specifier can be public, private, or
protected. It controls how the inherited members are accessed in the derived class.
Types of Inheritance (Access Specifiers)
Public Inheritance
Base public β Derived public; base protected β Derived protected.
Represents a true "is-a" relationship. Most common.
Private Inheritance
All inherited members become private in derived class. Used for "implemented-in-terms-of".
Protected Inheritance
Inherited members become protected. Rare; used in framework design.
Multiple Inheritance
A derived class inherits from more than one base class. Powerful but needs care (diamond problem).
Inheritance Types Tree
Fig 3.2 β Classification of inheritance by number of base/derived classes.
C++ Code Example
A complete example demonstrating public inheritance:
#include <iostream>
using namespace std;
class Vehicle {
protected:
int speed;
public:
Vehicle() : speed(0) {}
void start() { cout << "Vehicle started\n"; }
void stop() { cout << "Vehicle stopped\n"; }
};
class Car : public Vehicle {
private:
int doors;
public:
Car(int d) : doors(d) {}
void honk() { cout << "Car honks! Doors: " << doors << "\n"; }
};
class Bike : public Vehicle {
private:
int gears;
public:
Bike(int g) : gears(g) {}
void wheelie() { cout << "Bike does a wheelie! Gears: " << gears << "\n"; }
};
int main() {
Car c(4);
c.start();
c.honk();
c.stop();
Bike b(6);
b.start();
b.wheelie();
b.stop();
return 0;
}
Real-World Example
Think of an Employee base class (name, id, salary) and derived classes
Manager, Developer, Designer. Each derived class
reuses common data and adds its own (e.g., Developer has programmingLanguage).
Advantages & Disadvantages
β Advantages
- Code reusability
- Extensibility (add features easily)
- Polymorphism support
- Logical classification (is-a)
β Disadvantages
- Tight coupling between base & derived
- Multiple inheritance complexity
- Can break encapsulation if misused
- Deep hierarchies are hard to maintain
Important Questions
Short Questions
- Define inheritance in OOP.
- Differentiate between base class and derived class.
- What is the default access specifier in C++ class inheritance?
- Name the types of inheritance in C++.
Long Questions
- Explain public, private and protected inheritance with examples.
- Discuss advantages and disadvantages of inheritance.
- Write a C++ program demonstrating multilevel inheritance.
MCQ Practice
1. Which keyword is used to inherit a class in C++?
- inherits
- :
- extends
- implements
class Car : public Vehicle.2. In public inheritance, a public member of base becomes:
- private in derived
- public in derived
- protected in derived
- inaccessible
3. Which inheritance type represents a true "is-a" relationship?
- Public inheritance
- Private inheritance
- Protected inheritance
- Multiple inheritance
4. The problem where a class inherits the same base twice through multiple paths is called:
- Diamond of death
- Diamond problem
- Ambiguity error
- Virtual clash
5. Which of the following is NOT a type of inheritance?
- Single
- Multiple
- Multilevel
- Circular
Quick Revision
- Inheritance = derived class acquires base class members.
- Access specifiers: public, private, protected.
- Types: single, multiple, multilevel, hierarchical, hybrid.
- Public inheritance β true "is-a" relationship.
- Solves code reuse; beware of tight coupling.