×
>
<

C++ Programming

C++ Access Specifiers | CrackEase

Access Specifiers in C++

Access Specifiers in C++

Data hiding in C++ is achieved with the help of access specifiers (also called access modifiers). Class members — both data members and member functions — can have different accessibility depending on which access specifier they are declared under.

Access specifiers let you control which parts of a class are visible outside the class and which parts remain hidden (encapsulated).

Types of Access Modifiers

C++ has three access specifiers:

  1. public
  2. private
  3. protected

Tip: If no access specifier is provided, members of a class are private by default (compare this with struct, whose members are public by default).

Example (corrected)
  
class Rectangle{
  private:
    int length, breadth;

  public:
    void getValues(int l, int b);
    int area() const;
};
  
Access specifiers diagram
Summary
Public

Public members are accessible:

  • Within the same class
  • From derived classes
  • From outside the class (anywhere where the object is visible)

Protected

Protected members are accessible:

  • Within the same class
  • From derived classes (they behave like protected members in derived classes)

Protected members are not accessible directly from outside the class.

Private

Private members are accessible only:

  • Within the same class (i.e., by member functions and friends)

Private members are not accessible from derived classes or from outside the class.

Public Access Specifier

If a class member is declared public then:

  1. It is accessible from any code that has access to the object.
  2. Public members are accessed using the dot operator (object.member).

Example Program –

  
#include <iostream>
using namespace std;

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

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

int main(){
  Rectangle rect1;

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

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

If a class member is declared private then:

  1. It can only be accessed by member functions of the same class (or friend functions/classes).
  2. It is not accessible directly from outside the class.

Example Program demonstrating error when accessing private members directly –

  
#include <iostream>
using namespace std;

class Rectangle{
private:
    int length;
    int breadth;

public:
    void setValues(int l, int b) {
        length = l;
        breadth = b;
    }
    int area() const {
        return length * breadth;
    }
};

int main()
{
  Rectangle rect1;

  // The following lines would cause a compilation error if uncommented:
  // rect1.length = 10;
  // rect1.breadth = 20;

  // Correct way: use public member function
  rect1.setValues(10, 20);
  cout << "Area is: " << rect1.area() << endl;
  return 0;
}
  
output
  
Area is: 200
  

Notes: By keeping data members private and exposing public setter/getter (or other API) functions, you enforce invariants and validation and avoid accidental misuse.

Protected Access Specifier

If a class member is declared protected then:

  1. It behaves like private for outside code.
  2. However, derived classes (subclasses) can access protected members directly.

Example (uses simple inheritance):

  
#include <iostream>
using namespace std;

// base class
class Rectangle
{
protected:
    int height;
public:
    Rectangle() : height(0) {}
};

// derived class
class Square : public Rectangle
{
public:
    void setHeight(int h) {
        // derived class can access protected member of base class
        height = h;
    }
    void displayHeight() const {
        cout << "height is: " << height << endl;
    }
};

int main() {
    Square square1;
    square1.setHeight(10);
    square1.displayHeight();
    return 0;
}
  
output
  
height is: 10
  
Footer Content | CrackEase