Create class for Employee - C++ Class

C++ examples for Class:Class Creation

Description

Create class for Employee

Demo Code

                         
#include <iostream>
                         //from w ww . j a v a2  s. co m
class Employee {
 private:
    const double BASE_HOURS = 40.0f;
                         
    double hoursWorked = 0.0f;
    double hourlyRate = 0.0f;
                         
 public:
    Employee();
    ~Employee();
                         
    // SETTERS
    void setHoursWorked(double);
    void setHourlyRate(double);
                         
    // GETTERS
    double getHoursWorked();
    double getHourlyRate();
    double getSalary();
                         
    void run();
};
                         
Employee::Employee() {}
Employee::~Employee() {}
                         
// SETTERS
void Employee::setHoursWorked(double hw) {
    hoursWorked = (hw > 0.0f) ? hw : -1;
}
void Employee::setHourlyRate(double rate) { hourlyRate = rate; }
// GETTERS
double Employee::getHoursWorked() { return hoursWorked; }
double Employee::getHourlyRate() { return hourlyRate; }
double Employee::getSalary() {
    if (getHoursWorked() > BASE_HOURS) {
        double overtime = getHoursWorked() - BASE_HOURS;
        double overtimePay = getHourlyRate() + (getHourlyRate() * 0.5);
                         
        return (BASE_HOURS * getHourlyRate()) + (overtime * overtimePay);
    } else {
        return getHoursWorked() * getHourlyRate();
    }
}
                         
void Employee::run() {
    double dtmp = 0.0f;
                         
    std::cout << "Enter hours worked (-1 to end): ";
    std::cin >> dtmp;
    setHoursWorked(dtmp);
                         
    if (dtmp != -1) {
        std::cout << "Enter hourly rate of the employee ($00.00): ";
        std::cin >> dtmp;
        setHourlyRate(dtmp);
                         
        std::cout << "Salary is: " << getSalary() << std::endl;
                         
        dtmp = 0.0f;
    }
}
                         
int main(int argc, const char *argv[]) {
    Employee sc;
                         
    while (sc.getHoursWorked() != -1) {
        sc.run();
    }
    return 0;
}

Result


Related Tutorials