Employee类具有姓名,街道地址,城市和邮编等私有数据成员,都可以用字符型数组来表示.在成员函数中,构造函数用来初始化所有成员数组,对字符数组的赋值可以使用字符串拷贝函数strcpy(char*,char*name);display()中使用cout显示姓名,街道地址,城市和邮编等属性,change_name(()改变类中表示姓名属性的字符数组类型的数据成员.在主程序中声明这个类的对象并对其进行操作.
#include <iostream>
#include <string>
using namespace std;
class Employee{private:char name[30];
char street[30];
char city[18];
char zip[6];public:Employee();
Employee(char *n,char *str,char *ct,char *z);
void change_name(char *n);
void display();};Employee::Employee (){}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 << " "<< city << " " << zip<< endl;}void main(){Employee em[5];
Employee el("张三","永乐大街1号","陕西","710600");
el.display();
温馨提示:内容为网友见解,仅供参考