Access Specifiers

Access Specifiers control the accessibility of class members outside the class. They are used usually with data members. There are three types of access specifiers:

  1. Public
  2. Private
  3. Protected

Public Access Specifier

When we declare our members public, we can access them outside of the class. We have already discussed examples of public access modifier.

Private Access Specifier

When we declare our members private, we can access them only inside of the class.

  • Public members can be modified from outside the class.
  • Private members can not be directly changed from outside the class. We have to get their address in main function with the help of some public function and then at that address, we can change the value.

Note: By default, members of class are private. If class has data members, they will be private if do not specify access specifier.

Protected Access Specifier

We will discuss this in inheritance.

Practice Exercises

Complete these exercises to reinforce your learning and earn XP

Sign in to track your progress and earn XP!
Exercise 1 of 2Easy

What is the output when x = 5 in: if(x > 5) print A; else print B;

10 XP~3 min
Exercise 2 of 2Easy

The keyword used for alternative condition is ___.

10 XP~2 min