+1 vote
46 views

the class named GrandFather is the top-level-base-class and it is inherited by intermediate-base-class Father, which is in turn is inherited by the class Son, by performing multilevel inheritance. Father and Mother are inherited by multiple inheritance in Son.

Besides this, the class Son also inherits another class Mother, by performing multiple inheritance (as class Son is inheriting from both Father and Mother classes at the same time), which means that -

The privateand members of class GrandFather, Father and Mother are inherited classes, such as their properties - height, eye_color, hair_color.

The publicgetters and setters functions are inherited and have become the public members in the derived class, Son.

The protected member of class Mother is also inherited as protected member in the derived class, Son.

Grandfather 1 Name (Ali) Height, Eye color, Face color

Grandfather 2 Name (Asad) Height, Eye color, Face color

Father Name (Raza Ali) Height, Eye color, Face color

Mother Name (Rabia Asad) Height, Eye color, Face color

Son Name (Ahsan Raza) Height, Eye color, Face color

by (140 points)  
edited by | 46 views

1 Answer

+2 votes

Hi Aqsa, Here is the solution to your Problem Statement. Please change it according to your needs!

#include<iostream>
using namespace std;
class GrandFather{
    private:
        string name;
        float height;
        string eye_color, hair_color;
    public:
        GrandFather(){
        } //Default Constructor
        GrandFather(string n, float h, string e_c, string h_c){
            name = n;
            height = h;
            eye_color = e_c;
            hair_color = h_c;
        }
        //Setter Functions
        void setName(string n){
            name = n;
        }
        void setHeight(float h){
            height = h;
        }
        void setEyeColor(string e_c){
            eye_color = e_c;
        }
        void setHairColor(string h_c){
            hair_color = h_c;
        }

        //Getter Functions
        string getName(){
            return name;
        }
        float getHeight(){
            return height;
        }
        string getEyeColor(){
            return eye_color;
        }
        string getHairColor(){
            return hair_color;
        }

        //Function to show Data
        void showData(){
            cout<<"------------------------\n";
            cout<<"I am Grandfather (object): "<<endl;
            cout<<"Name: "<<name<<endl;
            cout<<"Height: "<<height<<endl;
            cout<<"Eye Color: "<<eye_color<<endl;
            cout<<"Hair Color: "<<hair_color<<endl;
            cout<<"------------------------\n";

        }
};

class Father : public GrandFather{
    public:
    Father(){
    } //Default Constructor
    Father(string n, float h, string e_c, string h_c){
            setName(n);
            setHeight(h);
            setEyeColor(e_c);
            setHairColor(h_c);
        }
    //Override the function showData()
    //Function to show Data
        void showData(){
            cout<<"------------------------\n";
            cout<<"I am Father (object): "<<endl;
            cout<<"Name: "<< getName() <<endl;
            cout<<"Height: "<< getHeight() <<endl;
            cout<<"Eye Color: "<< getEyeColor() <<endl;
            cout<<"Hair Color: "<< getHairColor() <<endl;
            cout<<"------------------------\n";

        }
};

class Mother{
    protected:
        string name;
        float height;
        string eye_color, hair_color;
    public:
        Mother(){
    } //Default Constructor
        Mother(string n, float h, string e_c, string h_c){
            name = n;
            height = h;
            eye_color = e_c;
            hair_color = h_c;
        }
    //Function to show Data
        void showData(){
            cout<<"------------------------\n";
            cout<<"I am Mother (object): "<<endl;
            cout<<"Name: "<< name <<endl;
            cout<<"Height: "<< height <<endl;
            cout<<"Eye Color: "<< eye_color <<endl;
            cout<<"Hair Color: "<< hair_color <<endl;
            cout<<"------------------------\n";

        }
};


class Son : public Father, protected Mother{
    public:
        Son(string n, float h, string e_c, string h_c){
            setName(n);
            setHeight(h);
            setEyeColor(e_c);
            setHairColor(h_c);
        }
    //Override the function showData()
    //Function to show Data
        void showData(){
            cout<<"------------------------\n";
            cout<<"I am Son (object) inhertied by Father and Mother: "<<endl;
            cout<<"Name: "<< getName() <<endl;
            cout<<"Height: "<< getHeight() <<endl;
            cout<<"Eye Color: "<< getEyeColor() <<endl;
            cout<<"Hair Color: "<< getHairColor() <<endl;
            cout<<"------------------------\n";

        }
};

main(){
    GrandFather gf("Ali", 5.4, "Black", "Light Black");
    GrandFather gf2("Asad", 5.5, "Black", "White");

    Father f("Raza", 5.6, "Black", "Light Black");
    Mother m("Rabia Asad", 5.2, "Brown", "White");

    Son s("Ahsan Raza", 5.68, "Dark Brown", "Dark White");

    //Show Data from objects
    gf.showData();
    gf2.showData();

    f.showData();
//Program by M.Athar
m.showData();

    s.showData();

    cout<<"Program solved by M.Athar on meansflow.com\n Contact me for Software Customization!";
}
by (3.3k points)  
Thank you so, much dear!
You are most welcome Dear!

Related questions

+2 votes
1 answer
asked Jun 5 by toheed (1.4k points) | 17 views
+2 votes
1 answer
+1 vote
2 answers
+2 votes
3 answers
+1 vote
0 answers
asked Jun 5 by toheed (1.4k points) | 15 views
+2 votes
1 answer
+2 votes
2 answers
asked Jun 4 by toheed (1.4k points) | 93 views
+2 votes
0 answers
asked Jun 12 by toheed (1.4k points) | 21 views
+1 vote
1 answer
asked Jun 27 by fatyma Nauman (555 points) | 44 views




Welcome to Meansflow - Where Developers and Students Learn, Share, & Build Careers, where you can ask questions and receive answers from other members of the community.
118 questions
90 answers
43 comments
111 users