To write a program in C++ for, Hierarchical Inheritance - WAY2IT

Latest Post :

To write a program in C++ for, Hierarchical Inheritance



Code :-

#include<iostream.h>
#include<conio.h>
#include<string.h>
class student
{
int id;
char name[20];
public:
void getDetails(int i,char nm[])
{
id=i;
strcpy(name,nm);
}
void dispDetails()
{
cout<<"\nId :"<<id;
cout<<"\nName :"<<name;
}
};
class EnggStudent:public student
{
int sub1,sub2,sub3,res;
public:
void getMarks(int s1,int s2,int s3)

{
sub1=s1;
sub2=s2;
sub3=s3;
}
char DisplayResult()
{
res=(sub1+sub2+sub3)/3;
if(res>=50)
return 'P';
else
return 'F';
}
};
class ScienceStudent:public student
{
int sub1,sub2,sub3,sub4,res;
public:
void getMarks(int s1,int s2,int s3,int s4)
{
sub1=s1;
sub2=s2;
sub3=s3;
sub4=s4;
}
char DisplayResult()
{
res=(sub1+sub2+sub3+sub4)/4;
if(res>=50)
return 'P';
else
return 'F';
}
};
void main()
{
clrscr();
EnggStudent E1;
E1.getDetails(11,"Meena");
E1.dispDetails();
E1.getMarks(78,78,97);
char R;
R=E1.DisplayResult();
if(R=='P')
cout<<"\tPassed";
else if(R=='F')
cout<<"\tFail ";
else
cout<<"\tAbsent";
ScienceStudent S1;
S1.getDetails(12,"Vani");
S1.dispDetails();
S1.getMarks(78,68,77,81);
char R1;
R1=S1.DisplayResult();
if(R1=='P')
cout<<"\tPassed";
else if(R1=='F')
cout<<"\tFail ";
else
cout<<"\tAbsent";
getch();
}



Output :-