Multiplication Example using Hybrid Inheritance in C++


The program defines four classes, A, B, C, and D. Class A has a protected data member a and a public method get_a() that prompts the user to enter a value for a. Class B publicly inherits from A and has a protected data member b and a public method get_b() that prompts the user to enter a value for b. Class C has a protected data member c and a public method get_c() that prompts the user to enter a value for c.

Class D publicly inherits from both B and C and has a protected data member d. It also defines a public method mul() that calls the get_a(), get_b(), and get_c() methods to prompt the user for values for a, b, and c. It then calculates and prints the product of a, b, and c. In the main() function, an object of class D is created and its mul() method is called to perform the multiplication operation.

Source Code

#include<iostream>
using namespace std;
class A
{
  protected:
    int a;
  public:
    void get_a()
    {
      std::cout<<"Enter the value of 'a' : "<<std::endl;
      cin>>a;
    }
};
class B : public A
{
  protected:
    int b;
  public:
    void get_b()
    {
      std::cout<<"Enter the value of 'b' : "<<std::endl;
      cin>>b;
    }
};
class C
{
   protected:
    int c;
   public:
    void get_c()
    {
      std::cout<<"Enter the value of c is : "<<std::endl;
      cin>>c;
    }
};
class D : public B, public C
{
  protected:
    int d;
  public:
    void mul()
    {
       get_a();
       get_b();
       get_c();
       std::cout<<"Multiplication of a,b,c is : "<<a*b*c<<std::endl;
    }
};
int main()
{
  D d;
  d.mul();
  return 0;
}
To download raw file Click Here

Output

Enter the value of 'a' : 
20
Enter the value of 'b' : 
30
Enter the value of c is : 
20
Multiplication of a,b,c is : 12000

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples