
- 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
Private and Protected Members in C++
A class in C++ has public, private and protected sections which contain the corresponding class members.
The private data members cannot be accessed from outside the class. They can only be accessed by class or friend functions. All the class members are private by default.
The protected members in a class are similar to private members but they can be accessed by derived classes or child classes while private members cannot.
A program that demonstrates private and protected members in a class is given as follows −
Example
#include <iostream> using namespace std; class Base { public : int a = 8; protected : int b = 10; private : int c = 20; }; class Derived : public Base { public : void func() { cout << "The value of a : " << a; cout << "\nThe value of b : " << b; } }; int main() { Derived obj; obj.func(); return 0; }
Output
The output of the above program is as follows.
The value of a : 8 The value of b : 10
Now, let us understand the above program.
In the class Base, the data members are a, b and c which are public, protected and private respectively. The code snippet for this is given as follows.
class Base { public : int a = 8; protected : int b = 10; private : int c = 20; };
The class Derived inherits the class Base. The function func() prints the values of a and b. It cannot print the value of c as that is private to class Base and cannot be accessed in class Derived. The code snippet for this is given as follows.
class Derived : public Base { public : void func() { cout << "The value of a : " << a; cout << "\nThe value of b : " << b; } };
In the function main(), the object obj of class Derived is created. Then the function func() is called. The code snippet for this is given as follows.
int main() { Derived obj; obj.func(); return 0; }
- Related Articles
- Can we declare interface members as private or protected in java8?
- Difference Between Private and Protected in C++
- Difference between private, public, and protected modifiers in C++
- Difference between private, public, and protected inheritance in C++
- Accessing protected members in a C++ derived class
- What are private, public, default and protected access Java modifiers?
- How to initialize private static members in C++?
- What are the differences between public, protected and private access specifiers in C#?
- Can we declare the variables of a Java interface private and protected?
- How to share private members among common instances in JavaScript?
- C++ Program to Access private members of a class
- Can we declare an abstract method, private, protected, public or default in java?
- Can we declare a top level class as protected or private in Java?\n
- Can we declare main() method as private or protected or with no access modifier in java?
- WiFi Protected Access (WPA) and WiFi Protected Access 2 (WPA2)
