Monday, April 27, 2009

Multilevel Inheritance


Multilevel Inheritance
A new class is derived from another derived class is called multilevel inheritance


Class C is derived from class B, and Class B is derived from class A, here Class B is called derived as well as base class for class C it is base class, for class A it is derived class. Class C is called derived class. Using derived class object, we ca access the members of both base derived and two base classes. But using Base class object we can’t access members of derived class.

Example:
Header files
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.: “<cout<<”Name : “<cout<
}
class mark: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< cout< cout< cout< cout< cout< cout<<“Total =“< cout<<“Average =“<}
class result:public mark
{
public:
void check();
};
void result::check()
{
if(avg>=90)
{
cout<”Distinction”;
}
else if(avg<=80 && avg>=60)
{
cout<<”I Class”;
}
else if(avg<60>=40)
{
cout<<”II Class”;
}
else
cout<<”Fail”;
}
void main()
{
clrscr();
result obj1;
obj1.get();
obj1.getmark();
obj1.diaplay();
obj1.displaymark();
obj1.check();
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
80
75
80
90
100
Total = 425
Average = 85
I Class

No comments:

Post a Comment