×

C++ Programs

C++ Most popular & Searched Programs

C++ Basic I/O Programs

C++ Constructor & Destructor Programs

C++ Manipulators Programs

C++ Inheritance Programs

C++ Operator Overloading Programs

C++ File Handling Programs

C++ Bit Manipulation Programs

C++ Classes & Object Programs

C++ program to demonstrate example of multiple inheritance

Learn about the multiple inheritance in C++, and implement a C++ program using the multiple inheritance.
[Last updated : March 02, 2023]

Multiple Inheritance in C++

In C++, the multiple inheritance is defined as the inheritance in which a derived class inherits more than one base classes.

This program will demonstrate example of multiple inheritance in c++ programming language.

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

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.