1.题目:
Problem Description
设计一个职工类,职工类中包括职工姓名、薪水和所有职工薪水总和(静态成员)三个数据成员,成员函数包括有构造函数、析构函数、及返回所有职工薪水总和的静态成员函数,此外还包含有一个友元函数,用来比较两个员工的薪水,结果返回薪水大的那个员工。要求将类定义完整并且用主函数进行测试。
Input
输入包括3个测试数据
Output
输出包括5行,分别是3个职工的信息,总的薪水总和,还有薪水最高的那个职工的信息。
Sample Input
Zhangsan 1200
Lisi 2400
Wangwu 1800
Sample Output
Zhangsan 1200
Lisi 2400
Wangwu 1800
Allsalary is:5400
The highest salary is:Lisi 2400
2.参考代码:
#include <iostream>
#include <string>
using namespace std;
class Account
{
private:
char* name;
int money;
public:
Account(char* n, int m);
Account(Account&);
~Account();
bool operator>(Account&);
static int sum;
void show();
};
Account::Account(char* n, int m)
{
name = new char[strlen(n) + 1];
strcpy(name, n);
money = m;
sum += money;
}
Account::Account(Account& c)
{
name = new char[strlen(c.name) + 1];
strcpy(name, c.name);
money = c.money;
sum += c.money;
}
Account::~Account()
{
delete []name;
sum -= money;
}
bool Account::operator>(Account& c)
{
return money > c.money;
}
void Account::show()
{
cout << name << " " << money << endl;
}
int Account::sum = 0;
int main()
{
char n1[21], n2[21], n3[21];
int m1, m2, m3;
cin >> n1 >> m1 >> n2 >> m2 >> n3 >> m3;
Account i(n1, m1), j(n2, m2), k(n3, m3);
i.show();
j.show();
k.show();
cout << "Allsalary is:" << Account::sum << endl << "The highest salary is:";
if (i > j && i > k)
i.show();
else if (j > i && j > k)
j.show();
else if (k > i && k > j)
k.show();
return 0;
}