Calculate Square of Sum of two numbers using Single Inheritance || Single Inheritance || C++

In this, we are going to see a program to Calculate Square of Sum of two numbers using Single 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;
    void getA()
    {
        cout << "Enter 1st Number: ";
        cin >> a;
    }
    void getB()
    {
        cout << "Enter 2nd Number: ";
        cin >> b;
    }
};

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

void main()
{
    clrscr();
    SumSq obj;
    obj.getA();
    obj.getB();
    cout << "Square of sum of the 2 numbers = " << obj.sq();
    getch();
}

//...........Coded by Shreya Idate

 

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

#include <iostream>
using namespace std;

class Get
{
    public:
    int a, b;
    void getA()
    {
        cout << "Enter 1st Number: ";
        cin >> a;
    }
    void getB()
    {
        cout << "Enter 2nd Number: ";
        cin >> b;
    }
};

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

int main()
{
    SumSq obj;
    obj.getA();
    obj.getB();
    //obj.sum();
    cout << "Square of sum of the 2 numbers = " << obj.sq();
    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