Product and Average of two Integer and Double numbers using Hybrid Inheritance with function overloading
In this program, we are going to find Product and Average of two Integer and Double numbers using Hybrid Inheritance with function overloading 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 addition(int num1, int num2)
    {
        return num1 + num2;
    }
    double addition(double n1, double n2)
    {
        return n1 + n2;
    }
};

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

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

// second sub class
class PrintPrdt : public Prdt
{
    public:
    int a, b;
    double c, d;
    void print()
    {
        cout << "\nProduct of the 2 integer numbers = " << Prdt::product(a, b);
        cout << "\nProduct of the 2 double numbers = " << Prdt::product(c, d);
    }
};

// main function
void main()
{
    clrscr();
    PrintPrdt obj1;
    Average obj2;
    cout << "Enter 2 integer numbers: ";
    cin >> obj1.a >> obj1.b;
    cout << "Enter 2 double numbers: ";
    cin >> obj1.c >> obj1.d;
    obj1.print();
    obj2.a = obj1.a;
    obj2.b = obj1.b;
    obj2.c = obj1.c;
    obj2.d = obj1.d;
    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 addition(int num1, int num2)
    {
        return num1 + num2;
    }
    double addition(double n1, double n2)
    {
        return n1 + n2;
    }
};

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

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

// second sub class
class PrintPrdt : public Prdt
{
    public:
    int a, b;
    double c, d;
    void print()
    {
        cout << "\nProduct of the 2 integer numbers = " << Prdt::product(a, b);
        cout << "\nProduct of the 2 double numbers = " << Prdt::product(c, d);
    }
};

// main function
int main()
{
    PrintPrdt obj1;
    Average obj2;
    cout << "Enter 2 integer numbers: ";
    cin >> obj1.a >> obj1.b;
    cout << "Enter 2 double numbers: ";
    cin >> obj1.c >> obj1.d;
    obj1.print();
    obj2.a = obj1.a;
    obj2.b = obj1.b;
    obj2.c = obj1.c;
    obj2.d = obj1.d;
    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