ACCESS SPECIFIERS:
The access specifier provides access rights for the class members. Access
specifiers available in C++ are: private, public and protected.
They are used in C++ to implement a concept called data hiding.
It means it cannot be accessed by functions outside the class even by mistake.
1.
PRIVATE:
The mechanism
used to hide data is to put it in a class and make it private. Private data and
functions can be accessed from within the class only. We usually declare data
in this section. A class guarantees maximum protection to private members.
2.
PUBLIC:
A class does
not provide any security to the public members. They can be accessed from both
inside and outside the class. We usually declare functions in this section.
3.
PROTECTED:
The access
specifier protected is same as that of private. But the difference is a private
member can’t be inherited where as protected member can be inherited by the
derived classes. Derived classes will be having rights over the base classes
protected members.
Syntax:
class class-name
{
private/public/protected:
data_members;
member_functions;
};
IMPORTANT FEATURES OF ACCESS
SPECIFIERS:
1. By default, all the members of a class are
private and they cannot be accessible outside the class.
2. Private members accessible only with in the
class.
3. Private members can be accessed through
public members.
4. The member variables and functions that are
declared as public can be accessed by the members outside the class.
5. Protected members can be inherited by the
derived classes.
Example:
#include<iostream.h>
class shape
{
private:
int a, b;
void set_val()
{
a=10;
b=20;
}
void display()
{
cout<<“a=“<<a<<“\nb=“<<b;
}
public:
void sum()
{
set_val();
display();
cout<<“\nsum=“<<a+b;
}
void sub(void);
};
void shape::sub()
{
a=30;
b=10;
cout<,”\nsubtraction=“<<a-b;
}
void main()
{
shape s;
s.sum(); // sum() is a public member. It accesses set_val() and
display() which are private
members.
s.sub();
}
Output:
a=10
b=20
sum =30
Subtraction=20
Example for access specifier is of the base class is public
Base class access control
1.
The derived class will
have the members of the base class based on the declaration type such as
private, public or protected and the access specifier at the time of inheriting
the base class.
2.
The members of the base
class will be protected from the derived class based on the access specifier.
All the private members of base class will not be inherited by the derived
class irrespective of the access specifier.
In
case the derived class is a class, the following cases will be based on the
access specifer:
Case 1: Access specifier is public:
a.
All the public members
of base class will become the public members of the derived class.
b.
All the protected
members of the base class will become the protected members of derived class.
c.
All the private members
of the base class will not be inherited to the derived class.
Case 2: Access specifier is protected:
a.
All the public members
of the base class will become protected members of the derived class.
b.
All the protected
members of the base class will become the protected members of the derived
class.
c.
All the private members
of the base class will not be inherited to the derived class.
Case 3: Access specifier is private:
a. All the public members of the base class will become private
members of the derived class.
b. All the protected members of the base class will become the
private members of the derived class.
c. All the private members of the base class will not be
inherited to the derived class.
Visibility
of inherited members
Base class visibility
|
Derived class visibility
|
||
Public derivation
|
Private derivation
|
Protected derivation
|
|
Private
|
Not inherited
|
Not inherited
|
Not inherited
|
Protected
|
Protected
|
Private
|
Protected
|
Public
|
Public
|
Private
|
Protected
|
Example for access specifier is of the base class is public
No comments:
Post a Comment