Swiftorial Logo
Home
Swift Lessons
AI Tools
Learn More
Career
Resources

Types of Inheritance in C++

1. Single Inheritance

Single inheritance is the simplest type of inheritance in C++. In single inheritance, a class (child class) inherits from another class (base class) directly.

Example:

class Base {
public:
    void show() {
        cout << "Base class show function" << endl;
    }
};

class Derived : public Base {
public:
    void display() {
        cout << "Derived class display function" << endl;
    }
};

int main() {
    Derived obj;
    obj.show();
    obj.display();
    return 0;
}
                    
Output:
Base class show function
Derived class display function
                    

2. Multiple Inheritance

In multiple inheritance, a class can inherit from more than one base class. This can be useful when a class needs to inherit properties and behaviors from multiple sources.

Example:

class Base1 {
public:
    void show() {
        cout << "Base1 class show function" << endl;
    }
};

class Base2 {
public:
    void display() {
        cout << "Base2 class display function" << endl;
    }
};

class Derived : public Base1, public Base2 {
};

int main() {
    Derived obj;
    obj.show();
    obj.display();
    return 0;
}
                    
Output:
Base1 class show function
Base2 class display function
                    

3. Hierarchical Inheritance

In hierarchical inheritance, multiple derived classes inherit from a single base class. This allows the child classes to share a common interface or implementation.

Example:

class Base {
public:
    void show() {
        cout << "Base class show function" << endl;
    }
};

class Derived1 : public Base {
};

class Derived2 : public Base {
};

int main() {
    Derived1 obj1;
    Derived2 obj2;
    obj1.show();
    obj2.show();
    return 0;
}
                    
Output:
Base class show function
Base class show function
                    

4. Multilevel Inheritance

Multilevel inheritance involves a class being derived from another derived class, forming a chain of inheritance.

Example:

class Base {
public:
    void show() {
        cout << "Base class show function" << endl;
    }
};

class Derived1 : public Base {
};

class Derived2 : public Derived1 {
};

int main() {
    Derived2 obj;
    obj.show();
    return 0;
}
                    
Output:
Base class show function
                    

5. Hybrid Inheritance

Hybrid inheritance is a combination of two or more types of inheritance. It can be a mix of single, multiple, hierarchical, or multilevel inheritance.

Example:

class Base {
public:
    void show() {
        cout << "Base class show function" << endl;
    }
};

class Derived1 : public Base {
};

class Derived2 : public Base {
};

class Derived3 : public Derived1, public Derived2 {
};

int main() {
    Derived3 obj;
    obj.show();
    return 0;
}
                    
Output:
error: request for member 'show' is ambiguous
                    

Note: In the above example, there is an ambiguity error because the member function show() is inherited from two base classes. This can be resolved using the scope resolution operator or virtual inheritance.