Program To Calculate Gross Salary Of An Employee By Demonstrating How The Private Member Functions Can Be Accessed From Within Its Class In C++

LEVEL: Beginner

Whats in this post?

  1. Code
  2. Output
  3. Background and Explanation of the code


To copy code: Click View Source “View Source” on the right of this code snippet.

Background and Explanation of the code

The above program is used to calculate the gross salary of an employee whose basic pay is known by using the formula

Gross salary = basic pay + hra + da.

We assume here that hra = 15% of basic pay and da = 50% of basic pay.

The class employee contains two private data members and two private member functions cal_da ( ) and cal_hra ( ).These private member functions are not accessible from outside the class and acts as utility functions for computing gross salary.The public member function read ( ) inputs the name and basic pay of the employee .The public member function display_salary ( ) calculates and display the gross salary by calling private member functions of the class within itself.


Enter employee name = Rahul

Enter basic pay of employee =  3000

Gross salary of the employee =  4950