How to access private/protected method outside a class in C++
Last Updated :
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 class
class 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 Code
int main()
{
// Create object of child class
Child child(1019);
// Upcasting
Parent* obj = &child;
// Function call of child class
obj->disp();
return 0;
}
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
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 class
class 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 Code
int main()
{
// Create object of child class
Child child(1019);
// Upcasting
Parent* obj = &child;
// Function call of child class
obj->disp();
return 0;
}
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.
Similar Reads
How to Override a Base Class Method in C++? In C++, method overriding allows us to redefine a function from a parent class (base class) in its child class (derived class) to modify its behavior for objects of the child class. In this article, we will learn how to override a base class method in the derived class in C++. Override Inherited Met
2 min read
How to Create a Class with Private and Public Members in C++? In C++, the classes are blueprints for creating objects with specific properties and methods that provide a feature of access specifiers to the user through which they can control the access of the data members present in a class. In this article, we will learn how to create a class with private and
3 min read
How to Override a Base Class Method in a Derived Class in C++? Function overriding is a concept in object-oriented programming languages, in which a function/method of a parent class is redefined in its child class to change its behavior for the objects of the child class. In this article, we are going to learn how to override a base class function in a derived
2 min read
Can We Access Local Variable's Memory Outside its Scope? In C++, local variables are those variables that are declared inside a function or a block. Generally, we cannot access these variables outside its scope using the variable name but there are some exploits that we can use to do that. In this article, we will see how to access a local variable memory
2 min read
How to Access Private Variable in C++? In C++, Private members of a class are accessible only within the class they are declared and by friend functions, they are not accessible outside the class not even by derived classes. In this article, we will learn how to access private variables in C++. Accessing Private Variables From Class in C
2 min read