定义一个 Employee 类,其中包括表示姓名、地址、城市和邮编等属性,包括setName( )和 display( )等函数。display( )使用 cout 语显示姓名、地址、城市和邮编等属性,函数 setName( )改变对象的姓名属性,编写c++代码
时间: 2024-05-03 17:20:28 浏览: 81
Sure! 下面是C++的代码实现:
```c++
#include <iostream>
using namespace std;
class Employee {
private:
string name;
string address;
string city;
string zipCode;
public:
// 构造函数
Employee(string name, string address, string city, string zipCode) {
this->name = name;
this->address = address;
this->city = city;
this->zipCode = zipCode;
}
// 修改姓名
void setName(string name) {
this->name = name;
}
// 显示信息
void display() {
cout << "Name: " << name << endl;
cout << "Address: " << address << endl;
cout << "City: " << city << endl;
cout << "Zip Code: " << zipCode << endl;
}
};
int main() {
// 创建对象
Employee bob("Bob", "123 Main St", "New York", "10001");
// 显示原始信息
cout << "Original Information:" << endl;
bob.display();
// 修改姓名
bob.setName("Bobby");
// 显示修改后的信息
cout << "New Information:" << endl;
bob.display();
return 0;
}
```
输出结果为:
```
Original Information:
Name: Bob
Address: 123 Main St
City: New York
Zip Code: 10001
New Information:
Name: Bobby
Address: 123 Main St
City: New York
Zip Code: 10001
```
阅读全文