//Sample Program to demonstrate Multilevel Inheritance
# include < iostream.h >
# include < string.h >
#include < conio.h >
class student {
private: int id; char name[20];
public: student(){
id = 0; strcpy(name,"\0"); }
student(int i, char *n)
{
id = i;
strcpy(name,n);
}
void getdata()
{
cout << "\n Enter Roll Number: ";
cin >> id;
cout <<"\n Enter Name: ";
cin >> name;
}
void dispdata()
{
cout << "\n Roll Number= " << id << "\n Name= " << name;}

};


class marks: public student	{
protected: int mks[3];
public:
marks()
{
mks[0]=mks[1]=mks[2]=0;
}
marks(int i,char *n,int a,int b,int c): student(i,n)
{
mks[0]=a; mks[1]=b; mks[2]=c;
}
void getdata() {
student::getdata();
int i;
for(i=0;i<3;i++)
{
cout << "\n Enter marks for subject " << i+1 <<" -> ";
cin >> mks[i];}
}
void dispdata()
{
student::dispdata();
for(int i=0;i < 3;i++)
{
cout << "\n Marks in subject " << i+1;
cout << " is " << mks[i];}
}
};
class report: public marks
//second level of Inheritance
{
private: int tot;
public: report() {tot=0;}
report(int i,char *n,int a,int b,int c):
marks(i,n,a,b,c) { }
void getdata()
{
marks::getdata();
}
void dispdata()
{
float avg;
tot=mks[0]+mks[1]+mks[2];
avg=tot/3.0;
marks::dispdata();
cout << "\n Total="<< tot;
cout << "\n Average = " << avg << endl;
}
};
void main()
{
clrscr();
report r1(9701,"Poornima",76,87,90);
cout << "\n Student Details are: \n";
r1.dispdata();
r1.getdata();
cout << "\n Student Details are: \n";
r1.dispdata();
getch();
}
Click here to go back.

Our aim is to provide information to the knowledge seekers. 


comments powered by Disqus




Footer1