Advertisement
Advertisement
//C++ Example of Hybrid Inheritance
#include<iostream>
#include<string>
using namespace std;
class GrandFather
{
private:
int height;
public:
void set_height(int);
int get_height();
};
//class Father is inheriting class GrandFather
class Father : public GrandFather
{
private:
string hair_color;
public:
string get_hair_color();
void set_hair_color(string);
};
class Mother
{
protected:
string eye_color;
public:
string get_eye_color();
void set_eye_color(string);
};
//Defining set_height() function of GrandFather class
void GrandFather :: set_height(int h)
{
height = h;
}
//Defining get_height() function of GrandFather class
int GrandFather :: get_height()
{
return height;
}
//Defining set_hair_color() function of Father class
void Father :: set_hair_color(string color)
{
hair_color = color;
}
//Defining get_hair_color() function of Father class
string Father :: get_hair_color()
{
return hair_color;
}
//Defining set_eye_color() function of Mother class
void Mother :: set_eye_color(string color)
{
eye_color = color;
}
//Defining get_eye_color() function of Mother class
string Mother :: get_eye_color()
{
return eye_color;
}
//class Son is inheriting from Father(inherited from GrandFather) and from Mother too(mix of multilevel and multiple inheritance)
class Son : public Father, public Mother
{
};
int main()
{
Son ob;
ob.set_height(175);
ob.set_hair_color("brown");
ob.set_eye_color("black");
cout<<"The color of eyes of son is : " << ob.get_eyes_color() <<"\n";
cout<<"The hair color of son is : " << ob.get_hair_color() << "\n";
cout<<"The height of son is : " << ob.get_height() <<"\n";
}
The color of eyes of son is : black
The hair color of son is : brown
The height of son is : 175
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement