Javatpoint Logo
Javatpoint Logo

Access Class Members in C++

C++ is a strong and flexible programming language renowned for its object-oriented features. Encapsulation is one of the core concepts of object-oriented programming (OOP), which enables us to hide the internal features of a class and expose only the necessary functionality to the outside world. To do this, C++ offers a number of access specifiers that regulate the visibility of class members, such as public, private, and protected. In this article, we will examine these access specifiers and learn how to access class members in C++ with a tonne of examples and explanations.

Overview of Access Specifiers:

Access specifiers control the visibility and use of class members (variables and functions) within a class. Three main access specifiers are available in C++:


Anyone can access members who have been designated as public, both inside and outside of the class. This specifier offers the widest visibility, enabling access to the member from any area of the program.


Members of the class that defined them can only access private members. They are ideal for storing internal implementation details because they are concealed from the outside world.


Members who have been designated as protected are somewhat different from private members. They are reachable from the class and classes that it has derived. If we wish to provide subclasses only a certain amount of access, protected members are frequently employed.

Accessing Public Class Members:

Let's start by examining how to access members of the public class. We can access public members from anywhere in the program.


Let's take an example to demonstrate the access public class members in C++.


This is a public function.
Accessing publicVar from outside the class: 42


In this example, PublicVar and publicFunction are public members of the MyClass class. We create an object of MyClass named obj inside the main method. The publicVar member is directly accessible, modifiable, and callable from outside the class, as is the publicFunction member.

We can see that the public members can be accessed by anybody, anywhere, at any time. Hence they ought to be utilized for interface elements that must be accessed from outside the system.

Accessing Private Class Members:

Members of the class that defined them can only access private members. A compilation error will appear if we try to access a private member from outside the class.


Let's take an example to demonstrate the access private class members in C++.


This is a private function.


In the above example, the MyClass class's private members, privateVar and privateFunction, are declared. Private members cannot be accessible from outside the class. Therefore, if we uncomment the line that tries to access privateVar directly, a compilation error will result.

We have supplied the public member functions accessPrivateFunction and setPrivateVar to indirectly access the private members. Enforcing encapsulation and managing access to sensitive information and implementation details are typical practices in C++.

As per the output, the private function privateFunction was reachable via the public member function accessPrivateFunction. Similarly, we use the public member method setPrivateVar to set the value of privateVar.

Accessing Members of Protected Classes:

Private and protected members both have restriction against direct access from outside the class. However, they differ significantly in that derived classes (subclasses) can access protected members.


Let's take an example to demonstrate the access protected class members in C++.


Accessing protectedVar from a derived class: 42


In this example, the ProtectedVar is designated as a protected member of MyBaseClass. MyDerivedClass is a derived class that comes from MyBaseClass. We construct a MyDerivedClass object in main and try to access protectedVar directly. However, it would cause a compilation problem.

Instead, we use MyBaseClass's public member functions to access protectedVar. The protectedVar function's value is set using the setProtectedVar function, and its value is retrieved using the getProtectedVar function. These functions allow the derived class MyDerivedClass to access protectedVar, proving the use of the protected access specifier.

Proper usage of Access specifiers:

After going over C++'s three access specifiers, let's talk about when to utilize each one.

  • Use the public access specifier for class members that should be reachable from wherever in the program. These components of the class's interface specify how outside code can communicate with it.
  • Use the private access specifier for members that are internal to the class and should not be immediately accessed from the outside. Typically, private members are used for implementation specifics and to impose data concealment.
  • If we want to provide derived classes with restricted access, use the protected access specifier. It keeps unauthorized code from directly accessing the protected members while yet allowing derived classes to inherit and utilize them.


Creating reliable and maintainable code in C++ requires an understanding of and skill with access specifiers. We can design classes that are efficient and secure by adhering to the encapsulation principles and managing the visibility of class members with public, private, and protected access specifiers.

In conclusion, public members establish the class's interface and are reachable from everywhere. The private members are used for internal implementation details and are hidden from the public. Derived classes can access protected members, giving subclasses a restricted mechanism to obtain information.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA