Calculate Square of Sum of two numbers using Multiple Inheritance

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


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

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

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

class num2
{
    public:
    int n2;
    void getn2()
    {
        cout << "Enter the second number: ";
        cin >> n2;
    }
};

class calculate : public num1, public num2
{
    public:
    int s, sq;
    void calc()
    {
        s = n1 + n2;
        sq = s * s;
    }
};

void main()
{
    clrscr();
    calculate obj1;
    obj1.getn1();
    obj1.getn2();
    obj1.calc();
    cout << "Sum of numbers is: " << obj1.s << endl;
    cout << "Square of sum of both numbers is: " << obj1.sq << endl;
    getch();
}

//...........Coded by Ananya Vidyadharan
 

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

#include <iostream>
using namespace std;

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

class num2
{
    public:
    int n2;
    void getn2()
    {
        cout << "Enter the second number: ";
        cin >> n2;
    }
};

class calculate : public num1, public num2
{
    public:
    int s, sq;
    void calc()
    {
        s = n1 + n2;
        sq = s * s;
    }
};

int main()
{
    calculate obj1;
    obj1.getn1();
    obj1.getn2();
    obj1.calc();
    cout << "Sum of numbers is: " << obj1.s << endl;
    cout << "Square of sum of both numbers is: " << obj1.sq << endl;
    return 0;
}

//...........Coded by Ananya Vidyadharan
    

#ENJOY CODING

Post a Comment

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

Previous Post Next Post