C++ Inheritance | Find output programs | Set 5

This section contains the C++ find output programs with their explanations on C++ Inheritance (set 5).
Submitted by Nidhi, on July 05, 2020

Program 1:

#include <iostream>
#include <string.h>
using namespace std;

class Fact {
public:
    int factorial(int num)
    {
        int f = 1;

        for (int i = 2; i <= num; i++) {
            f = f * i;
        }
        return f;
    }
};

class Power {
public:
    int pow(int num, int p)
    {
        int c = 1;

        for (int i = 1; i <= p; i++) {
            c = c * num;
        }
        return c;
    }
};

class Sample : Fact, Power {
public:
    void calculate()
    {
        int A = 2, B = 3, NUM = 5;
        int RES = 0;

        RES = pow(A, B) * factorial(NUM);

        cout << RES << endl;
    }
};

int main()
{
    Sample S;
    S.calculate();
    return 0;
}

Output:

960

Explanation:

Here, we created three classes Fact, Power, and Sample. The Fact class contains a member function to calculate factorial of specified number; Power class contains a member function to calculate the power of a number.

Both Fact and Power class is inherited by Sample class and consumed the member functions of Both parent class.

In the main() function, we created the object of Sample class and call calculate() function that solves the mathematical expression using factorial() and power() function and prints "960" on the console screen.

Program 2:

#include <iostream>
#include <string.h>
using namespace std;

class Fact {
public:
    int factorial(int num)
    {
        int f = 1;

        for (int i = 2; i <= num; i++) {
            f = f * i;
        }
        return f;
    }
};

class Power {
public:
    int pow(int num, int p)
    {
        int c = 1;

        for (int i = 1; i <= p; i++) {
            c = c * num;
        }
        return c;
    }
};

class Sample : Fact, Power {
public:
    Sample()
    {
        cout << "Result: ";
    }
    void calculate()
    {
        int A = 2, B = 3, NUM = 5;
        int RES = 0;

        RES = pow(A, B) * factorial(NUM);

        cout << RES << endl;
    }
};

int main()
{
    Sample* S;

    S->calculate();

    return 0;
}

Output:

960

Explanation:

Here, we created three classes Fact, Power, and Sample. The Fact class contains a member function to calculate factorial of specified number; Power class contains a member function to calculate the power of a number.

Both Fact and Power class is inherited by Sample class and consumed the member functions of Both parent class.

In the main() function, we created the pointer of Sample class and call calculate() function that solves the mathematical expression using factorial() and power() function and prints "960" on the console screen.

Here, we used pointer without initializing it, so it will not call the constructor of the class, and also it is not the proper way. We must assign the address of the object to the pointer.

Program 3:

#include <iostream>
#include <string.h>
using namespace std;

class Fact {
public:
    int factorial(int num)
    {
        int f = 1;

        for (int i = 2; i <= num; i++) {
            f = f * i;
        }
        return f;
    }
};

class Power {
public:
    int pow(int num, int p)
    {
        int c = 1;

        for (int i = 1; i <= p; i++) {
            c = c * num;
        }
        return c;
    }
};

class Sample {
public:
    void calculate()
    {
        int A = 2, B = 3, NUM = 5;
        int RES = 0;

        Fact F;
        Power P;

        RES = P.pow(A, B) * F.factorial(NUM);

        cout << RES << endl;
    }
};

int main()
{
    Sample S;
    S.calculate();
    return 0;
}

Output:

960

Explanation:

Here, we created three classes Fact, Power, and Sample. The Fact class contains a member function to calculate factorial of specified number; Power class contains a member function to calculate the power of a number.

Here, we created the object of class Fact and Power inside the calculate() member function and calculate the mathematical expression and print the result on the console screen.

In the main() function, we created the object of Sample class and call calculate() function that print "960" on the console screen.






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.