Write a Class Named Employee with Constructors, Accessors & Mutators
In object-oriented programming, a class is a blueprint for creating objects
with specific data and behavior. In this exercise, we write a class named
Employee that holds basic employee details such as name, ID
number, department and position. We’ll show how to implement constructors,
mutator (setter) methods, and accessor (getter) methods.
As the annual inflation rate in Pakistan increased to 27.3% in August of
2022, the highest since May 1975, and figures could go even higher as deadly
floods hit the country.
Transport prices recorded the most significant increase (63.1%), and
housing and utilities (27.6%).
The (SWO) Staff welfare organization (attached department of Establishment
Division primarily set up to look after the welfare of Federal Government
employees FGE's) has launched a meeting and decided to increase Conveyance
Allowance and House Rental Allowance to 25% and 45% of the basic salary
respectively and reduce the tax to 2.5% of the basic salary. so that FGE's
(Federal Government employees) can manage their living needs.
Suppose you have been hired by a Federal Government Department to develop a
system for their Human Resource Department to calculate the salaries of the
employees as per SWO's above policy.
The user will enter Employee Id, Employee Name and basic salary of the
employee and the allowances will be calculated according to the basic salary
and the tax deduction. Then the Net Payable Salary will be calculated
according to the following formulation:
Net Payable Salary = Basic Salary + Conveyance Allowance + House Rental
Allowance – Tax
Conveyance Allowance = 35% of the Basic salary
House Rental Allowance = 50% of the Basic salary
Tax = 1.5% of the Basic salary
Implement C++ code for the following functionalities:
Write a class named Employee with the following attributes: Employee Id,
Employee Name, Employee Basic Salary, Conveyance Allowance, House Rental
Allowance, tax deduction and
Employee Gross Salary
Create an array of three objects using a new operator which will store the
data. Create a function for entering data of the employees as mentioned in
the Screenshot given below.
Calculate the salary of the employees according to the formulation
mentioned above.
Display the Employee Details using the Getter Functions in the main()
function
Program:
#include <iostream>
#include <string.h>
#include <conio.h>
using namespace std;
class Employee {
private:
string Employee_Name;
int Employee_Id;
int Employee_Basic_Salary;
int Employee_Gross_Salary;
int Conveyance_Allowance;
int House_Rental_Allowance;
int tax_deduction;
public:
void setEmployee_Id(int id);
void setEmployee_Basic_Salary(int
salaray);
void setEmployee_Name(string
name);
int getEmployee_Id();
int
getEmployee_Basic_Salary();
string getEmployee_Name();
int
getHouse_Rental_Allowance();
int getConveyance_Allowance();
int getTax_deduction();
int
getEmployee_Gross_Salary();
};
void Employee::setEmployee_Id(int id)
{
this->Employee_Id = id;
}
void Employee::setEmployee_Basic_Salary(int salaray)
{
this->Employee_Basic_Salary =
salaray;
}
void Employee::setEmployee_Name(string name)
{
this->Employee_Name = name;
}
int Employee::getEmployee_Id()
{
return this->Employee_Id;
}
int Employee::getEmployee_Basic_Salary()
{
return
this->Employee_Basic_Salary;
}
string Employee::getEmployee_Name()
{
return this->Employee_Name;
}
int Employee::getHouse_Rental_Allowance()
{
this->House_Rental_Allowance =
(50 * this->Employee_Basic_Salary) / 100;
return
this->House_Rental_Allowance;
}
int Employee::getConveyance_Allowance()
{
this->Conveyance_Allowance =
(35 * this->Employee_Basic_Salary) / 100;
return
this->Conveyance_Allowance;
}
int Employee::getTax_deduction()
{
this->tax_deduction = (1.5 *
this->Employee_Basic_Salary) / 100;
return this->tax_deduction;
}
int Employee::getEmployee_Gross_Salary()
{
this->Employee_Gross_Salary =
(this->Employee_Basic_Salary + this->Conveyance_Allowance +
this->House_Rental_Allowance - this->tax_deduction);
return
this->Employee_Gross_Salary;
}
int main()
{
Employee *emp = new
Employee[3];
int id, salary;
string name;
cout << "Assignmnet
Submitted by: <BS00000000>" << endl;
for (int i = 0; i < 3; i++)
{
cout <<
"________________________________________" << endl;
cout << "Enter Employee ID
:";
cin >> id;
emp[i].setEmployee_Id(id);
cout << "Enter Employee
name :";
cin >> name;
emp[i].setEmployee_Name(name);
cout << "Enter Employee
Basic Salary :";
cin >> salary;
emp[i].setEmployee_Basic_Salary(salary);
cout <<
"________________________________________" << endl;
}
for (int i = 0; i < 3; i++)
{
cout <<
"----------------------------------------------------" << endl;
cout << "Empployee
Id:"<< " " << emp[i].getEmployee_Id()<<endl;
cout << "Employee
Name:"<< " " << emp[i].getEmployee_Name()<<endl;
cout << "Employee Basic
Salary:" << " " <<
emp[i].getEmployee_Basic_Salary()<<endl;
cout << "Employee House
Rental Allowance:"<< " " <<
emp[i].getHouse_Rental_Allowance()<<endl;
cout << "Employee
Conveyance Allowance:" << " " <<
emp[i].getConveyance_Allowance()<<endl;
cout << "Employee Tax
Deduction:"<< " " << emp[i].getTax_deduction()<<endl;
cout << "Employee Gross
Salary:"<< " " <<
emp[i].getEmployee_Gross_Salary()<<endl;
cout <<
"----------------------------------------------------" << endl;
}
delete []emp;
return 0;
};
Output:
Related Topics
Frequently Asked Questions (FAQs)
1. Why do we use private fields in a class?
Private fields enforce encapsulation — protecting data and allowing
controlled access through methods. This is a core principle of
object-oriented programming.
2. What is a constructor?
A constructor initializes objects and is called automatically when the
object is created. It has the same name as the class.
3. Why overload constructors?
Overloading constructors allows creating objects with different levels of
provided data, increasing flexibility.
Explore more about OBJECT ORIENTED PROGRAMMING in
Computer Science Notes & MCQs.
Query Point Official – Smart Notes for Exams & Conceptual Learning
No comments