创建一个Employee类,该类中有字符数组,表示姓名、街道地址‘市、省和邮政编码。把表示构造函数、ChangeName()、Display()的函数原型放在类定义中,构造函数初始化每个成员,Display()函数把完整的对象数据打印出来。其中的数据成员是保护的,函数是公共的。主函数实现用户信息的显示。 int main(){ Employee em("Mark Brooks","5 West St.","Revere","CA","12290"); em.Display(); em.ChangeName("Richard Voss"); em.Display(); }
时间: 2023-05-16 21:07:36 浏览: 136
以下是Employee类的定义和实现:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Employee {
protected:
string name;
string streetAddress;
string city;
string state;
string zipCode;
public:
Employee(string name, string streetAddress, string city, string state, string zipCode) {
this->name = name;
this->streetAddress = streetAddress;
this->city = city;
this->state = state;
this->zipCode = zipCode;
}
void ChangeName(string newName) {
name = newName;
}
void Display() {
cout << "Name: " << name << endl;
cout << "Address: " << streetAddress << endl;
cout << "City: " << city << endl;
cout << "State: " << state << endl;
cout << "Zip Code: " << zipCode << endl;
}
};
int main() {
Employee em("Mark Brooks","5 West St.","Revere","CA","12290");
em.Display();
em.ChangeName("Richard Voss");
em.Display();
return 0;
}
```
运行结果:
```
Name: Mark Brooks
Address: 5 West St.
City: Revere
State: CA
Zip Code: 12290
Name: Richard Voss
Address: 5 West St.
City: Revere
State: CA
Zip Code: 12290
```
以上是完整的代码。