Calculate Square of Sum of two numbers using Multilevel Inheritance
In this, we are going to see a program to calculate square of sum of two numbers using Multilevel Inheritance in C++ Programming Language.
 


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

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

class Get
{
    public:
    int a, b, res;
    void getA()
    {
        cout << "Enter value of a: ";
        cin >> a;
    }
    void getB()
    {
        cout << "Enter value of b: ";
        cin >> b;
    }
};

class SumSq : public Get
{
    public:
    int s;
    void sum()
    {
        s = a + b;
    }
    int sq()
    {
        sum();
        return s * s;
    }
};

class Result : public SumSq
{
    public:
    void res()
    {
        cout<< "Square of sum of the 2 numbers = " << sq();
    }
};

void main()
{
    clrscr();
    Result obj;
    obj.getA();
    obj.getB();
    obj.sq();
    obj.res();
    getch();
}

//...........Coded by Sahil Shaikh


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

#include <iostream>
using namespace std;

class Get
{
    public:
    int a, b, res;
    void getA()
    {
        cout << "Enter value of a: ";
        cin >> a;
    }
    void getB()
    {
        cout <<"Enter value of b: ";
        cin >> b;
    }
};

class SumSq : public Get
{
    public:
    int s;
    void sum()
    {
        s = a + b;
    }
    int sq()
    {
        sum();
        return s * s;
    }
};

class Result : public SumSq
{
    public:
    void res()
    {
        cout << "Square of sum of the 2 numbers = " << sq();
    }
};

int main()
{
    Result obj;
    obj.getA();
    obj.getB();
    obj.sq();
    obj.res();
    return 0;
}

//...........Coded by Sahil Shaikh


#ENJOY CODING

Post a Comment

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

Previous Post Next Post