Calculate Square of Sum of two numbers using Hybrid Inheritance
In this program, we are going to Calculate Square of Sum 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 num1
{
    public:
    int n1;
    void getN1()
    {
        cout<<"Enter the first number: ";
        cin >> n1;
    }
};

// base class
class num2
{
    public:
    int n2;
    void getN2()
    {
        cout<<"Enter the Second number: ";
        cin >> n2;
    }
};

// first sub class
class Sum : public num1, public num2
{
    public:
    int sum;
    void getSum()
    {
        sum = n1 + n2;
    }
};

// second sub class
class SumSquare : public Sum
{
    public:
    int sq;
    void getSquare()
    {
        sq = sum * sum;
    }
    void printSq()
    {
        cout << "Square of the sum of the 2 numbers = " << sq;
    }
};

// main function
void main()
{
    clrscr();
    SumSquare obj;
    obj.getN1();
    obj.getN2();
    obj.getSum();
    obj.getSquare();
    obj.printSq();
    getch();
}

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


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

#include <iostream>
using namespace std;

// base class
class num1
{
    public:
    int n1;
    void getN1()
    {
        cout<<"Enter the first number: ";
        cin >> n1;
    }
};

// base class
class num2
{
    public:
    int n2;
    void getN2()
    {
        cout<<"Enter the Second number: ";
        cin >> n2;
    }
};

// first sub class
class Sum : public num1, public num2
{
    public:
    int sum;
    void getSum()
    {
        sum = n1 + n2;
    }
};

// second sub class
class SumSquare : public Sum
{
    public:
    int sq;
    void getSquare()
    {
        sq = sum * sum;
    }
    void printSq()
    {
        cout << "Square of the sum of the 2 numbers = " << sq;
    }
};

// main function
int main()
{
    SumSquare obj;
    obj.getN1();
    obj.getN2();
    obj.getSum();
    obj.getSquare();
    obj.printSq();
    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