Employee class

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 文件

结果:
picture
这个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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Memories off

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值