Multiple
Inheritance
Program
to illustrate Multiple inheritance
The
order of inheritance by derived class to inherit the base class is left to
right.
#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 detail //Base
class
{
int dd,mm,yy;
char cl[4];
public:
void acceptdob()
{
cout<<"\n Enter date,month,year in digits and class
.. ";
cin>>dd>>mm>>yy>>cl;
}
void displaydob()
{
cout<<"\n class:-"<<cl;
cout<<"\t\t DOB :
"<<dd<<” - “<<mm<<” –“ <<yy<<endl;
}
};
class exam : public student,public detail //derived class with
multiple
base class
{
public:
int mark1, mark2
,mark3,mark4,mark5,mark6,total;
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;
}
};
int main()
{
exam e1;
e1.acceptname(); //calling
base class function using derived class object
e1.acceptdob(); //calling
base class function using derived class object
e1.acceptmark();
e1.displayname(); //calling
base class function using derived class object
e1.displaydob(); //calling
base class function using derived class object
e1.displaymark();
return 0;
}
Enter roll no and name .. 1201 MEENA
Enter date,month,year in digits and class .. 7 12 2001 XII
Enter lang,eng,phy,che,csc,mat marks.. 96 98 100 100 100 100
Roll no :-1201
Name :- MEENA
class :-XII DOB :
7 - 12 -2001
Marks Obtained
Language.. 96
English .. 98
Physics .. 100
Chemistry.. 100
Comp.sci.. 100
Maths .. 100
In
the above program the class “exam” is derived from class “student” and “detail”
Hence it access all the members of both the classes.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2026 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.