Question #193718

Design a code for inheritance for that consider three classes namely grandfather, father and child. Each one of them has a character data type member. Each class contain a pair of constructor and destructors. The father is derived from the class grandfather. Similarly the class child is derived from class father. The class grandfather is a base class of the class father. The class father is a base class of the class child. The class child is derived from the class father. The class father is intermediate classes that act as a base class as well as derived class. Constructors are executed from the base class to the derived class and destructors are executed from the derived class to the base class.


Expert's answer

#include <iostream>


using namespace std;
//define the class Grandfather
class Grandfather{
    protected: 
        char x;
    public:
    //define the constructor
        Grandfather(){
            
        }
    //define the destructors
        ~Grandfather(){
            
        }
};
//define the father class
class Father: public Grandfather{
    protected:
        char y;
    public:
    //define the constructor
        Father(){
            
        }
    //define the destructors
        ~Father(){
            
        }
};


//define the child class
class Child:public Father{
    private:
        char z;
    public:
    //define the constructor
        Child(){
            
        }
    //define the destructors
        ~Child(){
            
        }
};
int main()
{
    Grandfather gf;
    Father f;
    Child cl;
    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!

LATEST TUTORIALS
APPROVED BY CLIENTS