Saturday, 11 April 2015

Hybrid Inheritance


There could be situations where we need to apply two or more types of inheritance to design a program. For instance, refer the following figure.



From the figure derivation of student, test and Result, forms the multilevel inheritance and the derivation of test, sports and result, forms the multiple inheritance. So this type of inheritance is also called multiple and multilevel inheritance.


#include <iostream.h>
class student
{
protected:
int rno;
public:
void getnumber(int a)
{
rno = a;
}
void putnumber(void)
{
cout<<”Roll Number is “<<rno<<endl;
}
};
class test:public student
{
protected:
float p1, p2;
public:
void getmarks(float m, float n)
{
p1 = m; p2 = n;
}
void putmarks(void)
{
cout<<”Marks in two subjects”<<endl;
cout<<p1<<p2<<endl;
}
};

class sports
{
protected:
float score;
public:
void getscore(float s)
{
score = s;
}
void putscore(void)
{
cout<<”Sports “<<score<<endl;
}
};
class result : public test, public sports
{
float total;
public:
void display(void)
{
total = p1 + p2 + score;
putnumber();
putmarks();
putscore();
cout<<”Total score”<<total<<endl;
}
};
void main()
{
result student1;
student1.getnumber(123);
student1.getmarks(27.5,33.0);
student1.getscore(6.0);
student1.display();
}




No comments:

Post a Comment