Inheritance
and Access Control
When
you declare a derived class, a visibility mode can precede each base class in
the base list of the derived class. This does not alter the access attributes
of the individual members of a base class , but allows the derived class to
access the members of a base class with restriction.
As
you already know ,you can derive classes using any of the three visibility
mode:
•
In a public base class, public and protected members of the base class remain
public and protected members of the derived class.
•
In a protected base class, public and protected members of the base class are
protected members of the derived class.
•
In a private base class, public and protected members of the base class become
private members of the derived class.
In
all these cases, private members of the
base class remain private and cannot be used by the derived class. However it can be indirectly accessed by
the derived class using the public or protected member function of the base
class since they have the access privilege for the private members of the base
class.
#include<iostream>
using namespace std;
class add
{
int num1,sum;//private data member can’t be derived by derived
class
protected:
int num2;
public:
add()
{
num1= num2= sum=0;
cout<<"\n Add constructor .. ";
}
accept()
{
cout<<"\n Enter two numbers .. ";
cin>>num1>>num2;
}
plus()
{
sum = num1 + num2;
cout<<"\n The sum of two numbers is .. "<<
sum;
int difference(){ return num1-num2;} //return an integer value
~add()
{cout<<endl<<"Add destructor"; }
};
class subtract :public add //subtract
derived from add
{
int sub;
public:
subtract()
{
sub = 0;
cout<<"\n Subtract constructor .. ";
}
minus()
{
accept(); // member function of base class can access its
datamember sub= difference();
cout<<"\n The difference of two numbers are ...
"<< sub;
}
~subtract()
{cout<<endl<<"Subtract destructor"; }
};
int main()
{
subtract s;
int choice = 0;
cout<<"\n Enter your choice ";
cout<<" \n1. Add..\n2. Subtract ..";
cin>>choice;
switch( choice )
{
case 1:
s.accept();
s.plus();
break;
case 2:
s.minus();
break;
}
return 0;
}
A member function can call another member function without dot
operator and object
Add constructor ..
Subtract constructor ..
Enter your choice
Add..
Subtract .. 2
Enter two numbers .. 20 10
The difference of two numbers are ... 10
Subtract destructor
Add destructor
In
the above program the data member num1 which is under private visibility in add
class cannot be inherited by the subtract class.But subtract class can inherit
the protected and public members of add class. Hence using the inherited
accept() function the value of num1 is accepted and using the inherited
difference() function the values are subtracted and passed to the subtract
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.
From
a privately derived class, public and protected members of the base class
become private members of the derived class. Hence it is not possible to access
the derived members using the object of the derived class.The Derived members
are invoked by calling it from the publicly defined members
#include<iostream>
#include<string>
using namespace std;
class Employee
{
private:
char name[50];
int code;
public:
void getinfo();
void dispinfo();
};
class staff: private 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()
{
getinfo(); //invoked inside
cout<<"Experience:";
cin>>ex;
}
void staff::display()
{
dispinfo(); //member
function called inside another member function
cout<<"Experience:"<<ex<<"
Years"<<endl;
}
int main()
{
staff s;
cout<<"Enter data"<<endl;
s.getdata();
cout<<endl<<endl<<"\tDisplay
Data"<<endl;
s.display();
return 0;
}
Enter data
Name: BALAMURUGAN
Code: 1201
Experience: 30
Display Data
Name: BALAMURUGAN
Code: 1201
Experience: 30 Years
If
you look at the output of publicly derived class and privately derived class are
same the way of defining is different. This is because the private members of
the derived class cannot be accessed by its object. In the above program since
staff is privately derived getinfo() and dispinfo() become private to class
“staff”. To access both the member function they are invoked inside the publ;ic
member functions getdata() and display() respectively.
Member functions can access the private members
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.