类的静态与友元——职工薪水

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;
}


 

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值