Access control in publicly derived class
From a publicly derived class, public and protected members of the base class remain public and protected members of the derived class. The public members can accessed by the object of the derived class similar to its own members in public.
#include<iostream>
#include<string>
using namespace std;
class Employee
{
private:
char name[50];
int code;
public:
void getinfo();
void dispinfo();
};
class staff: public Employee
{
private:
int ex;
public:
void getdata();
void display();
};
void Employee::getinfo()
{
cout<<"Name:";
gets(name);
cout<<"Code:";
cin>>code;
}
void Employee::dispinfo()
{
cout<<"Name:"<<name<<endl;
cout<<"Code:"<<code<<endl;
}
void staff::getdata()
{
cout<<"Experience:";
cin>>ex;
}
void staff::display()
{
cout<<"Experience:"<<ex<<" Years"<<endl;
}
int main()
{
staff s;
cout<<"Enter data"<<endl;
s.getinfo(); //derived member function
s.getdata(); //defined member function
cout<<endl<<endl<<"\tDisplay Data"<<endl;
s.dispinfo(); //derived member function
s.display();//defined member function
return 0;
}
Enter data
Name:USHA
Code:1201
Experience: 30
Display Data
Name:USHA
Code:1201
Experience:30 Years
In the above program since “staff” is derived publicly even the derived function can be accessed by the object of the class.
We cannot deny access to certain members of a base class when inhering publicly.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.