Answer to Question #252549 in C++ for Jagan

Question #252549
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. 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. display the name of grandfather, father and child.
1
Expert's answer
2021-10-17T14:00:25-0400

Source code

#include <iostream>


using namespace std;
class grandfather{
    private:
        string name;
    public:
        grandfather(string n){
            name=n;
        }
    
};


class father: public grandfather{
    private:
        string name;
    public:
        father(string n,string n2): grandfather(n){
            name=n2;
        }
};


class child: public father{
    private:
        string c_name;
        string f_name;
        string g_name;
    public:
        child(string n,string n2, string n3):father(n,n2){
            g_name=n;
            f_name=n2;
            c_name=n3;
            
        }
        void display(){
            cout<<"\nName of grandfather: "<<g_name;
            cout<<"\nName of father: "<<f_name;
            cout<<"\nName of child: "<<c_name;
        }
};


int main()
{
    child c("John","Mark","Smith");
    c.display();


    return 0;
}


Output


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