C++语言程序设计实验答案 - 数组、指针与字符串 下载本文

void display(); void change_name(char *n); }; Employee::Employee(char *n,char *s,char *c,char *m){ strcpy(name,n); strcpy(street,s); strcpy(city,c); strcpy(mail,m); } void Employee::display(){ cout<<\姓名:\<

使用循环语句把数据显示出来。程序名:lab6_6.cpp。 参考运行结果:

9

★ 程序及运行结果:

//lab6_6.cpp #include \ #include using namespace std; void main(){ Employee emp[5]={ Employee(\,\,\,\), Employee(\,\,\,\), Employee(\,\,\,\), Employee(\,\,\,\), Employee(\,\,\,\) }; for(int i=0;i<5;i++){ emp[i].display(); cout<

7.(编程)修改实验4中的people(人员)类。

程序名:lab6_7.cpp。 参考运行结果:

★ 程序及运行结果:

(1) lab6_7.h:People(人员)类的声明及成员函数的实现 //lab6_7.h #include #include using namespace std; 11

class Date{ private: int yy,mm,dd; public: Date(){ } Date(Date &d) : yy(d.yy),mm(d.mm),dd(d.dd){}//增添 ~Date(){ } int Getyy()const{ return yy; } int Getmm()const{ return mm; } int Getdd()const{ return dd; } void Setyy(int y){ yy=y; } void Setmm(int m){ mm=m; } void Setdd(int d){ dd=d; } }; class People{ private: char name[11]; //姓名,增添 char number[7]; //编号,改为数组 char sex[3]; //男或女,改为数组 Date birthday; //出生日期 char id[16]; //身份证号,改为数组 public: People(){} People(People &p); ~People(){} void Getname(char na[]){ strcpy(na,name); } void Getnumber(char nu[]){ strcpy(nu,number); } void Getsex(char se[]){strcpy(se,sex); } int Getbirthyy()const{ return birthday.Getyy(); } int Getbirthmm()const{ return birthday.Getmm(); } int Getbirthdd()const{ return birthday.Getdd(); } void Getid(char d[]){ strcpy(d,id); } void Setname(char na[]){ strcpy(name,na); } void Setnumber(char nu[]){ strcpy(number,nu); } void Setsex(char se[]){strcpy(sex,se); } void Setbirthyy(int y){ birthday.Setyy(y); } void Setbirthmm(int m){ birthday.Setmm(m); } void Setbirthdd(int d){ birthday.Setdd(d); } void Setid(char d[]){ strcpy(id,d); } 12