ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP2111Multiple Inheritance
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP2111

    Object Oriented Programming

    Progress0 / 23 done
    Topics23
    1Introduction to object oriented design2History and advantages of object oriented design3Introduction to object oriented programming concepts4Classes and objects5Data encapsulation6Constructors and destructors7Access modifiers8Const vs non-const functions9Static data members & functions10Function overloading11Operator overloading12Identification of classes and their relationships13Composition and aggregation14Inheritance15Multiple inheritance16Polymorphism17Abstract classes and interfaces18Generic programming concepts19Function & class templates20Standard template library21Object streams22Data and object serialization using object streams23Exception handling
    7 min read
    Topic 15 of 23
    Topic Study Note Illustration
    15

    Multiple inheritance

    Multiple Inheritance in C++

    Multiple Inheritance in C++

    Multiple inheritance is a feature in object-oriented programming (OOP) where a derived class can inherit from more than one base class. In C++, this allows a class to inherit characteristics (attributes and methods) from multiple classes, promoting code reuse and flexibility.

    What is Multiple Inheritance?

    In multiple inheritance, a single derived class inherits from two or more base classes. The derived class gets access to all the public and protected members of its base classes. This allows the derived class to combine the functionalities of multiple classes.

    For example, if we have two base classes Animal and Vehicle, a Car class can inherit from both to represent a "car" that is both an animal (because it might have behaviors like eat() or move()) and a vehicle (with characteristics like start() or stop()).

    Syntax of Multiple Inheritance:

    class DerivedClass : public BaseClass1, public BaseClass2 {
        // Members and methods of DerivedClass
    };
    
    • BaseClass1 and BaseClass2 are the base classes that the derived class inherits from.
    • The access specifier (public, protected, or private) determines the visibility of the base class members in the derived class.

    Example of Multiple Inheritance:

    #include <iostream>
    using namespace std;
    
    // Base class 1
    class Animal {
    public:
        void eat() {
            cout << "Animal is eating." << endl;
        }
    };
    
    // Base class 2
    class Vehicle {
    public:
        void drive() {
            cout << "Vehicle is driving." << endl;
        }
    };
    
    // Derived class that inherits from both Animal and Vehicle
    class Car : public Animal, public Vehicle {
    public:
        void honk() {
            cout << "Car is honking." << endl;
        }
    };
    
    int main() {
        Car myCar;
        myCar.eat();      // Inherited from Animal
        myCar.drive();    // Inherited from Vehicle
        myCar.honk();     // Defined in Car
    
        return 0;
    }
    

    Explanation:

    • The Car class is derived from both Animal and Vehicle classes.
    • The Car object myCar can call the methods eat() (from Animal), drive() (from Vehicle), and honk() (which is defined in the Car class itself).

    Key Points About Multiple Inheritance:

    1. Code Reusability:

      • Multiple inheritance allows you to reuse code from multiple base classes. For example, a class can inherit from both Shape (for geometric properties) and Color (for color properties).
    2. Combining Behaviors:

      • A class can inherit and combine behaviors from different base classes. For example, a class FlyingCar might inherit from both Car and Plane, combining features of both transportation methods.
    3. Complex Relationships:

      • Multiple inheritance can help model complex relationships where a class must be treated as an instance of multiple base types. This is useful when a class has more than one "role."

    Issues in Multiple Inheritance:

    While multiple inheritance provides flexibility, it can also introduce some complexities. Here are some challenges:

    1. Ambiguity Problem (Diamond Problem):

    The diamond problem occurs when two base classes have a method or member with the same name, and a derived class inherits from both base classes. The compiler may be unable to determine which version of the method or member to use.

    Example of Diamond Problem:

    #include <iostream>
    using namespace std;
    
    // Base class 1
    class A {
    public:
        void show() {
            cout << "Class A" << endl;
        }
    };
    
    // Base class 2
    class B : public A {
    public:
        void show() {
            cout << "Class B" << endl;
        }
    };
    
    // Base class 3
    class C : public A {
    public:
        void show() {
            cout << "Class C" << endl;
        }
    };
    
    // Derived class
    class D : public B, public C {
    public:
        // This will cause ambiguity because both B and C have their own show() method.
    };
    
    int main() {
        D obj;
        obj.show();  // Error: Ambiguous call to show()
        return 0;
    }
    

    Solution to Diamond Problem:

    The diamond problem can be avoided by using virtual inheritance. Virtual inheritance ensures that the base class is only included once in the derived class, resolving ambiguity.

    Example with Virtual Inheritance:

    #include <iostream>
    using namespace std;
    
    // Base class 1
    class A {
    public:
        void show() {
            cout << "Class A" << endl;
        }
    };
    
    // Base class 2, virtually inherits A
    class B : virtual public A {
    public:
        void show() {
            cout << "Class B" << endl;
        }
    };
    
    // Base class 3, virtually inherits A
    class C : virtual public A {
    public:
        void show() {
            cout << "Class C" << endl;
        }
    };
    
    // Derived class
    class D : public B, public C {
    public:
        void show() {
            cout << "Class D" << endl;
        }
    };
    
    int main() {
        D obj;
        obj.show();  // Output: Class D (no ambiguity)
        return 0;
    }
    

    Explanation:

    • In this example, both B and C virtually inherit from A, which means A will only be included once in the D class. This prevents the ambiguity when calling the show() function.

    2. Inheritance of Constructors:

    In multiple inheritance, constructors are not inherited by the derived class. Each base class's constructor must be called explicitly in the derived class.

    Example of Constructor in Multiple Inheritance:

    #include <iostream>
    using namespace std;
    
    // Base class 1
    class A {
    public:
        A() {
            cout << "Constructor of A" << endl;
        }
    };
    
    // Base class 2
    class B {
    public:
        B() {
            cout << "Constructor of B" << endl;
        }
    };
    
    // Derived class
    class D : public A, public B {
    public:
        D() {
            cout << "Constructor of D" << endl;
        }
    };
    
    int main() {
        D obj;
        // Output:
        // Constructor of A
        // Constructor of B
        // Constructor of D
        return 0;
    }
    

    Explanation:

    • The constructors of base classes A and B are called before the constructor of the derived class D.

    Summary of Multiple Inheritance in C++:

    1. Definition:

      • Multiple inheritance allows a derived class to inherit from two or more base classes.
    2. Advantages:

      • Code Reusability: The derived class can reuse code from multiple base classes.
      • Flexibility: Multiple inheritance allows you to combine different behaviors or attributes from various classes.
    3. Challenges:

      • Ambiguity Problem (Diamond Problem): When two base classes have members with the same name, ambiguity arises in the derived class. This can be resolved using virtual inheritance.
      • Constructors: Each base class's constructor must be called explicitly in the derived class, as constructors are not inherited.
    4. Virtual Inheritance:

      • To avoid ambiguity in multiple inheritance, C++ allows virtual inheritance. This ensures that base classes are only inherited once, solving the diamond problem.

    Multiple inheritance is a powerful feature, but it must be used carefully, especially in complex systems where the potential for ambiguity and confusion is higher.

    ← Previous topic 14
    Inheritance
    Next topic 16 →
    Polymorphism

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    7mremaining
    1.1kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Object Oriented Programming