Table of Contents
Classes are mainly composed of data members and member functions. However, one more component called access specifiers determines the visibility of the members of a class.
More like this:
- C++ Class Members and Objects
- C++ Introduction to Object-Oriented
- C++ Introduction to Classes and Objects
C++ allows us to specify these access specifiers to provide more control over the data. In this lesson, we will compare different access specifiers and their uses.
Different Access Specifiers in C++
C++ provides three access specifiers, which are public, private, and protected. Each of them has unique behavior and use case.
[adinserter block=”1″]
We can specify the access specifiers inside a class with the type name (public
, private
, or protected
) followed by a colon. Any class member declared after that access specifier will be public, private, or protected based on the type name.
The general syntax for using access specifiers in C++
class tagname { public: [public members]; private: [private members]; protected: [protected members]; };
If we declare a class member without any access specifier, then it is private
by default.
class tagname { /* if no access specifier is provided, it is private by default */ […private members…]; public: [public members]; protected: [protected members]; };
Public Access Specifier
A public access specifier makes the members visible to the code inside and outside of a class. Public data members can be read and modified from anywhere in our code. Therefore, this makes them vulnerable to any unwanted changes.
Using the dot notation, we can directly access any public member of a class.
/* use a public data member */ int data = obj.publicdata; /* change a public data member */ obj.publicdata = somevalue; /* invoke a public member function */ obj.publicfunc();
Private Access Specifier
The private access specifier makes the members visible only inside the class definition. We cannot access them using a dot operator.
Only functions of a class can use a private data member or invoke a private function.
class Example { private: int num; public: void setnum() { /* only member functions can access the private data */ num = 10; } }; /* declare an object of the example class */ Example obj; /* Error as num is a private member and not accessible outside the class */ obj.num = 10;
Private data members make the code more secure by preventing any accidental change or alteration of data outside the class.
[adinserter block=”1″]
Similarly, private member functions are only for use inside the class definition.
Protected Access Specifier
A protected specifier has only significance in inheritance. With inheritance, a class (called base/child) can extend and reuse the members of another class (called derived/parent).
A protected member is not accessible outside of a class, just like a private member. However, in inheritance, it is passed down to the derived/child class.
Accessors and Mutators
Accessors and mutators are public member functions that help us control a class member, whether private, protected, or public.
Accessor (or Getter)
An accessor is a public member function that is used to retrieve a data member’s value. We can never access a private data member outside its class definition.
Still, it may be required to access them in some cases. For that, we can define a public function (accessor) to return the private data member’s value as below.
class Example { private: int num; public: /* an accessor function */ int get_num() { /* member functions have access to private members */ return num; } }; Example obj; int val; /* ERROR: cannot use num directly, as it is a private member */ val = obj.num; /* CORRECT: as get_num() is a public member function */ val = obj.get_num();
Mutator (or Setter)
A mutator is a public member function used to set the value of a class data member.
class Example { private: int num; public: /* a mutator function */ void set_num(int value) { /* Perform validation to filter out and reject any unexpected value. This is the benefit of using a mutator function. */ if (num > 0) { num = value; } } int get_num() { return num; } }; Example obj; /* set the private value through a public function(mutator) */ obj.set_num(5); /* retrieve the private value through a public function (accessor) */ cout << obj.get_num();
We can provide secure and control data flow data by using a private variable and pairing up with mutators and accessors.
[adinserter block=”1″]
Points to Remember
- Access specifiers control the visibility of the class members. C++ provides three access specifiers, which are private, protected, and public.
- Private member: Not accessible from outside nor passed down on inheritance.
- Protected member: Not accessible outside of a class but passed on inheritance.
- Public member: Accessible both inside and outside of a class and passed on inheritance.
- Accessors and mutators are public member functions that help us get and set a private data member’s value.