Question:
Specify, design, and implement a class called employee. The class has data members for the employee’s name, ID number, and salary based on an hourly wage. Member functions include computing the yearly salary and increasing the salary by a certain percentage, Add additional data members to store biweekly paycheck information and calculate overtime (for over 40 hours per week) for each paycheck.
My answer:
employee.h
// ConsoleApplication5.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include "pch.h"
#include "employee.h"
#include <iostream>
using namespace std;
int main()
{
employee a("Lily", "4", 120.5);
a.employee_info();
cout << "Yearly salary: " << a.compute_yearly_salary() << endl;
cout << "Now increase the salary by a certain percentage" << endl;
a.increase_salary(0.5);
a.employee_info();
cout << "Yearly salary: " << a.compute_yearly_salary() << endl;
}
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单
// 入门提示:
// 1. 使用解决方案资源管理器窗口添加/管理文件
// 2. 使用团队资源管理器窗口连接到源代码管理
// 3. 使用输出窗口查看生成输出和其他消息
// 4. 使用错误列表窗口查看错误
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
employee.cpp
#include "pch.h"
#include "employee.h"
#include <cassert>
#include <iostream>
using namespace std;
double employee::compute_biweekly_paycheck()
{
return 2 * hourly_salary * 40;// 每周40小时
}
double employee::compute_yearly_salary()
{
return 52 * hourly_salary * 40;// 一年有52周
}
void employee::increase_salary(double percentage)
{
assert(percentage > -1 && percentage < 1);
hourly_salary = hourly_salary * (1 + percentage);
}
void employee::employee_info()
{
cout << "Name: " << name << endl;
cout << "ID Number: " << ID << endl;
cout << "hourly salary: " << hourly_salary << endl;
cout << "biweekly checkback: " << biweekly_paycheck << endl;
}
ConsoleApplication5.cpp
// ConsoleApplication5.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include "pch.h"
#include "employee.h"
#include <iostream>
using namespace std;
int main()
{
employee a("Lily", "4", 120.5);
a.employee_info();
cout << "Yearly salary: " << a.compute_yearly_salary() << endl;
cout << "Now increase the salary by a certain percentage" << endl;
a.increase_salary(0.5);
a.employee_info();
cout << "Yearly salary: " << a.compute_yearly_salary() << endl;
}
// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单
// 入门提示:
// 1. 使用解决方案资源管理器窗口添加/管理文件
// 2. 使用团队资源管理器窗口连接到源代码管理
// 3. 使用输出窗口查看生成输出和其他消息
// 4. 使用错误列表窗口查看错误
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
结果:
这个visual studio自动创建的项目中,要pch.h和pch.cpp
pch.h
// 入门提示:
// 1. 使用解决方案资源管理器窗口添加/管理文件
// 2. 使用团队资源管理器窗口连接到源代码管理
// 3. 使用输出窗口查看生成输出和其他消息
// 4. 使用错误列表窗口查看错误
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
#ifndef PCH_H
#define PCH_H
// TODO: 添加要在此处预编译的标头
#endif //PCH_H
pch.cpp
// pch.cpp: 与预编译标头对应的源文件;编译成功所必需的
#include "pch.h"
// 一般情况下,忽略此文件,但如果你使用的是预编译标头,请保留它。
Reference:
整理自 Data Structures and Other Objects Using C++ ( Fourth Edition ) Michael Main, Walter Savitch. p125