EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Access Specifiers in C++

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » C ++ Programming Tutorial » Access Specifiers in C++

Access Specifiers in C++

Introduction to Access Specifiers in C++

Access specifiers in C++ are basically used in OOPs concepts. In classes, we start their use, they are mainly used in inheritance. They set the range for the usage of the variable and the functions of a particular class. Access specifiers are used for data hiding purposes also.

What is Access Specifiers in C++?

Here we will discuss some basic concept of access specifier in C++ that you should know:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • The access specifiers used in C++ are Private, Protected and Public. The data members and member functions of a class declared as public are available to everyone and other classes can also access them. The public members of a class are accessible from everywhere in the program using the dot operator (.) which is called a direct member access operator along with the object of the same class.
  • The data members and member functions of a class declared as private are accessible only by the inside functions of the class and are restricted to be accessed directly by any object or function outside the class. The private data members of a class can be accessed using the member functions of the class or the friend functions.
  • Protected access modifier is similar to that of private access modifiers, the difference is that the class member declared as Protected cannot be accessed outside the class however they are accessible by any derived class or subclass of that class.
  • The main difference between private and protected is that protected inheritance allows continued access to base class members whereas private inheritance prohibits the access of those members.

How Does Access Specifiers Work in C++?

The accessibility of access specifiers in classes during inheritance is shown in Table.

Access Specifiers Private Protected Public
Access in same class √ √ √
Access in derived class × √ √
Access outside the class × × √

Working of access specifiers in inheritance is as discussed below:

1. Private

While creating a class using access specifier as private, the base class’ public and protected data members become the derived class’ private member and base class’ private member stays private. Hence, the members of the base class can be used only inside the derived class but are inaccessible through the object created for the derived class. The other way to access them is to create a function in the derived class. The below figure depicts the inheritance of data members of the base class when the access mode of the derived class is private.

Private

2. Protected

In derived class, when protected access specifier is used, the public and protected data members of the base class becomes the derived class’ protected member and base class’ private member are not accessible. Hence, the members of the base class can be used only inside the derived class as protected members. The below figure depicts the inheritance of data members of the base class when the access mode of the derived class is protected.

Protected

3. Public

While derive class is created, if public access specifier is used, the public data members of the base class become the public member of the derived class and protected members become the protected in the derived class but the private members of the base class are inaccessible. The following figure depicts the inheritance of data members of the base class when the access mode of the derived class is public.

Popular Course in this category
C++ Training (4 Courses, 5 Projects, 4 Quizzes)4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion | Lifetime Access | 4 Quizzes with Solutions
4.5 (5,233 ratings)
Course Price

View Course

Related Courses
Java Training (40 Courses, 29 Projects, 4 Quizzes)C Programming Training (3 Courses, 5 Project)

Public

Examples of Access Specifiers in C++

Here are some of the examples of access modifiers in C++ which are explained below:

Example #1: Private

This is the example of a private modifier in C++.

Code:

#include<iostream>
using namespace std;
class baseclass
{
private:
int s;
protected:
int t;
public:
int u;
baseclass()
{
s = 11;
t = 12;
u = 13;
}
};
class deriveclass: private baseclass
{
//t and u becomes private members of deriveclass and s will remain private
public:
void show ()
{
cout << "s is not accessible";
cout << "\nt is " << t;
cout << "\nu is " << u;
}
};
int main()
{
deriveclass l; //object created
l.show();
//l.s = 11; not valid : private members are inaccessible outside the class
//l.t = 12; not valid
//l.u = 13; not valid : t and u have become derived class’ private members
return 0;
}

Output:

private

Example #2: Protected

This is the example of a protected modifier in C++.

Code:

#include<iostream>
using namespace std;
class baseclass
{
private:
int a;
protected:
int b;
public:
int c;
baseclass()
{
a = 10;
b = 11;
c = 12;
}
};
class deriveclass: protected baseclass
{
//b and c becomes protected members of deriveclass
public:
void show ()
{
cout << "a is not accessible";
cout << "\nb is " << b;
cout << "\nc is " << c;
}
};
int main()
{
deriveclass d; // object created
d.show();
//d.a = 10; not valid : private members are inaccessible outside the class
//d.b = 11; not valid
//d.c = 12; not valid : b and c have become derived class’ private member
return 0;
}

Output:

Access Specifiers in C++ protected

Example #3: Public

This is the example of a public modifier in C++.

Code:

#include<iostream>
using namespace std;
class baseclass
{
private:
int u;
protected:
int v;
public:
int w;
baseclass()
{
u = 3;
v = 4;
w = 5;
}
};
class deriveclass: public baseclass
{
//v becomes protected and w becomes public members of class derive
public:
void show()
{
cout << "u is not accessible";
cout << "\nvalue of v is " << v;
cout << "]\nvalue of w is " << w;
}
};
int main()
{
deriveclass c;
c.show();
//c.u = 3; not valid: private members are inaccessible outside the class
//c.v = 4; not valid: v is now protected member of derived class
//c.w = 5; valid: w is now a public member of derived class
return 0;
}

Output:

Access Specifiers in C++ public

Conclusion

Whenever data needs any kind of restriction of accessibility that can be set as private or protected so that only the authorized functions can access them. Otherwise, the data can be set as public which can be accessed from anywhere in the program by any class in inheritance.

Recommended Articles

This is a guide to Access Specifiers in C++. Here we discuss what is access specifiers in C++ its working along with their examples and implementation. You may also look at the following articles to learn more-

  1. Features of C++
  2. C++ Keywords
  3. Rust vs C++
  4. Palindrome in C++

C++ Training (4 Courses, 3 Projects, 4 Quizzes)

4 Online Courses

5 Hands-on Projects

37+ Hours

Verifiable Certificate of Completion

Lifetime Access

4 Quizzes with Solutions

Learn More

1 Shares
Share
Tweet
Share
Primary Sidebar
C plus plus Programming Tutorial
  • Advanced
    • C++ namespace
    • Encapsulation in C++
    • Access Modifiers in C++
    • Abstract Class in C++
    • C++ Class and Object
    • What is Template Class in C++?
    • C++ Algorithm
    • Data Structures and Algorithms C++
    • C++ Garbage Collection
    • Virtual Keyword in C++
    • Access Specifiers in C++
    • Storage Class in C++
    • Call by Value in C++
    • Multimap in C++
    • C++ Multiset
    • C++ Lambda Expressions
    • Stack in C++
    • C++ Static
    • C++ static_cast
    • Deque in C++
    • C++ Vector Functions
    • C++ 2D Vector
    • C++ List
    • C++ Mutable
    • Enum in C++
    • Abstraction in C++
    • Signal in C++
    • C++ Queue
    • Priority Queue in C++
    • Regular Expressions in C++
    • C++ Hash Table
    • C++ Expression
    • File Handling in C++
    • C++ Stream
    • ifstream in C++
    • C++ ofstream
    • C++ fstream
    • C++ Read File
    • C++ iomanip
    • Macros in C++
    • Templates in C++
    • C++ setprecision
    • C++ Int to String
    • C++ thread( )
    • C++ Thread Pool
    • C++ thread_local
  • Basic
    • Introduction To C++
    • What is C++
    • Features of C++
    • Applications of C++
    • Best C++ Compiler
    • C++ Data Types
    • C++ Double
    • C++ unsigned int
    • User Defined Data Types in C++
    • Variables in C++
    • C++ Keywords
    • Pointers in C++
    • C++ Void Pointer
    • Function Pointer in C++
    • Iterator in C++
    • C++ Commands
    • Object in C++
    • C++ Literals
    • C++ Reference
    • C++ Undefined Reference
    • String in C++
    • C++ Programming Language (Basics)
    • C++ Identifiers
    • C++ Header Files
    • Type Casting in C++
    • C++ Formatter
  • Operators
    • C++ Operators
    • Arithmetic Operators in C++
    • Assignment Operators in C++
    • Bitwise Operators in C++
    • Relational Operators in C++
    • Boolean Operators in C++
    • Unary Operators in C++
    • C++ Operator[]
    • Operator Precedence in C++
    • C++ operator=()
  • Control Statements
    • Control Statement in C++
    • if else Statement in C++
    • Else If in C++
    • Nested if in C++
    • Continue Statement in C++
    • Break Statement in C++
    • Switch Statement in C++
    • goto Statement in C++
    • C++ Struct
    • Loops in C++
    • Do While Loop in C++
    • Nested Loop in C++
  • Functions
    • C++ getline()
    • C++ String Functions
    • Math Functions in C++
    • Friend Function in C++
    • Recursive Function in C++
    • Virtual Functions in C++
    • strcat() in C++
    • swap() in C++
    • strcmp() in C++
    • ceil function in C++
    • C++ begin()
    • size() in C++
    • C++ test()
    • C++ any()
    • C++ Bitset
    • C++ find()
    • C++?Aggregation
    • C++?String append
    • C++ String Copy
    • C++ end()
    • C++ endl
    • C++ push_back
    • C++ shuffle()
    • malloc() in C++
    • C++ reserve()
    • C++ unique()
    • C++ sort()
    • C++ find_if()
    • Reflection in C++
    • C++ replace()
    • C++ search()
    • C++ Memset
    • C++ size_t
    • C++ Substring
    • C++ Max
    • C++ absolute value
    • C++ memcpy
    • C++ wchar_t
    • C++ free()
    • C++ pair
    • C++ this
    • C++ sizeof()
    • C++ Move Semantics
  • Array
    • Arrays in C++
    • 2D Arrays in C++
    • 3D Arrays in C++
    • Multi-Dimensional Arrays in C++
    • C++ Array Functions
    • String Array in C++
    • C++ Length of Array
    • C++ arraylist
  • Constuctor and Destructor
    • Constructor and Destructor in C++
    • Constructor in C++
    • Destructor in C++
    • Copy Constructor in C++
    • Parameterized Constructor in C++
  • Overloading and overriding
    • Overloading and Overriding in C++
    • Overloading in C++
    • Overriding in C++
    • Function Overloading in C++
    • Function Overriding in C++
    • Method Overloading in C++
  • Inhertiance
    • Types of Inheritance in C++
    • Single Inheritance in C++
    • Multiple Inheritance in C++
    • Hierarchical Inheritance in C++
    • Multilevel Inheritance in C++
    • Hybrid Inheritance in C++
  • Sorting
    • Sorting in C++ 
    • Heap Sort in C++
    • C++ Vector Sort
    • Insertion Sort in C++
    • Selection Sort in C++
    • C++ QuickSort
    • Sort string C++
  • Programs
    • Patterns in C++
    • Star Patterns In c++
    • Swapping in C++
    • Reverse Number in C++
    • Palindrome Program in C++
    • Palindrome in C++
    • Factorial Program in C++
    • Fibonacci Series in C++
    • Square Root in C++
    • Random Number Generator in C++
    • Prime Number in C++
    • Leap Year Program in C++
    • Anagram in C++
    • Armstrong Number in C++
    • Reverse String in C++
    • Socket Programming in C++
    • Matrix Multiplication in C++
    • C++ using vs typedef
    • C++ vector vs list
    • C++ vector vs array
  • Interview question
    • C++ Interview Questions
    • Multithreading Interview Questions C++

Related Courses

C++ Training Course

Java Training Course

C Programming Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C++ Training (4 Courses, 3 Projects, 4 Quizzes) Learn More