Types of Inheritances in CPP

Types of Inheritances in CPP

In C++, inheritance is a mechanism that allows you to create a new class based on an existing class. There are five types of inheritance in C++:

  1. Single Inheritance

  2. Multilevel Inheritance

  3. Multiple Inheritance

  4. Hierarchical Inheritance

  5. Hybrid Inheritance

Let's discuss each of these types of inheritance in detail.

1)Single Inheritance

Single Inheritance is the most commonly used type of inheritance in C++. In single inheritance, a derived class is derived from a single base class. This means that a derived class can inherit the properties of only one base class. The syntax for single inheritance is as follows:

kotlinCopy codeclass Derived_class : access_mode Base_class

Here, the access_mode can be either public, protected or private, and it specifies the access level for the members of the base class in the derived class.

Example:

csharpCopy codeclass Animal {
public:
    void eat() {
        cout << "I am eating" << endl;
    }
};

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

int main() {
    Dog d;
    d.eat();    // public member of Animal class
    d.bark();   // public member of Dog class
    return 0;
}

In the above example, the Dog class inherits the public member of the Animal class using single inheritance.

2) Multilevel Inheritance

Multilevel Inheritance is a type of inheritance in which a derived class is derived from a base class, which is again derived from another base class. This means that a derived class can inherit the properties of more than one base class. The syntax for multilevel inheritance is as follows:

kotlinCopy codeclass Derived_class : access_mode Base_class1, access_mode Base_class2, ..., access_mode Base_classN

Here, the access_mode can be either public, protected or private, and it specifies the access level for the members of the base class in the derived class.

Example:

csharpCopy codeclass Animal {
public:
    void eat() {
        cout << "I am eating" << endl;
    }
};

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

class Poodle : public Dog {
public:
    void wagTail() {
        cout << "I am wagging my tail" << endl;
    }
};

int main() {
    Poodle p;
    p.eat();        // public member of Animal class
    p.bark();       // public member of Dog class
    p.wagTail();    // public member of Poodle class
    return 0;
}

In the above example, the Poodle class is derived from the Dog class, which is itself derived from the Animal class, using multilevel inheritance.

3) Multiple Inheritance

Multiple Inheritance is a type of inheritance in which a derived class is derived from two or more base classes. This means that a derived class can inherit the properties of more than one base class. The syntax for multiple inheritance is as follows:

kotlinCopy codeclass Derived_class : access_mode Base_class1, access_mode Base_class2, ..., access_mode Base_classN

Here, the access_mode can be either public, protected or private, and it specifies the access level for the members of the base class in the derived class.

Example:

cCopy codeclass Animal {
public:
    void eat() {
        cout << "I am eating" << endl;
    }
};

class Machine {
public:
    void work() {
        cout << "I am working" << endl;
    }
};

4) Hierarchical Inheritance

Hierarchical Inheritance is a type of inheritance in which a single base class is inherited by two or more derived classes. This means that multiple derived classes can inherit the properties of a single base class. The syntax for hierarchical inheritance is the same as single inheritance:

kotlinCopy codeclass Derived_class1 : access_mode Base_class
class Derived_class2 : access_mode Base_class
...
class Derived_classN : access_mode Base_class

Here, the access_mode can be either public, protected or private, and it specifies the access level for the members of the base class in the derived class.

Let's consider an example of hierarchical inheritance:

csharpCopy codeclass Animal {
protected:
    string name;

public:
    void setName(string n) {
        name = n;
    }

    void eat() {
        cout << name << " is eating" << endl;
    }
};

class Cat : public Animal {
public:
    void meow() {
        cout << name << " is meowing" << endl;
    }
};

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

int main() {
    Cat c;
    c.setName("Kitty");
    c.eat();    // public member of Animal class
    c.meow();   // public member of Cat class

    Dog d;
    d.setName("Buddy");
    d.eat();    // public member of Animal class
    d.bark();   // public member of Dog class

    return 0;
}

In the above example, the Animal class is inherited by two derived classes, Cat and Dog. The Cat class and Dog class have their own unique functions, meow() and bark() respectively, in addition to the eat() function inherited from the Animal class. This is an example of hierarchical inheritance, where multiple derived classes inherit the properties of a single base class.

5) Hybrid Inheritance

Hybrid inheritance is a combination of multiple inheritance and hierarchical inheritance in C++. In this type of inheritance, a class is derived from multiple base classes, and at least one of the base classes is itself derived from another base class.

The syntax for hybrid inheritance is as follows:

kotlinCopy codeclass Derived_class : access_mode Base_class1, access_mode Base_class2, ..., access_mode Base_classN

Here, the access mode can be either public, private, or protected, and it specifies the access level for the members of the base classes in the derived class.

Let's consider an example of hybrid inheritance:

cCopy codeclass Animal {
protected:
    string name;

public:
    void setName(string n) {
        name = n;
    }

    void eat() {
        cout << name << " is eating" << endl;
    }
};

class Mammal : public Animal {
public:
    void giveBirth() {
        cout << name << " is giving birth" << endl;
    }
};

class Reptile {
protected:
    string name;

public:
    void setName(string n) {
        name = n;
    }

    void crawl() {
        cout << name << " is crawling" << endl;
    }
};

class Lizard : public Reptile {
public:
    void regenerateTail() {
        cout << name << " is regenerating its tail" << endl;
    }
};

class Snake : public Reptile {
public:
    void shedSkin() {
        cout << name << " is shedding its skin" << endl;
    }
};

class Hybrid : public Mammal, public Lizard, public Snake {
public:
    void hybridFunction() {
        cout << name << " is performing a hybrid function" << endl;
    }
};

int main() {
    Hybrid h;
    h.Mammal::setName("Mammal");
    h.Reptile::setName("Reptile");
    h.Snake::setName("Snake");
    h.Lizard::setName("Lizard");
    h.Mammal::giveBirth();
    h.Lizard::regenerateTail();
    h.Snake::shedSkin();
    h.hybridFunction();

    return 0;
}

In this example, we have five classes - Animal, Mammal, Reptile, Lizard, and Snake. The Mammal and Reptile classes are inherited from the Animal class, and the Lizard and Snake classes are inherited from the Reptile class. Finally, the Hybrid class is inherited from the Mammal, Lizard, and Snake classes.

The Hybrid class has access to all the public and protected members of the base classes, including the unique functions of each class. This is an example of hybrid inheritance, where a derived class is derived from multiple base classes, and at least one of the base classes is itself derived from another base class.