Apply two or more types of inheritance to design a program is called Hybrid Inheritance.
Example of hybrid inheritance
#include <iostream>
using namespace std;
class student
{
protected:
int roll_no;
public:
void get_no(int a)
{
roll_no=a;
}
void put_no(void)
{
cout << “Roll No:”<<roll_no<<”\n”;
}
};
class test : public student
{
protected:
float part1,part2;
public:
void get_marks(float x,float y)
{
part1=x; part2=y;
}
void put_marks(void)
{
cout << “Marks obtained:” << “\n”
<< “Part1= “ <<part1<<”\n”
<<”Part2= “<<part2<<”\n”;
}
};
class sports
{
protected:
float score;
public:
void get_score(float s)
{
score=s;
}
void put_score(void)
{
cout << “Sports wt:” <<score<<”\n\n”;
}
};
class result : public test, public sports
{
float total;
public:
void display(void);
};
void result : : display(void)
{
total=part1 + part2 +score;
put_no();
put_marks();
put_score();
cout<<”Total score: “<<total<< “\n”;
}
int main()
{
result stud;
stud.get_no(1223);
stud.get_marks(27.5, 33.0);
stud.get_score(6.0);
stud.display();
return 0;
} |