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

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 

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 class
class Child : public Parent {
private:
    int secret_key;
  
    // Private method which will be called
    // Overrride 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 Code
int main()
{
    // Create object of child class
    Child child(1019);
  
    // Upcasting
    Parent* obj = &child;
  
    // Function call of child class
    obj->disp();
    return 0;
}
chevron_right

Output:

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 

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 class
class Child : public Parent {
protected:
    int secret_key;
  
    // Private method which will be called
    // Overrride 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 Code
int main()
{
    // Create object of child class
    Child child(1019);
  
    // Upcasting
    Parent* obj = &child;
  
    // Function call of child class
    obj->disp();
    return 0;
}
chevron_right

Output:
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.

Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.





Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
C++
Practice Tags :