Single
Inheritance
Though the
derived class inherits all the members of base class ,it
has access privilege only to non-private members of the base 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,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.acceptmark();
e1.displayname();
//calling base class function using derived class object
e1.displaymark();
return 0;
}
Enter roll no and name .. 1201 KANNAN
Enter lang,eng,phy,che,csc,mat marks.. 100 100 100 100 100 100
Roll no :-1201
Name :-KANNAN
Marks Obtained
Language.. 100
English .. 100
Physics .. 100
Chemistry.. 100
Comp.sci.. 100
Maths .. 100
In
the above program the derived class “exam” inherits all the members of the base
class “student”. But it has access privilege only to the non private members of
the base class.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.