} 5.(操作)调试程序lab4_2.cpp。
使用调试功能观察lab4_2.cpp程序的运行流程,跟踪观察类的构造函数、析构函数、成员函数的执行顺序,特别注意观察成员变量的构造与析构顺序。
6. 编程,习题2-31)声明一个表示时间的结构体。
声明一个表示时间的结构体,可以精确表示年、月、日、小时、分、秒;提示用户输入年、月、日、小时、分、秒的值,然后完整地显示出来。
建立一个项目lab4_3,包含一个C++源程序lab4_3.cpp。在主程序中实现输入输出。 参考运行结果:
★ 程序及运行结果:
//lab4_3.cpp(习题2-31) #include
} cout<<\输入小时:\; cin>>t2.hh; cout<<\输入分钟:\; cin>>t2.mi; cout<<\输入秒:\; cin>>t2.ss; cout< 类People的属性:number(编号),sex(性别)、birthday(出生日期)、id(身份证号)等。 其中“出生日期”声明为一个“日期”类内嵌子对象。 用成员函数实现人员信息的录入和显示。要求包括:构造函数和析构函数、内联成员函数、组合。程序名:lab4_4.cpp。 提示: 构造函数和析构函数的函数体可以定义为空; 两个类中对年、月和日分别定义取值和赋值成员函数; People类中的数据成员为: unsigned number;//编号,4位数字,第一位为1 char sex;//性别,m为男,w为女 Date birthday;//出生日期 unsigned id;//身份证号,8位数字,第一位不为0 参考程序运行结果: ★ 程序及运行结果: // lab4_4.cpp #include using namespace std; class Date{ private: int yy,mm,dd; public: Date(){ } ~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: unsigned number;//编号,4位数字,第一位为1 char sex;//性别,m为男,w为女 Date birthday;//出生日期 unsigned id;//身份证号,6位数字,第一位不为0 public: People(){} ~People(){} unsigned Getnumber(){ return number; } char Getsex(){ return sex; } int Getbirthyy()const{ return birthday.Getyy(); } int Getbirthmm()const{ return birthday.Getmm(); } int Getbirthdd()const{ return birthday.Getdd(); } unsigned Getid(){ return id; } void Setnumber(unsigned nu){ number=nu; } void Setsex(char se){ 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(unsigned d){ id=d; } }; void main(){ People p1,p2; 8 } unsigned n,d; char s; int yy,mm,dd; //输入p1 cout<<\输入编号:\; cin>>n; p1.Setnumber(n); cout<<\输入性别:\; cin>>s; p1.Setsex(s); cout<<\输入出生日期:\; cin>>yy>>mm>>dd; p1.Setbirthyy(yy); p1.Setbirthmm(mm); p1.Setbirthdd(dd); cout<<\输入身份证号:\; cin>>d; p1.Setid(d); cout<<\输入下一个人员的信息\< 三、实验提示 步骤6提示 先在主函数外用struct定义一个结构体,名称可为time,包含成员“年、月、日、时、分、秒”; 在主函数中,声明一个time的变量t1,并初始化“年、月、日、时、分、秒”为“2012、2、25、10、30、20”; 将变量t1的值取出,用cout显示日期,注意组合; 再声明一个time的变量t2; 依次输入“年、月、日、时、分、秒”,注意在输入前给出提示信息。 10