Multiple Inheritance In C++

Multiple Inheritances In C++ is the process of deriving a new class that inherits the attributes from two or more classes.

Multiple Inheritance In C
Multiple – Inheritance In C++

Syntax of the Derived class:

class D : visibility B-1, visibility B-2, ?  
{  
    // Body of the class;  
}   

Let’s look at a simple multiple inheritance scenario.

#include <iostream>  
using namespace std;  
class A  
{  
    protected:  
     int a;  
    public:  
    void get_a(int n)  
    {  
        a = n;  
    }  
};  
  
class B  
{  
    protected:  
    int b;  
    public:  
    void get_b(int n)  
    {  
        b = n;  
    }  
};  
class C : public A,public B  
{  
   public:  
    void display()  
    {  
        std::cout << "The value of a is : " <<a<< std::endl;  
        std::cout << "The value of b is : " <<b<< std::endl;  
        cout<<"Addition of a and b is : "<<a+b;  
    }  
};  
int main()  
{  
   C c;  
   c.get_a(10);  
   c.get_b(20);  
   c.display();  
  
    return 0;  
}  

Output:

The value of a is : 10
The value of b is : 20
Addition of a and b is : 30

In the example above, class ‘C’ inherits two base classes in public mode: ‘A’ and ‘B.’

Join Our Community

Join our WhatsApp Group To know more About Programming Language tips, tricks and knowledge about programming and how to start learning any programming language.

Default image
Lingaraj Senapati
Hey There! I am Lingaraj Senapati, the Co-founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.
Articles: 217

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply