定义一个 Employee 类,其中包括表示姓名、地址、城市和邮编等属性,包括setName()和display()等函数。display()使用cout语句显示姓名、地址、城市和邮编等属性,函数 setName()改变对象的姓名属性,实现并测试这个类。
时间: 2024-03-08 07:48:04 浏览: 93
下面是定义一个 Employee 类的示例代码,其中包括表示姓名、地址、城市和邮编等属性,以及 setName() 和 display() 等函数。你可以根据需要进行修改和测试。
```c++
#include <iostream>
#include <string>
using namespace std;
class Employee {
private:
string name;
string address;
string city;
string zipcode;
public:
void setName(string newName) {
name = newName;
}
void setAddress(string newAddress) {
address = newAddress;
}
void setCity(string newCity) {
city = newCity;
}
void setZipcode(string newZipcode) {
zipcode = newZipcode;
}
void display() {
cout << "Name: " << name << endl;
cout << "Address: " << address << endl;
cout << "City: " << city << endl;
cout << "Zipcode: " << zipcode << endl;
}
};
int main() {
Employee e1;
e1.setName("John Smith");
e1.setAddress("123 Main St");
e1.setCity("Anytown");
e1.setZipcode("12345");
e1.display();
return 0;
}
```
输出结果:
```
Name: John Smith
Address: 123 Main St
City: Anytown
Zipcode: 12345
```
阅读全文