Q:

C++ program to demonstrate example of hierarchical inheritance to get square and cube of a number

belongs to collection: C++ Inheritance programs/examples

0

C++ program to demonstrate example of hierarchical inheritance to get square and cube of a number

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Hierarchical inheritance to get square and cube of a number program in C++.

/*C++ program to demonstrate example of hierarchical inheritance 
to get square and cube of a number.*/

#include <iostream>
using namespace std;
 
class Number
{
    private:
        int num;
    public:
        void getNumber(void)
        {
            cout << "Enter an integer number: ";
            cin  >> num;
        }
        //to return num
        int returnNumber(void)
        { return num; }
};
 
//Base Class 1, to calculate square of a number
class Square:public Number
{
    public:
    int getSquare(void)
    {
        int num,sqr;
        num=returnNumber(); //get number from class Number
        sqr=num*num;
        return sqr;
    }
};
 
//Base Class 2, to calculate cube of a number
class Cube:public Number
{
    private:
         
    public:
    int getCube(void)
    {
        int num,cube;
        num=returnNumber(); //get number from class Number
        cube=num*num*num;
        return cube;
    }
};
int main()
{
        Square objS;
        Cube objC;
        int sqr,cube;
         
        objS.getNumber();
        sqr =objS.getSquare();
        cout << "Square of "<< objS.returnNumber() << " is: " << sqr  << endl;
         
        objC.getNumber();
        cube=objC.getCube();
        cout << "Cube   of "<< objS.returnNumber() << " is: " << cube << endl;
         
        return 0;
}

Output

Enter an integer number: 10
Square of 10 is: 100
Enter an integer number: 20
Cube   of 10 is: 8000

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ program to read and print employee information... >>
<< C++ program to demonstrate example of multiple inh...