How to access private/protected method outside a class in C++

05 Aug, 2021
Prerequisites: Access Modifiers in C++, Runtime Polymorphism
Private: The class members declared as private can be accessed only by the functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functions are allowed to access the private data members of a class.
We can access private method in other class using the virtual function, A virtual function is a member function which is declared within a base class and is re-defined (Overridden) by a derived class. Referring to a derived class object using a pointer or a reference to the base class can call a virtual function for that object and execute the derived class’s version of the function.
Program 1: To demonstrate the private access modifier 
C++
// C++ program to demonstrate private// access modifier#include <iostream>using namespace std; // Parent class having virtual// function disp()class Parent {public:    // Create virtual function    virtual void disp()    {        cout << "This is the public disp"             << " method of Parent class" << endl;    }}; // Child class inherit to parent classclass Child : public Parent {private:    int secret_key;     // Private method which will be called    // Override the method of parent class    void disp()    {        cout << "This is the private disp "             << "method of child class "             << endl;        cout << "The key is "             << secret_key << endl;    } public:    // Constructor of the child class    Child(int key) { secret_key = key; }}; // Driver Codeint main(){    // Create object of child class    Child child(1019);     // Upcasting    Parent* obj = &child;     // Function call of child class    obj->disp();    return 0;}
This is the private disp method of child class 
The key is 1019
 
Explanation:
In the above program, parent class has a function void disp() which is a virtual function. The child class has created another function with the same name i.e., void disp() but that function is private which means it is not allowed to be accessed directly by any object or function outside the class. In main() we are first creating an object of child class and passing a parameter to initialize the secret_key variable in the child class, After that, we are storing the address of the object of child class in a base class pointer this is also called upcasting. Now the base class pointer holds the address of child class object, but when calling a function using base class pointer it will call base class functions only. But if want it to call child class functions to make the base class functions virtual. This is also called as runtime polymorphism.
Protected: Protected access modifier is similar to that of private access modifiers, the difference is that the class member declared as Protected are inaccessible outside the class, but they can be accessed by any subclass(derived class) of that class.
Program 2: To demonstrate protected access modifier 
C++
// C++ program to demonstrate protected// access modifier#include <iostream>using namespace std; // Parent class having virtual// function disp()class Parent {public:    // Create virtual function    virtual void disp()    {        cout << "This is the public disp"             << " method of Parent class"             << endl;    }}; // Child class inherit to parent classclass Child : public Parent {protected:    int secret_key;     // Private method which will be called    // Override the method of parent class    void disp()    {        cout << "This is the protected  disp "             << "method of child class "             << endl;        cout << "The key is "             << secret_key << endl;    } public:    // Constructor of child class    Child(int key) { secret_key = key; }}; // Driver Codeint main(){    // Create object of child class    Child child(1019);     // Upcasting    Parent* obj = &child;     // Function call of child class    obj->disp();    return 0;}
This is the protected  disp method of child class 
The key is 1019
 
Explanation: 
In the above example, the parent class has function void disp() which is a virtual function. The child class has created another function with the same name i.e., void disp() but that function is private which means it is not allowed to be accessed directly by any object or function outside the class. In main() we are first creating an object of child class and passing a parameter to initialize the secret_key variable in the child class, after that we are storing the address of the object of child class in a base class pointer this is also called upcasting. Now the base class pointer holds the address of child class object, but when calling a function using base class pointer it will call base class functions only. But if want it to call child class functions to make the base class functions virtual. This is also called as Runtime Polymorphism. 
sooda367
access modifiers
C++-Class and Object
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Sorting a vector in C++
Polymorphism in C++
std::string class in C++
Friend class and function in C++
Pair in C++ Standard Template Library (STL)
Queue in C++ Standard Template Library (STL)
Unordered Sets in C++ Standard Template Library
List in C++ Standard Template Library (STL)
std::find in C++
Inline Functions in C++