Q:

C++ program to demonstrate example of multiple inheritance

belongs to collection: C++ Inheritance programs/examples

0

C++ program to demonstrate example of multiple inheritance

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Multiple inheritance program in C++

/*C++ program to demonstrate example of multiple inheritance.*/
#include <iostream>
using namespace std;
 
//Base Class - class A
class A
{
    private:
        int a;
    public:
        void get_a(int val_a){
            a=val_a;
        }
        void put_a(void){
            cout <<"value of a: " << a << endl;
        }
};
 
//Base Class - class B
class B
{
    private:
        int b;
    public:
        void get_b(int val_b){
            b=val_b;
        }
        void put_b(void){
            cout <<"value of b: " << b << endl;
        }
};
 
//Base Class - class C
class C
{
    private:
        int c;
    public:
        void get_c(int val_c){
            c=val_c;
        }
        void put_c(void){
            cout <<"value of c: " << c << endl;
        }
};
 
//multiple inheritance, Derived Class - class final
class final:public A,public B,public C
{
    public:
        void printValues(void)
        {
            //now, we can call member functions of class A,B,C
            put_a();
            put_b();
            put_c();
        }
};
 
int main()
{
    //create object of final (derived class)
    final objFinal;
     
    /*read values of a,b and c - which are the private members
      of class A,B and C respectively using public member functions
      get_a(), get_b() and get_c(), by calling with the object of 
      final class.*/
       
    objFinal.get_a(100);
    objFinal.get_b(200);
    objFinal.get_c(300);
     
    //print all values using final::printValues()
    objFinal.printValues();
     
    return 0;
}

Output

    value of a: 100
    value of b: 200
    value of c: 300

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ program to demonstrate example of hierarchical... >>
<< C++ program to read and print employee information...