Access Specifiers in C++

C++: Access Specifiers

On this page we will discuss about 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 it’s written under.

Access Specifiers in C++

Access Specifiers in C++ Language

The Access Specifiers help to achieve Data Hiding in C++.Basically, access specifiers are used to 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

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

Example program –

class Rectangle{
    
    private:
        int length, breadth;
        
    public:
        int variable1, variable2;
        void getValues();
        int area();
};

Access Specifiers in C++

Specifier Within Same Class In Derived Class Outside the Class
Private Yes No No
Protected Yes Yes No
Public Yes Yes Yes

1. Public Access Specifier

If any class member (data members/member functions) is declared under public then –

  1. Are accessible to everyone.
  2. Using the dot(.) function they can be accessed

An example of this is show below –

Run
#include <iostream>
using namespace std;

class Rectangle{
    
    // all below declared under public
  public: 
    int length, breadth;

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

int main(){
    Rectangle rect1;
    
    // can be accessed outside the class
    rect1.length = 10;
    rect1.breadth = 20;
    
    cout << "Area is: " <<   rect1.area();
    return 0;
}

Output

Area is: 200

2. Private Access Specifier

If any class member is declared under private then –

  1. 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.

An example of this is shown below –

Run
#include <iostream>
using namespace std;

class Rectangle{ 
  
  // these are a private 
  private: 
    int length, breadth;

  // these are public
  public: 
  
    // area is a member function of the class thus
    // can access private data members
    int area(){ 
        return length * breadth;
    }
};

int main()
{ 
    Rectangle rect1;
    
    // Error 
    // trying to access private member outside class
    rect1.length = 10;
    rect1.breadth = 20;

    // public member can be accessed 
    cout << "Area is:" << rect1.area();
    
    return 0;
}

Output

error: 'int Rectangle::length' is private within this context

We can solve the above setting/fetching value issue in the following ways –

  • Creating getter/setters
  • Initializing values via constructors
  • Setting values via public function

We will do this via setting values via public function below, however this can also be done via other methods too.

Run
#include <iostream>
using namespace std;

class Rectangle{ 
  
  // these are a private 
  private: 
    int length, breadth;

  // these are public
  public: 
  
    // area is a member function of the class thus
    // can access private data members
    int area(int l, int b){
        
        //setting values here
        length = l;
        breadth = b;
        
        // return result after setting values
        return length * breadth;
    }
};

int main()
{ 
    Rectangle rect1;

    // public member can be accessed 
    // also can be used to set values
    cout << "Area is: " << rect1.area(10, 20);
    
    return 0;
}

Output

Area is: 200

Protected Access Specifier

If any class members are declared under-protected then –

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

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

Run
#include <iostream>
using namespace std;

// Base(parent/super) class
class Rectangle
{ 
  // protected data members
  protected:
    int height;
};

// Derived(child/sub) class
class Square : public Rectangle
{
  public:
    void setHeight(int h){
        
        // Child class is able to access 
        // the inherited protected data members (height)
        // of base class
        height = h;
    }

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

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

Summary

Public

Public Access Specifiers allow access –

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

Protected

Protected 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

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription