hw11_02.cpp

2、设计Employee类,使用string对象,这个类包括:
四个string类数据成员:name, addr, city, zip,分别表示姓名,街道地址,省市,邮编
一个带四个形参的构造函数,用于初始化数据成员
成员函数voidChangeName(string & name),修改姓名
成员函数voidDisplay(),输出所有信息(即姓名,地址,省市和邮编)
数据成员是保护类型的,函数成员是公有类型的
实现这个类,在主函数中测试这个类:使用你自己的相关信息初始化数据,并在屏幕上输出。
#include <iostream>
#include <string> 

using namespace std;

class Employee
{
  public:
    Employee(const string &, const string &, const string &, const string &);
    void ChangeName(string &);
    void Display();

  protected:
    string name, addr, city, zip;
};
Employee::Employee(const string & n, const string & a, const string & c, const string & z) {
    name=n;
    addr=a;
    city=c;
    zip=z;
}
void Employee::ChangeName(string & s) {
    name=s;
}

void Employee::Display() {
    cout << "Name: " << name << endl;
    cout << "Address: " << addr << endl;
    cout << "City: " << city << endl;
    cout << "Zip: " << zip << endl;
}



int main()
{
    Employee emp("lightning", "华东师范大学", "中国上海市", "12345");
    emp.Display();
    string newName = "Jane Doe";
    emp.ChangeName(newName);
    emp.Display();

    return 0;


}

发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注

滚动至顶部