Cpp Inheritance

In C++, inheritance is a fundamental concept that allows you to create new classes based on existing classes. Inheritance is a way to establish a "is-a" relationship between classes, where a derived class (subclass) inherits properties and behaviors from a base class (superclass). This promotes code reusability and facilitates the creation of a hierarchy of classes.

1. Types of Inheritance:

a. Single Inheritance:
- In single inheritance, a class can inherit from only one base class. This is the most common type of inheritance.
- Example:

        
class Animal {
public:
    void eat() {
        cout << "Animal is eating." << endl;
    }
};

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

b. Multiple Inheritance:
- Multiple inheritance allows a class to inherit from more than one base class.
- Example:

        
class Bird {
public:
    void fly() {
        cout << "Bird is flying." << endl;
    }
};

class Mammal {
public:
    void walk() {
        cout << "Mammal is walking." << endl;
    }
};

class Bat : public Bird, public Mammal {
public:
    void hang() {
        cout << "Bat is hanging." << endl;
    }
        
    

c. Multilevel Inheritance:
- Multilevel inheritance involves creating a chain of classes where one class inherits from another, and then another class inherits from the second class.
- Example:

        
class Grandparent {
public:
    void grandparentMethod() {
        cout << "Grandparent method." << endl;
    }
};

class Parent : public Grandparent {
public:
    void parentMethod() {
        cout << "Parent method." << endl;
    }
};

class Child : public Parent {
public:
    void childMethod() {
        cout << "Child method." << endl;
    }
        
    

d. Hierarchical Inheritance:
- Hierarchical inheritance involves multiple derived classes inheriting from a single base class.
- Example:

        
class Shape {
public:
    void draw() {
        cout << "Drawing a shape." << endl;
    }
};

class Circle : public Shape {
public:
    void draw() {
        cout << "Drawing a circle." << endl;
    }
};

class Rectangle : public Shape {
public:
    void draw() {
        cout << "Drawing a rectangle." << endl;
    }
        
    

e. Hybrid Inheritance:
- Hybrid inheritance is a combination of two or more types of inheritance. It can be a mix of any of the above types.
- Example:

        
class Vehicle {
public:
    void drive() {
        cout << "Driving a vehicle." << endl;
    }
};

class Engine {
public:
    void start() {
        cout << "Starting the engine." << endl;
    }
};

class Car : public Vehicle, public Engine {
public:
    void honk() {
        cout << "Car is honking." << endl;
    }
};

class ElectricCar : public Car {
public:
    void charge() {
        cout << "Electric car is charging." << endl;
    }
        
    

In C++, you can use access specifiers (public, protected, private) to control the visibility and accessibility of inherited members. The public specifier makes the members of the base class public in the derived class, while the protected specifier makes them protected, and the private specifier makes them private.

Inheritance is a powerful feature of C++ that promotes code reuse and abstraction, allowing you to create complex class hierarchies to model real-world relationships and behaviors.