Access the private Members Functions of a class In C++


Private members can be accessed from within the class members only.Eg:
The private items of a family can be used by its members only.

C++ Program How to Access the private members function of a class

#include<iostream> using namespace std; class MyClass{ void kbc(){ cout<<"Welcome in kbc"<<endl; } public: void bbc(){ kbc(); cout<<"Welcome in bbc"<<endl; } }; int main(){ MyClass s; s.bbc(); return 0; }

Output:-

Welcome in kbc
Welcome in bbc

Explanation of the program

Here private member function kbc() cannot be accessed outside the class. Hence if the class object 's' tries to access kbc() then an error is obtained. It is accessed by another member function bbc() of the same class. In this case, we do not obtain any error.