设计一个职工类Employee。数据成员包括字符串型(或用string类)的name姓名、street街道地址、city市、province省、postalcode邮政编码,以及出生年月日Cdate内嵌对象(即Cdate类)。要求用成员函数实现以下功能 1创建职工对象时可以按不同方式初始化职工信息(即有多种不同的构造函数。 2动态地设置职工对象信息(如设置地址,修改邮政编码等)。 3在屏幕上打印职工信息。 4在主函数中测试该类
时间: 2023-04-12 14:01:18 浏览: 233
新建一个雇员类,它的数据成员有雇员代号,年龄,工资,性别, 姓名,输入雇员资料方法,打印雇员资料方法。
以下是职工类Employee的完整代码:
#include <iostream>
#include <string>
using namespace std;
class Cdate {
public:
int year;
int month;
int day;
};
class Employee {
public:
string name;
string street;
string city;
string province;
string postalcode;
Cdate birthdate;
Employee() {}
Employee(string n, string s, string c, string p, string pc, Cdate b) {
name = n;
street = s;
city = c;
province = p;
postalcode = pc;
birthdate = b;
}
void setInfo(string s, string c, string p, string pc) {
street = s;
city = c;
province = p;
postalcode = pc;
}
void printInfo() {
cout << "Name: " << name << endl;
cout << "Address: " << street << ", " << city << ", " << province << ", " << postalcode << endl;
cout << "Birthdate: " << birthdate.year << "/" << birthdate.month << "/" << birthdate.day << endl;
}
};
int main() {
Cdate b = { 1990, 1, 1 };
Employee e1("John", "123 Main St", "Toronto", "Ontario", "M1M 1M1", b);
e1.printInfo();
Employee e2;
e2.name = "Jane";
e2.birthdate.year = 1995;
e2.birthdate.month = 2;
e2.birthdate.day = 15;
e2.setInfo("456 Second St", "Vancouver", "British Columbia", "V2V 2V2");
e2.printInfo();
return 0;
}
阅读全文