ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Object Oriented Programming
    CC-211
    Progress0 / 24 topics
    Topics
    1. Object-Oriented Design: History and Advantages2. Object-Oriented Programming: Terminology and Features3. Classes and Objects4. Data Encapsulation5. Constructors and Destructors6. Access Modifiers7. Const vs Non-Const Functions8. Static Data Members and Functions9. Function Overloading10. Operator Overloading11. Identification of Classes and Their Relationships12. Composition13. Aggregation14. Inheritance15. Multiple Inheritances16. Polymorphism17. Abstract Classes18. Interfaces19. Generic Programming Concepts20. Function Templates21. Class Templates22. Standard Template Library23. Object Streams: Data and Object Serialization24. Exception Handling
    CC-211›Multiple Inheritances
    Object Oriented ProgrammingTopic 15 of 24

    Multiple Inheritances

    8 minread
    1,282words
    Intermediatelevel

    Multiple Inheritance in C++

    Multiple Inheritance is a feature in object-oriented programming where a class can inherit from more than one base class. This allows a derived class to acquire properties and behaviors (attributes and methods) from multiple parent classes, enabling the reuse of code from different sources.

    In C++, a class can derive from multiple classes, which gives it access to the attributes and methods of all its base classes.

    Key Concepts of Multiple Inheritance

    • "Has-a" and "Is-a" Relationships: In multiple inheritance, a class can inherit from more than one base class. It can combine the behaviors of different base classes to form a more specific class. For example, a FlyingCar class can inherit from both Car (has-a relationship) and FlyingMachine (has-a relationship), but it also is-a Car and is-a FlyingMachine at the same time.
    • Combining Behaviors: With multiple inheritance, the derived class can combine or extend the functionality of its base classes. This allows the derived class to have multiple capabilities, which might be appropriate in modeling more complex real-world relationships.

    Syntax of Multiple Inheritance

    In C++, you can inherit from multiple base classes using a comma-separated list of base classes in the class definition.

    class DerivedClass : public BaseClass1, public BaseClass2 {
        // Derived class code
    };
    

    Example of Multiple Inheritance in C++

    Consider a situation where we want to create a class FlyingCar, which inherits from two base classes: Car (for car-like behaviors) and FlyingMachine (for flying behaviors).

    Example Code:

    #include <iostream>
    using namespace std;
    
    // Base class 1: Car
    class Car {
    public:
        void drive() {
            cout << "Driving the car" << endl;
        }
    };
    
    // Base class 2: FlyingMachine
    class FlyingMachine {
    public:
        void fly() {
            cout << "Flying the flying machine" << endl;
        }
    };
    
    // Derived class: FlyingCar, inheriting from both Car and FlyingMachine
    class FlyingCar : public Car, public FlyingMachine {
    public:
        void display() {
            cout << "This is a flying car!" << endl;
        }
    };
    
    int main() {
        // Creating an object of FlyingCar
        FlyingCar fc;
    
        // Using methods from both base classes
        fc.drive();   // Method from Car
        fc.fly();     // Method from FlyingMachine
        fc.display(); // Method from FlyingCar
    
        return 0;
    }
    

    Explanation:

    • The Car class has a method drive(), which represents car-specific behavior.
    • The FlyingMachine class has a method fly(), which represents flying behavior.
    • The FlyingCar class is derived from both Car and FlyingMachine. It can call the methods drive() and fly(), inherited from both base classes, as well as its own method display().

    Output:

    Driving the car
    Flying the flying machine
    This is a flying car!
    

    Issues in Multiple Inheritance

    Although multiple inheritance can be powerful, it comes with certain complexities and potential problems. These include:

    1. Diamond Problem (Ambiguity)

    The diamond problem occurs when a class inherits from two classes that both have a common base class. This can cause ambiguity about which version of the base class’s methods and attributes should be inherited by the derived class. It leads to confusion because the derived class may inherit the same member or function from both paths in the inheritance hierarchy.

    Diamond Problem Example:
    #include <iostream>
    using namespace std;
    
    // Base class 1
    class Animal {
    public:
        void sound() {
            cout << "Animal sound!" << endl;
        }
    };
    
    // Base class 2
    class Dog : public Animal {
    public:
        void sound() {
            cout << "Dog barking!" << endl;
        }
    };
    
    // Base class 3
    class Cat : public Animal {
    public:
        void sound() {
            cout << "Cat meowing!" << endl;
        }
    };
    
    // Derived class
    class Pet : public Dog, public Cat {
        // Pet class has both Dog and Cat classes as base classes
    };
    
    int main() {
        Pet myPet;
        myPet.sound();  // Which sound() method should be called?
        return 0;
    }
    
    Explanation of Diamond Problem:

    In the example, the Pet class inherits from both Dog and Cat, which both inherit from the Animal class. The Dog and Cat classes each override the sound() method from Animal, and the Pet class now has two sound() methods (one from Dog and one from Cat). The compiler is unsure which sound() method to call, leading to ambiguity.

    2. Solution to Diamond Problem: Virtual Inheritance

    The diamond problem can be solved using virtual inheritance, which ensures that only one instance of the common base class (Animal in this case) is inherited, avoiding duplication and ambiguity. This allows the derived class to avoid inheriting multiple copies of the same base class.

    Modified Code with Virtual Inheritance:
    #include <iostream>
    using namespace std;
    
    // Virtual base class
    class Animal {
    public:
        void sound() {
            cout << "Animal sound!" << endl;
        }
    };
    
    // Base class 1 inheriting virtually from Animal
    class Dog : virtual public Animal {
    public:
        void sound() {
            cout << "Dog barking!" << endl;
        }
    };
    
    // Base class 2 inheriting virtually from Animal
    class Cat : virtual public Animal {
    public:
        void sound() {
            cout << "Cat meowing!" << endl;
        }
    };
    
    // Derived class inheriting from Dog and Cat
    class Pet : public Dog, public Cat {
        // No ambiguity now due to virtual inheritance
    };
    
    int main() {
        Pet myPet;
        myPet.sound();  // Which sound() method should be called? Now there is no ambiguity.
        return 0;
    }
    

    Explanation of Virtual Inheritance:

    In this code:

    • Both Dog and Cat classes inherit from Animal virtually, ensuring that only one instance of Animal exists in the Pet class.
    • This removes the ambiguity in method calls, as there is only one sound() method now, from the Pet class’s effective single Animal instance.

    Output:

    Dog barking!
    

    In this case, even though Pet inherits from both Dog and Cat, the Pet class calls the sound() method from the Dog class, which is now correctly inherited via virtual inheritance.

    Advantages of Multiple Inheritance

    1. Code Reusability: Multiple inheritance allows the derived class to reuse code from multiple base classes, avoiding redundancy and making it easier to extend the functionality of existing classes.

    2. Combining Behaviors: It enables a class to combine multiple behaviors. For example, a FlyingCar class can combine the behaviors of both Car and FlyingMachine, allowing it to inherit both driving and flying capabilities.

    3. Expressing Complex Real-World Relationships: Multiple inheritance allows us to represent more complex relationships between objects. For example, a Smartphone might inherit both from Phone (communication) and Camera (photography), combining multiple functionalities.

    Disadvantages of Multiple Inheritance

    1. Complexity and Ambiguity: Multiple inheritance can increase the complexity of the system, making it harder to maintain. The diamond problem, which causes ambiguity in method calls, is one of the major issues that arise in multiple inheritance.

    2. Tight Coupling: The derived class becomes tightly coupled with all the base classes. Changes in the base classes may propagate to the derived class, making the system harder to maintain.

    3. Increased Memory Consumption: If multiple copies of base classes are inherited, it can lead to higher memory usage. Virtual inheritance resolves this to an extent but adds overhead.

    Summary

    • Multiple inheritance allows a class to inherit from more than one base class, enabling it to acquire properties and methods from multiple sources.
    • Advantages include code reusability, combining multiple behaviors, and modeling complex real-world relationships.
    • The main problem in multiple inheritance is the diamond problem, which can be resolved using virtual inheritance.
    • Multiple inheritance can lead to complexity, ambiguity, and tight coupling, so it should be used judiciously in design.
    Previous topic 14
    Inheritance
    Next topic 16
    Polymorphism

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time8 min
      Word count1,282
      Code examples0
      DifficultyIntermediate