
- 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 and Protected in C++
In this post, we will understand the difference between private and protected access modifiers in C++.
Private Access Modifier
They are declared using the ‘private’ keyword, followed by a ‘:’.
They can’t be accessed outside the class.
The ‘private’ keyword is an access modifier that ensures that the functions and attributes inside the class are accessed only by class members in which they have been declared.
Only member functions or friend functions are allowed to access data that is labelled as ‘private’.
Example
#include <iostream> using namespace std; class base_class{ private: string my_name; int my_age; public: void getName(){ cout << "Enter the name... "; cin >> my_name; cout << "Enter the age... "; cin >> my_age; } void printIt(){ cout << "The name is : " << my_name << endl; cout << "The age is: " << my_age << endl; } }; int main(){ cout<<"An object of class is created"<< endl; base_class my_instance; my_instance.getName(); my_instance.printIt(); return 0; }
Output
/tmp/u5NtWSnX5A.o An object of class is created Enter the name... Jane Enter the age... 34 The name is : Jane The age is: 34
Protected Access Modifier
Protected access modifier is similar to private access modifier.
They are declared using the ‘protected’ keyword, followed by ‘:’.
Class member that is declared as ‘Protected’ can’t be accessed outside the class.
They can be accessed within the class in which they are declared.
They can also be accessed by a derived class whose parent class contains ‘protected’ members.
They are used while working with inheritance concept.
Example
#include <iostream> using namespace std; class base_class{ private: string my_name; int my_age; protected: int my_salary; public: void getName(){ cout << "Enter the name... "; cin >> my_name; cout << "Enter the age... "; cin >> my_age; } void printIt(){ cout << "The name is : " << my_name << endl; cout << "The age is: " << my_age << endl; } }; class derived_class : public base_class{ private: string my_city; public: void set_salary(int val){ my_salary = val; } void get_data_1(){ getName(); cout << "Enter the city... "; cin >> my_city; } void print_it_1(){ cout << "The salary is: " << my_salary << endl; printIt(); cout << "The city is: " << my_city << endl; } }; int main(){ cout<<"Instance of derived class is being created.."<<endl; derived_class my_instance_2 ; my_instance_2.set_salary(100); my_instance_2.get_data_1(); my_instance_2.print_it_1(); return 0; }
Output
/tmp/u5NtWSnX5A.o Instance of derived class is being created.. Enter the name... Jane Enter the age... 23 Enter the city... NewYork The salary is: 100 The name is : Jane The age is: 23 The city is: NewYork
- Related Articles
- Difference between private, public, and protected modifiers in C++
- Difference between private, public, and protected inheritance in C++
- Private and Protected Members in C++
- What are the differences between public, protected and private access specifiers in C#?
- Access Modifiers in Python : Public, Private and Protected
- What is difference between internal and private modifiers in C#?
- What are private, public, default and protected access Java modifiers?
- Difference between Private and Public IP addresses
- Difference between Private Key and Public Key
- Difference between Private Cloud and Data Center
- Difference between Private Hospitals and Public Hospitals
- Difference Between Virtual Private Network (VPN) and Proxy
- Can we declare the variables of a Java interface private and protected?
- Can we declare interface members as private or protected in java8?
- Public vs Protected vs Package vs Private Access Modifiers in Java
