Employee Class Implementation in C++
This code snippet defines an Employee class in C++. It includes member variables for employee details and methods to calculate overtime and gross salary. The constructor initializes the employee's attributes, and methods return the calculated values based on the provided inputs.unknown
c_cpp
15 days ago
2.3 kB
24
Indexable
Never
#include <iostream> #include <string> #include <cmath> #include <iomanip> using namespace std; class Employee { private: string employeeName; int employeeNo; string employeePosition; string maritalStatus; float dailyWage; float overTimeRate; float overTimeHours; public: Employee(string employeeName, int employeeNo, string employeePosition, string maritalStatus, float dailyWage, float overTimeRate, float overTimeHours) { this->employeeName = employeeName; this->employeeNo = employeeNo; this->employeePosition = employeePosition; this->maritalStatus = maritalStatus; this->dailyWage = dailyWage; this->overTimeRate = overTimeRate; this->overTimeHours = overTimeHours; } float calculateOverTime() { return round(overTimeHours * overTimeRate * 100) / 100.0; } float calculateGrossSalary() { return round((dailyWage * 31) + 2000 + calculateOverTime()); } float calculateTotalDeduction() { float tax; if (maritalStatus == "single") { tax = 0; } else { tax = 0.10f * calculateGrossSalary(); } float sss = 0.02f * calculateGrossSalary(); float medicare = 0.01f * calculateGrossSalary(); float pagIbig = 0.05f * calculateGrossSalary(); return sss + medicare + pagIbig + tax; } float calculateNetpay() { return round((calculateGrossSalary() - calculateTotalDeduction()) * 100) / 100; } void displayData() { cout << "Employee Name: " << employeeName << endl; cout << "Employee No: " << employeeNo << endl; cout << "Employee Position: " << employeePosition << endl; cout << "Tax Code: " << maritalStatus << endl; cout << "Basic Salary: " << (dailyWage * 31) << endl; cout << "Allowance: " << 2000 << endl; cout << "Overtime: " << calculateOverTime() << endl; cout << "Gross Compensation: " << calculateGrossSalary() << endl; cout << "Total Deduction: " << calculateTotalDeduction() << endl; cout << "Netpay: " << calculateNetpay() << endl; } }; int main() { Employee e1("John", 1234, "Manager", "Married", 1000, 200, 4); e1.displayData(); return 0; }
Leave a Comment