Multiple Inheritance - Rybd04/2143-OOP GitHub Wiki

Definition

Multiple Inheritance

Allows a class to inherit properties and behaviors from more than one base class.

Explanation

Imagine a robot dog that has a robot parent and a dog parent. The robot dog can do things that both the robot and the dog can do.

Basic Code Example

#include <iostream>
using namespace std;

// Base Class 1
class Parent1 {
public:
    void showParent1() {
        cout << "This is Parent1\n";
    }
};

// Base Class 2
class Parent2 {
public:
    void showParent2() {
        cout << "This is Parent2\n";
    }
};

// Derived Class (inherits from both Parent1 and Parent2)
class Child : public Parent1, public Parent2 {
public:
    void showChild() {
        cout << "This is the Child\n";
    }
};

int main() {
    Child obj;
    obj.showParent1(); // Inherited from Parent1
    obj.showParent2(); // Inherited from Parent2
    obj.showChild();   // Own method
    return 0;
}

Image

image

Additional Resources

https://www.geeksforgeeks.org/multiple-inheritance-in-c/ https://www.w3schools.com/cpp/cpp_inheritance_multiple.asp

⚠️ **GitHub.com Fallback** ⚠️