Addition, Product and Average of Two numbers using Hybrid Inheritance
In this program, we are going to find Addition, Product and Average of Two numbers using Hybrid Inheritance in C++ Programming Language. 
 


The Code given below can be used in TURBO C++ Compilers: -

#include<iostream.h>
#include<conio.h>

// base class
class Add
{
    public:
    int num1, num2;
    int addition(int num1, int num2)
    {
        return num1 + num2;
    }
};

// base class
class Prdt
{
    public:
    int a, b;
    int product(int a, int b)
    {
        return a * b;
    }
};

// first sub class
class Average : public Add
{
    public:
    int c, d;
    void avg()
    {
        cout << "\nAverage of the 2 numbers = " << (float)Add::addition(c, d) / 2;
    }
};

// second sub class
class Print : public Add, public Prdt
{
    public:
    int e, f;
    void print()
    {
        cout << "Sum of the 2 numbers = " << Add::addition(e, f);
        cout << "\nProduct of the 2 numbers = " << Prdt::product(e, f);
    }
};

// main function
void main()
{
    clrscr();
    Print obj1;
    Average obj2;
    cout << "Enter 2 numbers: ";
    cin >> obj1.e >> obj1.f;
    obj1.print();
    obj2.c = obj1.e;
    obj2.d = obj1.f;
    obj2.avg();
    getch();
}

//.......Coded by SHREYA IDATE


The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
using namespace std;

// base class
class Add
{
    public:
    int num1, num2;
    int addition(int num1, int num2)
    {
        return num1 + num2;
    }
};

// base class
class Prdt
{
    public:
    int a, b;
    int product(int a, int b)
    {
        return a * b;
    }
};

// first sub class
class Average : public Add
{
    public:
    int c, d;
    void avg()
    {
        cout << "\nAverage of the 2 numbers = " << (float)Add::addition(c, d) / 2;
    }
};

// second sub class
class Print : public Add, public Prdt
{
    public:
    int e, f;
    void print()
    {
        cout << "Sum of the 2 numbers = " << Add::addition(e, f);
        cout << "\nProduct of the 2 numbers = " << Prdt::product(e, f);
    }
};

// main function
int main()
{
    Print obj1;
    Average obj2;
    cout << "Enter 2 numbers: ";
    cin >> obj1.e >> obj1.f;
    obj1.print();
    obj2.c = obj1.e;
    obj2.d = obj1.f;
    obj2.avg();
    return 0;
}

//.......Coded by SHREYA IDATE


#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post