Access Specifiers
Each user has different access privileges to the object.
• private
• public : int a;
• protected
• These keywords are called access-control
specifiers.
• All the members that follow a keyword belong to
that type.
• If no keyword is specified, then the members are
assumed to have private privilege.
class PiggyBank
{
int Money; //Private by default
void Display () //Private by default
{
….
}
private: //Private by declaration
int AccNumber;
public:
int code; //public
void SetData(int a; int b) //public
{
….
}
protected:
int PolicyCode; //Protected by declaration
void GetPolicyCode() ,,
{
….
}
};
Member Money, AccNumber and Display() will be
the type private, the member code and SetDate()
will be of type public; and the members
PolicyCode and GetData() will be of type
protected.
Data Hiding is mainly designed to protect well-
intentioned programmers from honest mistakes.
There are mechanisms to access even private data
using friends, pointer to members, etc. from
outside the class.
Member Money, AccNumber and Display() will be
the type private, the member code and SetDate()
will be of type public; and the members
PolicyCode and GetData() will be of type
protected.
Data Hiding is mainly designed to protect well-
intentioned programmers from honest mistakes.
There are mechanisms to access even private data
using friends, pointer to members, etc. from
outside the class.
Private Members
• Only the member functions of the same class
can access these members.
• Access control in C++ has the objective of
reducing the likelihood of bugs and enhancing
consistency.
class Person
{
private: //access specifier
//private members
………..
int age; //private data
int getage(); //private function
……..
};
Person p1; //create object of class Person
a=p1.age; //cannot access private data
p1. getage(); // cannot access private function
Private members accessibility
Error : Invalid access
Protected Members
• The access control of the protected members
in similar to that of private members and has
more significance in inheritance.
class Person
{
protected: //access specifier
//protected members
………..
int age; // protected data
int getage(); // protected function
……..
};
Person p1; //create object of class Person
a=p1.age; //cannot access protected member
p1. getage(); (same as private)
Protected members accessibility
Public Members
• The members of a class, which are to be
visible (accessible) outside the class, should be
declared in public section.
• All data members and functions declared in
the public section of the class can be accessed
without any restriction from anywhere in the
program, either by functions that belong to
the class or by those external to the class.
class Person
{
public: //access specifier
//public members
………..
int age; // public data
int getage(); // public function
……..
};
Person p1; //p1 object of class Person
a=p1.age; //can access public data
p1. getage(); //can access public function
Public members accessibility
No Error :
Can access public data
Can access public function
Visibility of class members
Access Specifier Accessible to
Own Class Members Objects of a Class
private: Yes No
protected: Yes No
public: Yes Yes
Difference between Private and
Protected
Private members Protected members
Private members are declared with the
keyword private followed by a colon (:)
character.
Protected members are declared with the
keyword protected followed by a colon (:)
character.
Private members are accessible within the
same class in which they are declared.
Protected members are accessible within
the same class and within the
derived/sub/child class.
Private members can also be accessed
through the friend function.
Protected members cannot be accessed
through the friend function.
//protected
#include <iostream>
using namespace std;
// base class
class Parent {
// protected data members
protected:
int id_protected;
};
// sub class or derived class
class Child : public Parent {
public:
void setId(int id)
{
// Child class is able to access the inherited
// protected data members of the base class
id_protected = id;
}
void displayId()
{
cout << "id_protected is: "
<< id_protected << endl;
}
};
// main function
int main()
{
Child obj1;
// member function of the derived class can
// access the protected data members of the
base class
obj1.setId(81);
obj1.displayId();
return 0;
}
Output
id_protected is: 81
//Private
#include <iostream>
using namespace std;
class Circle {
// private data member
private:
double radius;
// public member function
public:
void compute_area(double r)
{
// member function can access private
// data member radius
radius = r;
double area = 3.14 * radius * radius;
cout << "Radius is: " << radius << endl;
cout << "Area is: " << area;
}
};
// main function
int main()
{
// creating object of the class
Circle obj;
// trying to access private data member
// directly outside the class
obj.compute_area(1.5);
return 0;
}
Output
Radius is: 1.5
Area is: 7.065

Access specifiers (Public Private Protected) C++

  • 1.
  • 2.
    Each user hasdifferent access privileges to the object. • private • public : int a; • protected • These keywords are called access-control specifiers. • All the members that follow a keyword belong to that type. • If no keyword is specified, then the members are assumed to have private privilege.
  • 3.
    class PiggyBank { int Money;//Private by default void Display () //Private by default { …. } private: //Private by declaration int AccNumber; public: int code; //public void SetData(int a; int b) //public { …. } protected: int PolicyCode; //Protected by declaration void GetPolicyCode() ,, { …. } };
  • 4.
    Member Money, AccNumberand Display() will be the type private, the member code and SetDate() will be of type public; and the members PolicyCode and GetData() will be of type protected. Data Hiding is mainly designed to protect well- intentioned programmers from honest mistakes. There are mechanisms to access even private data using friends, pointer to members, etc. from outside the class.
  • 5.
    Member Money, AccNumberand Display() will be the type private, the member code and SetDate() will be of type public; and the members PolicyCode and GetData() will be of type protected. Data Hiding is mainly designed to protect well- intentioned programmers from honest mistakes. There are mechanisms to access even private data using friends, pointer to members, etc. from outside the class.
  • 6.
    Private Members • Onlythe member functions of the same class can access these members. • Access control in C++ has the objective of reducing the likelihood of bugs and enhancing consistency.
  • 7.
    class Person { private: //accessspecifier //private members ……….. int age; //private data int getage(); //private function …….. }; Person p1; //create object of class Person a=p1.age; //cannot access private data p1. getage(); // cannot access private function Private members accessibility
  • 8.
  • 9.
    Protected Members • Theaccess control of the protected members in similar to that of private members and has more significance in inheritance.
  • 10.
    class Person { protected: //accessspecifier //protected members ……….. int age; // protected data int getage(); // protected function …….. }; Person p1; //create object of class Person a=p1.age; //cannot access protected member p1. getage(); (same as private) Protected members accessibility
  • 11.
    Public Members • Themembers of a class, which are to be visible (accessible) outside the class, should be declared in public section. • All data members and functions declared in the public section of the class can be accessed without any restriction from anywhere in the program, either by functions that belong to the class or by those external to the class.
  • 12.
    class Person { public: //accessspecifier //public members ……….. int age; // public data int getage(); // public function …….. }; Person p1; //p1 object of class Person a=p1.age; //can access public data p1. getage(); //can access public function Public members accessibility
  • 13.
    No Error : Canaccess public data Can access public function
  • 14.
    Visibility of classmembers Access Specifier Accessible to Own Class Members Objects of a Class private: Yes No protected: Yes No public: Yes Yes
  • 15.
    Difference between Privateand Protected Private members Protected members Private members are declared with the keyword private followed by a colon (:) character. Protected members are declared with the keyword protected followed by a colon (:) character. Private members are accessible within the same class in which they are declared. Protected members are accessible within the same class and within the derived/sub/child class. Private members can also be accessed through the friend function. Protected members cannot be accessed through the friend function.
  • 16.
    //protected #include <iostream> using namespacestd; // base class class Parent { // protected data members protected: int id_protected; }; // sub class or derived class class Child : public Parent { public: void setId(int id) { // Child class is able to access the inherited // protected data members of the base class id_protected = id; } void displayId() { cout << "id_protected is: " << id_protected << endl; } }; // main function int main() { Child obj1; // member function of the derived class can // access the protected data members of the base class obj1.setId(81); obj1.displayId(); return 0; } Output id_protected is: 81
  • 17.
    //Private #include <iostream> using namespacestd; class Circle { // private data member private: double radius; // public member function public: void compute_area(double r) { // member function can access private // data member radius radius = r; double area = 3.14 * radius * radius; cout << "Radius is: " << radius << endl; cout << "Area is: " << area; } }; // main function int main() { // creating object of the class Circle obj; // trying to access private data member // directly outside the class obj.compute_area(1.5); return 0; } Output Radius is: 1.5 Area is: 7.065