๐Ÿงพ Unit 7: Inheritance [6 Hrs]


โœ๏ธ What is Inheritance?

Inheritance is one of the core features of Object-Oriented Programming. It allows a new class (called derived class) to acquire the properties and behaviors (data members and member functions) of an existing class (called base class).

๐Ÿ‘‰ It promotes code reuse, extensibility, and maintainability.

Syntax:

class DerivedClassName : access_specifier BaseClassName {
    // Members of the derived class
};

๐Ÿ”น 1. Introduction & Benefits of Inheritance

โœ๏ธ Benefits:

โœ… Example: Basic Inheritance

#include <iostream>
using namespace std;

class Animal {
public:
    void sound() {
        cout << "Animal makes sound" << endl;
    }
};

class Dog : public Animal {
public:
    void bark() {
        cout << "Dog barks" << endl;
    }
};

int main() {
    Dog d;
    d.sound(); // Inherited from Animal
    d.bark();  // Defined in Dog
    return 0;
}

๐Ÿ“ Output:

Animal makes sound
Dog barks