C++ program (Code Snippet) – Access private member functions inside public member function in C++ class
Let’s consider the following example:
/*C++ program to demonstrate calling of
private member functions inside public member function*/
#include <iostream>
using namespace std;
//class definition
class A{
private:
int a;
int b;
//set value of a
void set_a(int a){
this->a=a;
}
//set value of b
void set_b(int b){
this->b=b;
}
public:
void getValues(int x,int y){
set_a(x); //calling private member function
set_b(y); //calling private member function
}
void putValues(){
//printing values of private data members a,b
cout<<"a="<<a<<",b="<<b<<endl;
}
};
int main(){
//creating object
A objA;
//set values to class data members
objA.getValues(100,200);
//print values
objA.putValues();
return 0;
}
C++ program (Code Snippet) – Access private member functions inside public member function in C++ class
Let’s consider the following example:
Output