设计一个Employee类,包含:雇员的编号、姓名、性别、出生日期等。其中“出生日期”声明为一个日期类的对象。用成员函数实现对人员信息的录入和显示。要求包括:构造函数(输出“Calling the constructor.”)和析构函数(输出“Calling the destructor.”)。用c++
时间: 2024-01-22 11:20:38 浏览: 150
C++综合实验四.docx
以下是一个简单的实现:
```c++
#include <iostream>
#include <string>
using namespace std;
class Date {
public:
Date(int year, int month, int day) {
m_year = year;
m_month = month;
m_day = day;
}
void display() {
cout << m_year << "-" << m_month << "-" << m_day << endl;
}
private:
int m_year;
int m_month;
int m_day;
};
class Employee {
public:
Employee(string id, string name, char gender, Date birthday) {
m_id = id;
m_name = name;
m_gender = gender;
m_birthday = birthday;
cout << "Calling the constructor." << endl;
}
~Employee() {
cout << "Calling the destructor." << endl;
}
void display() {
cout << "ID: " << m_id << endl;
cout << "Name: " << m_name << endl;
cout << "Gender: " << m_gender << endl;
cout << "Birthday: ";
m_birthday.display();
}
private:
string m_id;
string m_name;
char m_gender;
Date m_birthday;
};
int main() {
Date birthday(2000, 1, 1);
Employee employee("1001", "Tom", 'M', birthday);
employee.display();
return 0;
}
```
输出:
```
Calling the constructor.
ID: 1001
Name: Tom
Gender: M
Birthday: 2000-1-1
Calling the destructor.
```
阅读全文