i2tutorials

CPP – Private member functions

Private member functions

 

A member function that is declared in a private section of a class is known as a private member function. A private function may only be called by another function belonging to its class. Even an object may not invoke a private function with the help of the dot operator from the outside.

 

Example Program:

#include <iostream>
using namespace std;
class maximum
{
int a,b;
int max();//private member function
public:
void read();
void print();
};
void maximum::read()
{
cout<<"\n enter a and b: ";
cin>>a>>b;
}
void maximum::print()
{
cout<<"value of a: "<<a;
cout<<"\nvalue of b: "<<b;
cout<<"\nmaximum value is : "<<max();
}
int maximum::max()
{
if(a>b)
return a;
return b;
}
int main()
{
maximum A;
A.read();
A.print();
return 0;
}

 

Output:

Exit mobile version