您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 其它文档 > 实验3继承和派生类的应用
C++面向对象程序设计实验指导实验3继承和派生类的应用3.1实验目的1.掌握多重继承和派生类的方法2.掌握初始化基类成员的方法3.掌握定义虚基类的方法3.2实验内容与步骤1.上机实验题一定义一个日期(年、月、日)的类和一个时间(时、分、秒)的类,并由这两个类派生出日期和时间类。主函数完成基类和派生类的测试工作。⑴分析定义一个描述日期的类,构造函数完成年、月、日的初始化,包含一个重新设置日期的成员函数,一个获取日期的成员函数。该类可定义为:classDate{intYear,Month,Day;//分别存放年、月、日public:Date(inty=0,intm=0,intd=0){Year=y;Month=m;Day=d;}voidSetDate(int,int,int);voidGetDate(char*);};函数SetDate完成数据成员的赋初值。函数GetDate要将整数年、月、日变换成字符串后,存放到参数所指向的字符串中。把一个整数变换成字符串可通过库函数:char*_itoa(inta,char*s,intb);来实现,参数a为要变换的整数,b为数制的基数(如10,表示将a转换为对应的十进制的字符串),转换的结果存放到s所指向的字符串中。函数返回变换后字符串的首指针。该成员函数可以是:voidDate::GetDate(char*s){chart[20];实验3继承和派生类的应用_itoa(Year,s,10);//将年变换为字符串表示strcat(s,/);//年、月、日之间用“/”隔开_itoa(Month,t,10);//将月变换为字符串表示strcat(s,t);//将年、月字符串拼接strcat(s,/);_itoa(Day,t,10);strcat(s,t);//将年、月、日拼接成一个字符串}定义描述时间的类与描述日期的类类同,然后用这二个类作为基类,公有派生出描述日期和时间的类。简化的参考程序如下:#includeiostream.h#includestring.h#includestdlib.hclassDate{intYear,Month,Day;//分别存放年、月、日public:Date(inty=0,intm=0,intd=0){Year=y;Month=m;Day=d;}voidSetDate(int,int,int);voidGetDate(char*);};voidDate::SetDate(inty,intm,intd){Year=y;Month=m;Day=d;}voidDate::GetDate(char*s){chart[20];_itoa(Year,s,10);strcat(s,/);_itoa(Month,t,10);strcat(s,t);strcat(s,/);_itoa(Day,t,10);strcat(s,t);}classTime{intHours,Minutes,Seconds;//时、分、秒public:Time(inth=0,intm=0,ints=0)C++面向对象程序设计实验指导{Hours=h;Minutes=m;Seconds=s;}voidSetTime(inth,intm,ints){Hours=h;Minutes=m;Seconds=s;}voidGetTime(char*);};voidTime::GetTime(char*s){chart[20];_itoa(Hours,s,10);strcat(s,:);_itoa(Minutes,t,10);strcat(s,t);strcat(s,:);_itoa(Seconds,t,10);strcat(s,t);}classDateTime:publicDate,publicTime{//公有派生public:DateTime():Date(),Time(){}DateTime(inty,intm,intd,inth,intmin,ints):Date(y,m,d),Time(h,min,s){}voidGetDateTime(char*);voidSetDateTime(inty,intm,intd,inth,intmin,ints);};voidDateTime::GetDateTime(char*s){chars1[100],s2[100];GetDate(s1);GetTime(s2);strcpy(s,日期和时间分别是:);strcat(s,s1);strcat(s,;);strcat(s,s2);}voidDateTime::SetDateTime(inty,intm,intd,inth,intmin,ints){SetDate(y,m,d);SetTime(h,min,s);}voidmain(void){Dated1(2003,1,30);chars[200];d1.GetDate(s);cout日期是:s'\n';Timet1(12,25,50);t1.GetTime(s);实验3继承和派生类的应用cout时间是:s'\n';DateTimedt1(2003,2,4,8,20,15);dt1.GetDateTime(s);couts'\n';dt1.SetDateTime(2003,12,30,23,50,20);dt1.GetDateTime(s);couts'\n';}⑵上机要求将类DateTime改为由基类Date和Time私有派生,程序能否正确编译和执行,为什么?⑶写出实验报告。2.上机实验题二设计一个描述儿童、成人和老人的类,儿童分为学龄前和学龄期儿童,成人有工作,老人已退休。提取共性作为基类,并派生出满足要求的各个类及每一个类上的操作。设计一个完整的程序,并完成测试程序的正确性。⑴分析描述一个人的基本特性包括:姓名,出生日期,出生地。把这些基本特性定义为一个基类Base。不论学龄前还是学龄期儿童都要有监护人。为此在类Base的基础上派生出类AllChil,再把类AllChil作为基类派出学龄前和学龄期儿童的类。把类Base作为基类,分别派生出成人类和老人类。一个供参考的部分程序为:#includeiostream.h#includestring.hclassBase{charName[14];//姓名intYear,Month,Day;//分别存放出生年、月、日charBothPlace[30];//出生地public:Base(char*n,inty,intm,intd,char*bp);Base();voidSetDate(int,int,int);//设置出生年、月、日voidSetName(char*);//设置姓名voidSetBothPlace(char*);//设置出生地voidGetDate(int*,int*,int*);//获取出生年、月、日voidGetName(char*);//获取姓名voidGetBothPlace(char*);//获取出生地voidPrintBase();//输出有关信息};classAllChil:publicBaseC++面向对象程序设计实验指导{charParMa[14];//监护人public:AllChil():Base();AllChil(char*n,inty,intm,intd,char*bp,char*pm):Base(n,y,m,d,bp);voidSetParMa(char*);//设置监护人voidGetParMa(char*);//获取监护人voidPrintAllChil();//输出有关信息};classMen:publicBase{char*unit;//单位intsalary;//工资chartel[20];//电话public:Men():Base();Men(char*n,inty,intm,intd,char*bp,char*u,ints,char*te):Base(n,y,m,d,bp);voidGetData(char*,int*,char*);//取单位,工资,电话voidSeetData(char*,int*,char*);//设置单位,工资,电话voidPrintMen();//输出有关信息};classOldMen:publicBase{intsalary;chartel[20];public:......};⑵上机要求完成程序的正确性测试工作。⑶写出实验报告。3.3项目选做设计程序从学生类中派生出硕士研究生和博士研究生类。
本文标题:实验3继承和派生类的应用
链接地址:https://www.777doc.com/doc-2530661 .html