Home >>Cpp Programs >C++ protected keyword

C++ protected keyword

C++ Protected

The C++ protected keyword generally specifies the access to a class members in the member-list up to the next access specifier that can be public or private or the end of the class definition in the C++ programming language. Please note that the CPP protected class members that are declared as protected can only be used by the following elements that are depicted below:

  • Member functions that are of the class that are originally used to declare these members.
  • Friends of the class that are originally has been declared these members.
  • Classes that are derived with public or protected access from the class that have generally originally declared these members.
  • Direct privately derived classes that also possesses private access to the verified protected members.

The C++ protected keyword generally specifies that the public and the protected members of the base class are generally the protected members of its derived classes, this happens in the case where preceding is done by the name of a base class.

Please note that the protected members are not as private as the private members that are basically accessible only to the members of the class in which they have been declared but doing this doesn’t make as public as the public members that are generally accessible in any of the function.

Protected members that are basically known to be accessible to any of the friend or member function that are of a derived class in C++ programming language and they are also declared as static. Protected members that are basically known to be not getting declared as the static that are basically being accessible to the friends and member functions in a derived class that too only through a reference to, pointer to, or object of the derived class.

Syntax

protected:
   [member-list]
protected base-class

Here is an example of the C++ protected keyword that will help you understand the basic concept of it and get you the physical aspect of its use:

#include  
using namespace std; 
class Base {
   protected : 
   int x = 10;    
};
class Child : public Base 
{ 
   public :
   void func() 
   {
      cout << "value of x = " << x;
   }
};
int main() {
   Child obj;
   obj.func(); 
   return 0;
}
Output : value of x=10

Cpp Programs Fibonacci Series in C++ Armstrong Number in C++ Factorial program in C++ Check Palindrome in C++ Prime Number Program In C++ Reverse Number Program in C++ Sum Of Digits Program in C++ C++ Find C++ protected keyword CPP Program for different ways to print array elements CPP Program to determine the colour of chess square CPP Program to Reverse Number CPP Program to Calculate Power of a Number CPP Program to print all Even and Odd numbers from 1 to N Program to find whether a no is power of two in CPP C++ program to find largest list of prime numbers Auto keyword in Cpp C++ program to print the left Rotation of the array Convert a given binary Tree to Doubly Linked List Delete keys in a Linked list using C++ program How do you delete a linked list in C++ Implement stack with linked list in c++ C++ Program to find first occurrence of a Number Using Recursion in an array C++ program to find Last occurrence of a Number using Recursion in an array C++ Program to find Union of two singly Linked Lists Remove Duplicates from linked list in c++ C++ Program to find Nth node in Linked List Merge sort singly linked list c++ C++ Program to Convert Roman Number to Integer Number C++ Program to find LCM of two numbers C++ Password Generator C++ Program to multiply two numbers without using multiplication operator C++ Program to print all possible subset of a set Sum of all the elements in an array divisible by a given number Print First uppercase letter in a string c++ C++ Program to Find the Number Occurring Odd Number of Times C++ Program for Print address of Variable Using Pointer C++ Program to Find ASCII Value of a Character C++ Classes and Objects Program Create a class method in C++ C++ Create Empty Class Define a class method outside the class definition in C++ How to create multiple objects of a class in C++