在employee.h文件中声明Employee类。Employee类具有姓名、街道地址、城市和邮编等私有数据成员,都可以用字符数组来表示。成员函数:
构造函数 用来初始化所有成员数组,对字符数组的赋值可以使用字符串拷贝函数strcpy(char *, char *name);
display() 使用cout显示姓名、街道地址、城市和邮编等属性; change_name() 改变类中表示姓名属性的字符数组类型的数据成员。 在主程序中声明这个类的对象并对其进行操作。程序名:lab6_5.cpp。 参考运行结果:
★ 程序及运行结果:
//lab6_5employee.h #include //使用C中的string 10#include using namespace std; class Employee{ private: char name[10],street[20],city[10],mail[7];//姓名、街道、城市、邮编 public: Employee(char *n,char *s,char *c,char *m); 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<<\"姓名:\"<6.(编程,习题6-27)使用上一小题中的Employee类声明对象数组emp[5]。使用循环语句把数据显示出来。程序名:lab6_6.cpp。 参考运行结果:
★ 程序及运行结果:
//lab6_6.cpp #include \"lab6_5employee.h\" #include using namespace std; void main(){ Employee emp[5]={ Employee(\"n1\",\"s1\",\"c1\",\"m1\"), Employee(\"n2\",\"s2\",\"c2\",\"m2\"), Employee(\"n3\",\"s3\",\"c3\",\"m3\"), Employee(\"n4\",\"s4\",\"c4\",\"m4\"), 12} Employee(\"n5\",\"s5\",\"c5\",\"m5\") }; for(int i=0;i<5;i++){ emp[i].display(); cout<程序名:lab6_7.cpp。 参考运行结果:13
★ 程序及运行结果:
(1) lab6_7.h:People(人员)类的声明及成员函数的实现 //lab6_7.h #include #include using namespace std; 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); } 14void 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); } void input(); void output(); }; People::People(People &p) :birthday(p.birthday) { strcpy(name,p.name); strcpy(number,p.number); strcpy(sex,p.sex); strcpy(id,p.id); } inline void People::input() { cout<<\"姓名:\"; cin>>name; cout<<\"编号:\"; cin>>number; cout<<\"性别(男/女):\"; cin>>sex; cout<<\"出生日期(年 月 日):\"; int y,m,d; cin>>y>>m>>d; Setbirthyy(y); Setbirthmm(m); Setbirthdd(d); cout<<\"身份证号:\"; cin>>id; } inline void People::output() 15
{ } (2) lab6_7.cpp:主函数(输入、输出和拷贝构造函数测试) //lab6_7.cpp #include \"lab6_7.h\" void main(){ People p; p.input(); cout<cout<<\"姓名:\"<四、实验提示步骤2提示
二维数组动态分配空间
示例1 m×n矩阵 #include using namespace std; void main(){ int i,j,m,n,**A; } cout<<\"输入\"<>A[i][j];//数组形式 cout<<\"输入矩阵的行数m和列数n:\"; cin>>m>>n; A=new int*[m]; //m行 for(i=0;i示例2 不规范的矩阵#include using namespace std; void main(){ int **a=new int*[2]; a[0]=new int[3]; a[1]=new int[2]; a[0][0]=11; a[0][1]=12; a[0][2]=13; a[1][0]=21; a[1][1]=22; cout<18