Answer to Question #224226 in C++ for Shank

Question #224226
Write a program to define a class Base that will contain a protected integer data member and inherit this class in class called Derived. Override the display function of Base class and add a new member function in the Derived class so that it returns the factorial of the Base class member.
1
Expert's answer
2021-08-09T02:33:29-0400
#include<iostream>
using namespace std;
class Base{
    protected:
        int x=5;
    public:
        void display (){
            cout<<"x = "<<x<<endl;
        }
};
class Derived:public Base{
    public:
        int getFactorial(int a){
            int fact=1;
            for(int i=1;i<=a;i++){
                fact=fact*i;
        }
        return fact;
         }
     void display (){
        cout<<"Factorial of "<<x<<" is "<<
        getFactorial (x);
    }  
};
int main()
{
    Base b;
    b.display();
    Derived d;
    d.display();
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS