Q:

C++ Inheritance | Find output programs | Set 5

belongs to collection: C++ find output programs

0

This section contains the C++ find output programs with their explanations on C++ Inheritance (set 5).

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;
}

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;
}

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;
}

All Answers

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

Answer Program 1:

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.

Answer Program 2:

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.

Answer Program 3:

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.

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

total answers (1)

C++ find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ Exceptional Handling | Find output programs | ... >>
<< C++ Inheritance | Find output programs | Set 4...