Employee Class in C++ for Salary Calculation
This C++ snippet defines an Employee class that manages employee attributes such as name, number, position, marital status, daily wage, and overtime details. It includes methods to calculate overtime pay and gross salary, demonstrating basic class structure and member functions in C++.#include <iostream> #include <string> #include <cmath> #include <iomanip> class Employee { private: std::string employeeName; int employeeNo; std::string employeePosition; std::string maritalStatus; float dailyWage; float overTimeRate; float overTimeHours; public: Employee(std::string employeeName, int employeeNo, std::string employeePosition, std::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 std::round(overTimeHours * overTimeRate * 100) / 100.0; } float calculateGrossSalary() { return std::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 std::round((calculateGrossSalary() - calculateTotalDeduction()) * 100) / 100; } void displayData() { std::cout << "Employee Name: " << employeeName << std::endl; std::cout << "Employee No: " << employeeNo << std::endl; std::cout << "Employee Position: " << employeePosition << std::endl; std::cout << "Tax Code: " << maritalStatus << std::endl; std::cout << "Basic Salary: " << (dailyWage * 31) << std::endl; std::cout << "Allowance: " << 2000 << std::endl; std::cout << "Overtime: " << calculateOverTime() << std::endl; std::cout << "Gross Compensation: " << calculateGrossSalary() << std::endl; std::cout << "Total Deduction: " << calculateTotalDeduction() << std::endl; std::cout << "Netpay: " << calculateNetpay() << std::endl; } }; int main() { Employee e1("John", 1234, "Manager", "Married", 1000, 200, 4); e1.displayData(); return 0; }
Leave a Comment