首页 > 试题广场 >

定义一个 Employee 类,其中包括表示姓名、街道地址

[问答题]

定义一个 Employee 类,其中包括表示姓名、街道地址、城市和邮编等属性,包括 chage_name() display()等函数;display()使用 cout 语句显示姓名、街道地址、城市和邮编等属性,函数 change_name()改变对象的姓名属性,实现并测试这个类。

推荐

解:

源程序:

#include <iostream.h>
#include <string.h>
class Employee
{
private:
char name[30];
char street[30];
char city[18];
char zip[6];
public:
Employee(char *n, char *str, char *ct, char *z);
void change_name(char *n);
void display();
};
Employee::Employee (char *n,char *str,char *ct, char *z)
{
strcpy(name, n);
strcpy(street, str);
strcpy(city, ct);
strcpy(zip, z);
}
void Employee::change_name (char *n)
{
strcpy(name, n);
}
void Employee::display ()
{
cout << name << " " << street << " ";
cout << city << " "<< zip;
}
void main(void)
{
Employee e1("张三","平安大街 3 号", "北京", "100000");
e1.display();
cout << endl;
e1.change_name("李四");
e1.display();
cout << endl;
}

程序运行输出:

张三 平安大街 3 号 北京 100000

李四 平安大街 3 号 北京 100000



发表于 2018-04-18 20:46:30 回复(0)