Multilevel
Inheritance
In
multilevel inheritance a derived class itself acts as a base class to derive
another class.
#include <iostream>
using namespace std;
class student //base class
{
private :
char name[20];
int rno;
public:
void acceptname()
{
cout<<"\n Enter roll no and name .. ";
cin>>rno>>name;
}
}
void displayname()
{
cout<<"\n Roll no :-"<<rno;
cout<<"\n Name :-"<<name<<endl;
}};
class exam : public student //derived class with single base
class
{
public:
int mark1, mark2
,mark3,mark4,mark5,mark6;
void acceptmark()
{
cout<<"\n
Enter lang,eng,phy,che,csc,mat marks.. ";
cin>>mark1>>mark2>>mark3>>mark4>>mark5>>mark6;
}
void displaymark(){
cout<<"\n\t\t Marks Obtained ";
cout<<"\n Language... "<<mark1;
cout<<"\n English... "<<mark2;
cout<<"\n Physics... "<<mark3;
cout<<"\n Chemistry... "<<mark4;
cout<<"\n Comp.sci... "<<mark5;
cout<<"\n Maths...
"<<mark6;
}
};
class result : public exam
{
int total;
public:
void showresult()
{
total=mark1+mark2+mark3+mark4+mark5+mark6;
cout<<"\nTOTAL
MARK SCORED : "<<total;
}
};
int main()
{
result r1;
r1.acceptname(); //calling
base class function using derived class object
r1.acceptmark(); //calling
base class function which itself is a derived
// class function using its derived class object
r1.displayname(); //calling
base class function using derived class
//object
r1.displaymark(); //calling
base class function which itself is a derived
//class function using its derived class object
r1.showresult(); //calling
the child class function
return 0;
}
Enter roll no and name .. 1201 SARATHI
Enter lang,eng,phy,che,csc,mat marks.. 96 98 100 100 100 100
Roll no :-1201
Name :-SARATHI
Marks Obtained
Language... 96
English... 98
Physics... 100
Chemistry... 100
Comp.sci... 100
Maths... 100
TOTAL MARK SCORED :
594
In
the above program class “result “ is derived from class “exam” which itself is
derived from class student.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.