
- 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
Difference between private, public, and protected modifiers in C++
Data hiding is one of the important features of Object Oriented Programming which allows preventing the functions of a program to access directly the internal representation of a class type. The access restriction to the class members is specified by the labeled access modifiers − public, private, and protected sections within the class body.
The default access for members and classes is private.
Example
class Base { public: // public members go here protected: // protected members go here private: // private members go here };
A public member is accessible from anywhere outside the class but within a program. You can set and get the value of public variables without any member.
A private member variable or function cannot be accessed, or even viewed from outside the class. Only the class and friend functions can access private members.
A protected member variable or function is very similar to a private member but it provided one additional benefit that they can be accessed in child classes which are called derived classes.
- Related Articles
- Difference between private, public, and protected inheritance in C++
- What are private, public, default and protected access Java modifiers?
- Difference Between Private and Protected in C++
- What are the differences between public, protected and private access specifiers in C#?
- Difference between Private Key and Public Key
- Difference between Private and Public IP addresses
- Difference between Private Hospitals and Public Hospitals
- What is difference between internal and private modifiers in C#?
- Differences between Private Law and Public Law
- Differences between Private Cloud and Public Cloud
- Can we declare an abstract method, private, protected, public or default in java?
- Private and Protected Members in C++
- Difference Between Presentation Skills and Public Speaking
- Difference between Private Cloud and Data Center
- Replacing ‘public’ with ‘private’ in “main” in Java
