C++C++11C++14C++17Introduction to C++Learn C++

Learn about Access Specifiers in C++ Classes

Object-Oriented Programming is a way to integrate with objects which can contain data in the form (attributes or properties of objects), and code blocks in the form of procedures (methodsfunctions of objects). These attributes and methods are variables and functions that belong to the class, they are generally referred to as class members.

Think that you have a class and you have integers, floating numbers, strings, and some methods. And maybe you have some attributes or methods that you don’t want to be seen by other classes or functions. Maybe you have a class in a dynamic library and you want to make some of them visible to developers, and you want to hide some of them. If you are asking, how I can make visible and hidden members in classes, C++ is a great programming language with very specific features. Access Specifiers are the statements used for this situation.

The Access Specifiers are one of the important parts of Classes, and they are used before you define your attributes (properties) or methods (functions). Access specifiers use used to define how the members (attributes and methods) of a class can be accessed. All the attributes and methods listed after these Access Specifiers have the same access. Thus, you can define class members as a public member, or private member, or a protected member.

In C++, mostly there are 3 Access Specifiers in Classes, these are :

  • public: members of a class are accessible from outside the class
  • private: members of a class can’t be accessed (or viewed) from outside the class, only used inside the class or used by the methods of this class
  • protected: members of a class can’t be accessed from outside the class. However, they can be accessed in inherited classes. Inheritance will be explained in another post later.

in addition to these, there might be specific Access Specifiers, for example,

  • __published: In C++ Builder, specifies that members in that section are displayed in the Object Inspector if the class is on the Component palette. Only classes derived from TObject can have __published sections.

A general class example with all public, private, and protected sections, can be given as below,

What happens if I don’t use an Access Specifier? Can we create a class without any Access Specifier? The answer is yes, we don’t need to use, but you should know that all the members of that class will be private.

Here above all members i, j, f, g, name are private.

For example, let’s define a THuman Class for human, name will be public and age and weight will be private, Here is the THuman class with private and public members,

We can also add some protected members that can’t be accessed from outside the class. However, they can be accessed in inherited classes,

Not that there is a possibility to access private members of a class using a public method inside the same class. This is called Encapsulation, Please read our next posts about Encapsulation to use it. Also, try to declare your class attributes as private as you can. It reduces the possibility of others or you make changes in the code or values of members. This is also needed to use Encapsulation.

Get started building powerful apps with C++Builder!

close

Oh hi there đź‘‹
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

Related posts
C++C++11C++14C++17Introduction to C++Learn C++

Learn about C++ Encapsulation

C++C++11C++14C++17Introduction to C++Learn C++

Discover Class Methods in C++

C++Learn C++Videos

Learn C++ With Monoids, Monads, and Applicative Functors: Repeated Software Patterns (CPPCon 2020) Video

C++Learn C++Videos

Learn C++ With Pipes: How Plumbing Can Make Your C++ More Expressive by Jonathan Boccara (CPPCon 2020) Video

Worth reading...
Learn to Use Parameters in Functions in C++
en_USEnglish