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.

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++Introduction to C++Language FeatureLearn C++

How To Use The Comprehensive Form Properties In C++ VCL Apps

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

This Is How To Add Characters to Wide Strings in Modern C++

C++ComponentsLanguage FeatureLearn C++

How To Create Gorgeous Transparent App Windows With C++

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

This Is How To Get A Substring of a Wide String in C++

Worth reading...
Learn to Use Object Arrays and Object Pointer Arrays in C++
en_USEnglish