
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Access Protected Members in C++ Derived Class
A class in C++ has the following access modifiers: public, private, and protected, which contain the corresponding class members. The protected members in a class are similar to private members as they cannot be accessed from outside the class, but they can be accessed by derived classes or child classes, while private members cannot.
In this article, we will see various examples of how to access protected members in C++ and how it is different from private members.
Accessing Protected Variable in C++
In this example, we have initialized a protected variable value in the parent class. We are accessing this variable in the Child class.
#include <iostream> using namespace std; class Parent { protected: int value = 42; //Protected member variable }; class Child : public Parent { public: void fun() { // Accessing protected member cout << "Protected Variable value: " << value << endl; } }; int main() { Child obj; obj.fun(); return 0; }
The output of the above code is as follows:
Protected Variable value: 42
Accessing Protected Function in C++
In this example, we have defined a protected function, msg(), in place of a variable. We are accessing this function in the child class using the function fun.
#include <iostream> using namespace std; class Parent { protected: void msg() { cout << "Protected function from Parent class called!" << endl; } }; class Child : public Parent { public: void fun() { msg(); // Direct access to protected function } }; int main() { Child obj; obj.fun(); return 0; }
The output of the above code is as follows:
Protected function from Parent class called!
Accessing Private and Protected Member in C++
To show the differences between the private and protected classes, we have defined a private and protected variable in the parent class.
Example
The following example explains the difference between private and protected classes as we access them using a child class. The result() function displays only the protected variable when accessed using the child class. The private variable will show an error and hence is commented.
#include <iostream> using namespace std; class Parent { private: int privateVariable = 10; protected: int protectedVariable = 20; public: void fun() { cout << "Private Variable in parent class: " << privateVariable << endl; cout << "Protected Variable in Parent class: " << protectedVariable << endl; } }; class Child : public Parent { public: void result() { // cout << "Private Variable in child class: " // << privateVariable << endl; // This will give an Error cout << "Protected Variable in child class: " << protectedVariable << endl; } }; int main() { Child obj; obj.result(); obj.fun(); return 0; }
The output of the above code is as follows:
Protected Variable in child class: 20 Private Variable in parent class: 10 Protected Variable in Parent class: 20