
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Access Modifiers in C++
Access Modifiers are used to implement data hiding in object oriented programming. There are three types of access modifiers used in C++. These are public, private and protected. Details about these are given as follows.
Public Access Modifier
The data members and member functions in a class that are declared public are available to everyone, including other classes. They can be accessed from any place in the program using the dot operator with the class object.
A program that demonstrates public access specifier is given as follows.
Example
#include<iostream> using namespace std; class Add { public: int a, b; void sum() { cout<<"Sum of "<< a <<" and "<< b <<" is "<<a+b; } }; int main() { Add obj; obj.a = 2; obj.b = 5; obj.sum(); return 0; }
Output
Sum of 2 and 5 is 7
In the above program, the class Add has two public data members a and b. The function sum() displays the sum of a and b. This is seen below.
class Add { public: int a, b; void sum() { cout<<"Sum of "<< a <<" and "<< b <<" is "<<a+b; } };
In the function main(), the object of class Add is created. Then a and b are initialized in main(). This can be done because they are public data types. Finally sum() is called that displays the sum of a and b. This is shown below.
Add obj; obj.a = 2; obj.b = 5; obj.sum();
Private Access Modifier
The data members that are declared private are only accessible from the functions inside the class and not by any functions outside the class. Friend functions can also access the private data members of a class.
A program that demonstrates private access modifier is given as follows.
Example
#include<iostream> using namespace std; class Add { private: int a, b; public: void setdata(int x, int y) { a = x; b = y; } void sum() { cout<<"Sum of "<< a <<" and "<< b <<" is "<<a+b; } }; int main() { Add obj; obj.setdata(9,5); obj.sum(); return 0; }
Output
Sum of 9 and 5 is 14
In the above program, the class Add has two private data members a and b. The function setdata() provides the values of a and b as they are private variables. The function sum() displays the sum of a and b. This is seen below.
class Add { private: int a, b; public: void setdata(int x, int y) { a = x; b = y; } void sum() { cout<<"Sum of "<< a <<" and "<< b <<" is "<<a+b; } };
In the function main(), the object of class Add is created. Then the function setdata() is called to initialize a and b as they are private variables. Finally sum() is called that displays the sum of a and b. This is shown below.
Add obj; obj.setdata(9,5); obj.sum();
Protected Access Modifier
The data members of the class that are declared protected are similar to those declared private. They cannot be directly accessed outside the class but they can be accessed by the derived class of the base class.
A program that demonstrates protected access modifier in C++ is as follows −
Example
#include<iostream> using namespace std; class Parent { protected: int a, b; }; class Child: public Parent { public: void getdata(int x, int y) { a=x; b=y; } void putdata() { cout<<"a = "<<a<<endl; cout<<"b = "<<b<<endl; } }; int main() { Child obj; obj.getdata(9,1); obj.putdata(); return 0; }
Output
a = 9 b = 1
In the above program, the parent class has two protected variables a and b. This is shown below.
class Parent { protected: int a, b; };
The Child class has two functions getdata() and putdata() that take the values of a and b and display them respectively. These functions can do this as a and b are protected variables and Child is a derived class of Parent. The code snippet for this is given below −
class Child: public Parent { public: void getdata(int x, int y) { a = x; b = y; } void putdata() { cout<<"a = "<<a<<endl; cout<<"b = "<<b<<endl; } };
In the function main(), an object obj of Child class is created. Then the functions getdata() and putdata() are called. This is shown below.
Child obj; obj.getdata(9,1); obj.putdata();
- Related Articles
- What are access modifiers and non-access modifiers in Java?
- Access Modifiers in Java
- Access Modifiers in C#
- Access and Non Access Modifiers in Java
- What are the differences between access modifiers and non-access modifiers in Java?
- What are access modifiers in C++?
- Types of access modifiers in Java?
- Non Access Modifiers in Java\n
- method overriding with access modifiers in Java
- java access modifiers with method overriding
- How many non-access modifiers are there in Java?
- What are final, abstract, synchronized non-access modifiers in Java?
- Can access modifiers be used for local variables in Java?
- What are different types of access modifiers available in C#?
- Why cannot we specify access modifiers inside an interface in C#?
