Inheritance: Single, Multiple, and Hierarchical
Inheritance allows child classes to derive properties and behaviors from parent classes, modeling natural hierarchies.
What is it?
Inheritance enables a derived class to reuse and extend code from a base class using the `: access-specifier BaseClass` syntax.
Why is it used?
Eliminates duplicate code across related classes (e.g. `Student` and `Instructor` both inheriting from `Person`).
Detailed Explanation
Inheritance Modes:
• `class Derived : public Base`: Public members stay public, protected stay protected.
• Calling Base Constructor: Use initializer list: `Derived(...) : Base(...)`.
• Types of Inheritance in C++:
• Single: One child inherits from one parent.
• Multilevel: Grandparent -> Parent -> Child.
• Multiple: One child inherits from multiple parents simultaneously (e.g. `class SmartPhone : public Phone, public Camera`).
• Diamond Problem: Solved using `virtual public Base`.
Syntax Structure
Syntax Blueprint
class Base { ... };
class Derived : public Base {
public:
Derived(int x) : Base(x) {}
};
Code Example
CPP Code
#include <iostream>
#include <string>
// Base Class
class Person {
protected:
std::string name;
std::string email;
public:
Person(const std::string &person_name, const std::string &person_email)
: name(person_name), email(person_email) {}
void display_identity() const {
std::cout << "Name: " << name << " | Email: " << email << "\n";
}
};
// Derived Class 1: Student
class Student : public Person {
private:
std::string enrolled_course;
public:
Student(const std::string &name, const std::string &email, const std::string &course)
: Person(name, email), enrolled_course(course) {} // Call base constructor
void display_student_card() const {
display_identity(); // Inherited method
std::cout << "Role: Student | Course: " << enrolled_course << "\n";
}
};
int main() {
Student s("Aditya Deshmukh", "aditya@example.com", "KLiC Advanced Python");
std::cout << "=== Student Profile (Inherited) ===\n";
s.display_student_card();
return 0;
}
Explanation of the Code
• `Student : public Person` inherits `name` and `email`.
• Subclass constructor forwards parameters to `Person(name, email)`.
• `Student` accesses inherited `protected` members directly.
Expected Output
=== Student Profile (Inherited) ===
Name: Aditya Deshmukh | Email: aditya@example.com
Role: Student | Course: KLiC Advanced Python
Real-World Application
Used across GUI toolkits (where `Button`, `TextBox`, and `Slider` all inherit from `UIWidget`).
Common Mistakes to Avoid
• Forgetting `public` in `class Derived : public Base` (in C++, inheritance is private by default!).
• Forgetting to call base class constructor in derived class initializer list.
Quick Practice Challenge
Create an `Employee` class that inherits from `Person` and adds an `int salary` attribute.
Key Takeaways
• Use `: public BaseClass` to inherit openly.
• Forward parameters to base constructor via initializer list.
• `protected` members are accessible inside derived subclasses.