×
>
<

Aptitude

Access Specifiers in C++

Access Specifiers in C++

Data hiding in C++ is achieved with the help of Access modifiers as known as Access specifiers. Class members (both data members and member functions) can have varying accessibility depending on the type of access specifier its written under.

The Access Specifiers help to achieve Data Hiding in C++.

Basically, access specifiers are used hide/show data from outside world, it may cause a particle class member to not to be accessible by outside scope functions.

Types of Access Modifiers

C++ has three different types of access modifiers –

  1. public
  2. private
  3. protected

Tips : If you don’t specify any keyword then, by default in C++ the class members will take private behaviour.

Example program for Data Abstraction
  
class Rectangle{
  private:
  int length, breadth;
  Public:
  void getValues();
  int area();
};
  
Summary
Public

Public Access Specifiers allow access –

  • Within the same class
  • Within the derived class
  • Outside the class

Protected

Public Access Specifiers allow access –

  • Within the same class
  • Within derived class as protected members

Protected Access Specifier don’t allow –

  • Outside the class

Private

Private Access Specifiers allow access –

  • Within the same class

Protected Access Specifier don’t allow –

  • Within the derived class
  • Outside the class

Public Access Specifier

If any class member is declared under public then –

  1. All such class members(both data members and member functions) are accessible by objects of other classes in the program as well
  2. Using dot(.) function they can be accessed

Example Program –

  
#include "iostream"
using namespace std;

// class definition
class Rectangle{
public: 
    int length, breadth;

int area(){
    return length*breadth;
}
};

// main function
int main(){
  Rectangle rect1;

  // accessing public datamember outside class
  rect1.length = 10;
  rect1.breadth = 20;

  cout << "Area is:" <<   rect1.area();
  return 0;
}
  
output
  
200
  
Private Access Specifier

If any class member is declared under private then –

  1. Such class members can only be accessed by functions declared inside the class
  2. Not accessible to the outside world(class)
  3. Only the member functions or the friend functions are allowed to access the private data members of a class.

Example Program –

  
#include 
using namespace std;

class Rectangle{ 
  // this is a private data member
private: 
    int length, breadth;

    // public member function 
public: 
int area()
{ 
    // Only member function can access private 
    // this is data member
    return length*breadth;
}
};

// main function
int main()
{ 
    // creating object of the class Rectangle
  Rectangle rect1;
    // This will cause error as we are trying to access private member outside class
  rect1.length=10;
  rect1.breadth=20;

    // trying to access private data member
    // directly outside the class

  cout << "Area is:" << rect.area();
  return 0;
}
  
output
  
error
  

since length and breadth are private, it will cause error at rect1.length, rect1.breadth, as we are trying to access private members outside the class.

We can solve the issue as follows, as member functions can access the variables inside the class. We create a public function to set values and display them as such –

Example Program –

  
#include 
using namespace std;

class Rectangle
{ 
//list of private data member
private: 
    int length, breadth;

// public member function 
public: 
  int area(int l, int b)
{ 
    // since, member function can access private 
    // data members
  length = l;
  breadth = b;

int area = length*breadth;

cout << "area is:" << area << endl;
}

};

// main function
int main()
{ 
  // creating object of the class
Rectangle rect1;

// trying to access private data member
// directly outside the class
rect1.area(10,20);
return 0;
}
  
output
  
error
  
Public Access Specifier

If any class member is declared under public then –

  1. All such class members(both data members and member functions) are accessible by objects of other classes in the program as well
  2. Using dot(.) function they can be accessed

Example Program –

  
#include "iostream"
using namespace std;

// class definition
class Rectangle{
public: 
    int length, breadth;

int area(){
    return length*breadth;
}
};

// main function
int main(){
  Rectangle rect1;

  // accessing public datamember outside class
  rect1.length = 10;
  rect1.breadth = 20;

  cout << "Area is:" <<   rect1.area();
  return 0;
}
  
output
  
area is:200
  
Protected Access Specifier

If any class members is declared under protected then –

  1. It is same as private accessibility
  2. But, derived(sub classes/child) classes can also access the protected class members.
  3. These protected members are inaccessible outside. But, are accessible in derived class as protected data members

Note – Program below, uses inheritance, which we will study in topics ahead. You should read inheritance and then come back on this page again to study protected access specifier again.

  
#include 
using namespace std;

// base class
class Rectangle
{ 
// protected data members
protected:
int height;
};

// sub class or derived class
class Square : public Rectangle
{
public:
void setHeight(int h)
{
// Child class is able to access the inherited 
// protected data members of base class

height = h;
}

void displayHeight()
{
cout << "height is:" << height << endl;
}
};

// main function
int main() {

Square square1;

// member function of derived class can
// access the protected data members of base class

square1.setHeight(10);
square1.displayHeight();
return 0;
}
  
output
  
height is 10