Hybrid Inheritance
Combination of hierarchical and multiple inheritance is called hybrid inheritance.
In this lass D is derived from class B and Class C, Using class D object we can access all the members (A,B,C). But when we accessing Class A members we have two ways in this case we have an ambiguity, i.e. in which way we are getting the properties of class A, so here we implementing virtual keyword to avoid the ambiguity.
Example:
#include <>
#include <>
class student
{
int rno;
char name[50];
char address[30];
public:
void get();
void display();
};
void student::get()
{
cout<<”Enter the Student Roll Number:”; cin>> rno;
cout<<”Enter the student Name :”; cin>> name;
cout<<”Enter the Student Address:”; cin>> address;
}
void student::display()
{
cout<<”Roll No.: “<<>class mark:virtual public student
{
int mark1, mark2, mark3, mark4, mark5;
float avg;
public:
void getmark();
void displaymark();
};
void mark::getmark()
{
cout<<”Enter the five subject Marks one by one”<<”\n”; cin>>mark1>>mark2>>mark3>>mark4>>mark5;
total=mark1+ mark2+ mark3+ mark4+ mark;
avg=total/5;
}
void mark::displaymarks()
{
cout<< total ="“<<" average ="“<<">class sports : public virtual student
{
Int points;
Public:
Void getsp();
Void displaysp();
};
Void sports::getsp()
{
Cout<<”Enter the Sports Points”; Cin>> points;
}
Void sports::displaysp()
{
Cout<<>class result: public mark, public sports
{
public:
void check();
};
void result::check()
{
if(avg > =90)
{
cout<”Distinction”; } else if(avg< =80 && avg> =60)
{
cout<<”I Class”; } else if(avg<> =40)
{
cout<<”II Class”; } else cout<<”Fail”; } void main() { clrscr(); result obj1; obj1.get(); obj1.getmark(); obj1.diaplay(); obj1.displaymark(); obj1.check(); obj1.displaysp(); getch(); } Output will be Enter the Student Roll Number: 101 Enter the student Name: Lak Enter the Student Address: Chennai Enter the five subject Marks one by one 80 75 80 90 100 Enter the Sports Points: 60 80 75 80 90 100 Total = 425 Average = 85 I Class 60
Combination of hierarchical and multiple inheritance is called hybrid inheritance.
In this lass D is derived from class B and Class C, Using class D object we can access all the members (A,B,C). But when we accessing Class A members we have two ways in this case we have an ambiguity, i.e. in which way we are getting the properties of class A, so here we implementing virtual keyword to avoid the ambiguity.
Example:
#include <>
#include <>
class student
{
int rno;
char name[50];
char address[30];
public:
void get();
void display();
};
void student::get()
{
cout<<”Enter the Student Roll Number:”; cin>> rno;
cout<<”Enter the student Name :”; cin>> name;
cout<<”Enter the Student Address:”; cin>> address;
}
void student::display()
{
cout<<”Roll No.: “<<>class mark:virtual public student
{
int mark1, mark2, mark3, mark4, mark5;
float avg;
public:
void getmark();
void displaymark();
};
void mark::getmark()
{
cout<<”Enter the five subject Marks one by one”<<”\n”; cin>>mark1>>mark2>>mark3>>mark4>>mark5;
total=mark1+ mark2+ mark3+ mark4+ mark;
avg=total/5;
}
void mark::displaymarks()
{
cout<< total ="“<<" average ="“<<">class sports : public virtual student
{
Int points;
Public:
Void getsp();
Void displaysp();
};
Void sports::getsp()
{
Cout<<”Enter the Sports Points”; Cin>> points;
}
Void sports::displaysp()
{
Cout<<>class result: public mark, public sports
{
public:
void check();
};
void result::check()
{
if(avg > =90)
{
cout<”Distinction”; } else if(avg< =80 && avg> =60)
{
cout<<”I Class”; } else if(avg<> =40)
{
cout<<”II Class”; } else cout<<”Fail”; } void main() { clrscr(); result obj1; obj1.get(); obj1.getmark(); obj1.diaplay(); obj1.displaymark(); obj1.check(); obj1.displaysp(); getch(); } Output will be Enter the Student Roll Number: 101 Enter the student Name: Lak Enter the Student Address: Chennai Enter the five subject Marks one by one 80 75 80 90 100 Enter the Sports Points: 60 80 75 80 90 100 Total = 425 Average = 85 I Class 60
No comments:
Post a Comment