(1)定义date类,数据成员year、month、day,带参数的构造函数用于初始化数据成员。 (2)定义雇员类employee,存储雇员的编号、姓名、生日信息,数据成员生日为date类的对象,完成类的定义使主函数可实现如下语句: //定义一个雇员,编号为10,生日为1980.11.20,姓名为Tom employee Tom(“Tom”,10,1980,11,20); date today(1980,11,20); if(Tom.isbirthday(today)) //今天是否Tom生日
时间: 2024-03-27 14:34:53 浏览: 84
以下是题目的代码实现:
```c++
#include<iostream>
using namespace std;
//日期类的定义
class date {
public:
int year;
int month;
int day;
date(int y, int m, int d) {
year = y;
month = m;
day = d;
}
};
//雇员类的定义
class employee {
public:
int id; //雇员编号
string name; //雇员姓名
date birthday; //雇员生日
employee(string n, int i, int y, int m, int d) :name(n), id(i), birthday(y, m, d) {}
bool isbirthday(date today) {
if (today.year == birthday.year && today.month == birthday.month && today.day == birthday.day) {
return true;
}
return false;
}
};
int main() {
//定义一个雇员
employee Tom("Tom", 10, 1980, 11, 20);
//定义一个日期
date today(1980, 11, 20);
//判断今天是否Tom的生日
if (Tom.isbirthday(today)) {
cout << "Today is Tom's birthday!" << endl;
}
else {
cout << "Today is not Tom's birthday." << endl;
}
return 0;
}
```
输出结果:
```
Today is Tom's birthday!
```
阅读全文