Accessing private member of a class using member function
#include <iostream>
using namespace std;
class employee
{
private: //Private section starts
int empid;
float esalary;
public: //Public section starts
void display() //Member function
{
empid = 001;
esalary = 10000;
cout<<"Employee Id is : "<<empid<<endl;
cout<<"Employee Salary is : "<<esalary<<endl;
}
};
int main()
{
employee e; //Object Declaration
e.display(); //Calling to member function
return 0;
}
Accessing private member function using public member function
#include <iostream>
using namespace std;
class employee
{
private: //Private section starts
int empid;
float esalary;
void addvalues() //Private member function
{
empid = 001;
esalary = 10000;
}
public: //Public section starts
void display() //Public member function
{
addvalues(); //Calling to private member function
cout<<"Employee Id is : "<<empid<<endl;
cout<<"Employee Salary is : "<<esalary<<endl;
}
};
int main()
{
employee e; //Object Declaration
e.display(); //Calling to public member function
//e.addvalues(); cannot be accessible
return 0;
}
#include <iostream>
using namespace std;
class employee
{
private: //Private section starts
int empid;
float esalary;
public: //Public section starts
void display(void); //Prototype Declaration
}; //End of class
void employee :: display() //Function Definition Outside the Class
{
empid = 001;
esalary = 10000;
cout<<"Employee Id is : "<<empid<<endl;
cout<<"Employee Salary is : "<<esalary<<endl;
}
int main()
{
employee e; //Object Declaration
e.display(); //Calling to public member function
return 0;
}