类里的通用成员函数应声明为static

类C的成员函数f,如果f的实现实现不依赖于C的任何成员变量,则f为通用函数。

对于通用函数f,可以将其从类C中分离出来做成一个全局函数,也可以仍然让它属于类C,但加上static。

两种处理方法实际都是使f成为不以来于具体对象实例的,随处可调用的全局函数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,根据您的要求,我来设计一个用于人事管理的People(人员)类。 ```cpp #include <iostream> using namespace std; class People { private: int number; // 编号 string sex; // 性别 string id; // 身份证号 static int ntotalnumber; // 人员总数 class calendar { // 出生日期,作为People(人员)类的封闭类对象 public: int year; int month; int day; calendar(int y, int m, int d) : year(y), month(m), day(d) {} void display() { cout << year << "-" << month << "-" << day << endl; } } birthday; public: People(int n, string s, string i, int y, int m, int d) : number(n), sex(s), id(i), birthday(y, m, d) { ntotalnumber++; } People(const People& p) : number(p.number), sex(p.sex), id(p.id), birthday(p.birthday.year, p.birthday.month, p.birthday.day) {} ~People() { ntotalnumber--; } void display() { cout << "编号:" << number << endl; cout << "性别:" << sex << endl; cout << "身份证号:" << id << endl; cout << "出生日期:"; birthday.display(); } static void displayTotalNumber() { cout << "总人数:" << ntotalnumber << endl; } }; int People::ntotalnumber = 0; // 初始化静态成员变量 int main() { People p1(1, "男", "123456789012345678", 1990, 10, 1); People p2(2, "女", "123456789012345679", 1995, 3, 15); People::displayTotalNumber(); // 输出总人数 p1.display(); // 输出p1的信息 p2.display(); // 输出p2的信息 return 0; } ``` 以上是一个比较简单的People(人员)类的设计,包括了构造函数和析构函数、复制构造函数、内联成员函数、静态成员函数等。其中,出生日期使用了封闭类对象的方式,静态成员变量和静态成员函数用于统计和输出总人数。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值