Write a C++ program to illustrate hybrid inheritance concept using student database creation as an example.
Program Code:
Step 1: Create a class with some methods and variables.
Step 2: Create a new class that extends this class.
Step 3: Create a new class with some new methods.
Step 4: Create a new class again which inherits from classes defined in step 2 and step 3.
Step 5.Create an object of the class defined in step 4 and Create it call methods defined in step 1 and step 3.
Program Code:
#include<iostream.h>
class tests;
class sports;
class result;
class Student
{
protected :int rn;
public:
void get()
{cout<<"\n\nEnter roll no.:";
cin>>rn;
}
void put()
{
cout<<"\nRoll no.:"<<rn<<endl;
}
};
class tests:public Student
{
protected:
float sub1,sub2;
public:
void get_m()
{
cout<<"\nenter marks:";
cin>>sub1>>sub2;
}
void put_m()
{
cout<<"\nMarks in subject 1="<<sub1<<endl;
cout<<"\nMarks in subject 2="<<sub2<<endl;
}
};
class sports
{
protected:
char g;
public:
void get_g()
{
cout<<"Enter the grade:";
cin>>g;
}
void put_g()
{
cout<<"\nGrade in sports:"<<g<<endl;
}
};
class result:private tests,private sports
{
public:
void res()
{
get();
get_m();
get_g();
cout<<"\n------\nRESULT\n------\n";
put();
put_m();
put_g();
float total=sub1+sub2;
cout<<"TOTAL="<<total;
}
};
void main()
{
result s1;
s1.res();
}
Sample Inputs and Outputs:
Enter roll no.:100
Enter Marks: 89 90
Enter the Grade: A
Result
roll no.:100
Marks in subject 1=89
Marks in subject 2=90
Grade in sports: A
Total= 179
Post A Comment:
0 comments: