Example of Access Specifiers in C++
Let's look at a practical example to illustrate how these access specifiers work:
C++
#include <iostream>
using namespace std;
class Base {
public:
int publicVar;
void publicMethod() {
cout << "Public Method" << endl;
}
private:
int privateVar;
void privateMethod() {
cout << "Private Method" << endl;
}
protected:
int protectedVar;
void protectedMethod() {
cout << "Protected Method" << endl;
}
};
class Derived : public Base {
public:
void accessMethods() {
publicVar = 1; // Accessible
publicMethod(); // Accessible
// privateVar = 2; // Not accessible
// privateMethod(); // Not accessible
protectedVar = 3; // Accessible
protectedMethod(); // Accessible
}
};
int main() {
Base base;
Derived derived;
base.publicVar = 5; // Accessible
base.publicMethod(); // Accessible
// base.privateVar = 10; // Not accessible
// base.privateMethod(); // Not accessible
// base.protectedVar = 15; // Not accessible
// base.protectedMethod(); // Not accessible
derived.accessMethods(); // Access through derived class
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output
Public Method
Public Method
Protected Method
In this example:
- The public members of Base are accessible both inside and outside the class, and from derived classes.
- The private members are only accessible within the Base class itself.
- The protected members are accessible within Base and its derived classes but not outside these classes.
Types of Access Modifiers
In C++, there are three kinds of access modifiers:
1. Public access modifier
2. Private access modifier
3. Protected Access modifier
Let us discuss each one of them.
1. Public Access Modifier
Public means all class members declared public might be available to everyone. The data members and member functions said other classes could access the public. Hence, public members can be called from any part of the program.
Example
C++
#include <bits/stdc++.h>
using namespace std;
class Sample
{
public:
int age;
void displayAge()
{
cout << "Age = " << age << endl;
}
};
int main()
{
Sample obj1;
cout << "Enter your age: ";
cin >> obj1.age;
obj1.displayAge();
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
Enter your age: 19
Age = 19
You can also try this code with Online C++ Compiler
Run Code2. Private Access Modifier
The class which was declared as private can only be accessed inside the class where declared. The private class cannot be accessed outside the class and throws a compile-time error. By default, functions and class variables are private.
Example
C++
#include <bits/stdc++.h>
using namespace std;
class Sample
{
private:
int age;
public:
void displayAge(int a)
{
age = a;
cout << "Age = " << age << endl;
}
};
int main()
{
int ageInput;
Sample obj1;
cout << "Enter your age: ";
cin >> ageInput;
obj1.displayAge(ageInput);
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
Enter your age: 19
Age = 19
You can also try this code with Online C++ Compiler
Run Code3. Protected Access Modifier
Protected is the last access specifier, and it is similar to private. It makes class members inaccessible outside the class, but they can be accessed by any subclass of that class ( Also known as Inheritance ).
Example
C++
#include <bits/stdc++.h>
using namespace std;
class Sample
{
protected:
int age;
};
class SampleChild: public Sample
{
public:
void displayAge(int a)
{
age = a;
cout << "Age = " << age << endl;
}
};
int main()
{
int ageInput;
SampleChild child;
cout << "Enter your age: ";
cin >> ageInput;
child.displayAge(ageInput);
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output:
Enter your age: 19
Age = 19
Try and compile with online c++ compiler.
Know What is Object in OOPs here in detail.
Frequently Asked Questions
What is the use of access modifiers in C++?
Access Modifiers or access Specifiers in a class are used to assign the accessibility to the class members. that is, it sets some restrictions on the class members not to get directly accessed by the outside functions.
What is the difference between access specifiers in C++?
- public: Members are accessible from anywhere.
- private: Members are accessible only within the class.
- protected: Members are accessible within the class and derived classes.
What is the default access specifier?
The default access specifier for members of a class in C++ is private.
What is the difference between open and public access specifiers?
C++ does not have an "open" access specifier. In languages like Kotlin, "open" allows a class to be inherited. In C++, "public" grants access to members from any part of the program.
How many public classes a C++ file can have?
In fact, you can not create public classes in a single document, only one class should be public and it should be the name of the class. if you try to create public classes in the same file the compiler generates a compile-time error.
Conclusion
We learned about the access modifiers for C++ classes with the help of examples. And also learned all other types and functions that can access public elements. Public details can be accessed by all other classes and functions, and protected details are just like the private, except derived classes can access them.
Recommended Reading: