《上机实验11:类的多态性应用.docx》由会员分享,可在线阅读,更多相关《上机实验11:类的多态性应用.docx(9页珍藏版)》请在优知文库上搜索。
1、上机实验11:类的多态性应用一、实验内容已知人类是学生类和教师类的基类,调试下面程序使之运行后输出下图结果?改写程序在主函数中使用指针数组的方式使之输出相同的运行结果。头文件People.h#pragmaonceclassCPeopIe(public:CPeopIefchar*name);CPeople(void);private:charname10;public:voiddisplay();char*getName(););程序文件People.cpp#includeStdAfx.h#includePeople.h#includeusingnamespacestd;CPeople:CPeo
2、ple(char*name)strcpy(this-name,name);)CPeople:CPeople(void)()voidCPeople:display()(nameendl;CoUt我是人,名字为:char*CPeople:getName()(returnname;)头文件Student.hftpragmaonceincludepeople.hclassCStudent:publicCPeopIe(public:CStudentfchar*name);CStudent(void);voiddisplay(););程序文件Studentxpp#includeStdAfx.h#inclu
3、deStudent.hftincludeusingnamespacestd;CStudent-CStudentfchar*name):CPeople(name)()CStudent:CStudent(void)()voidCStudent:display()(COUt我是学生,姓名为:,getName()endl;)头文件Teachenh#pragmaonce#includePeople.hclassCTeacher:publicCPeopIe(public:CTeacher(char*name);CTeacher(void);voiddisplay););程序文件TeaCher.c叩ftin
4、cludeStdAfx.h#indudeTeacher.h#includeusingnamespacestd;CTeacher:CTeacher(char*name):CPeople(name)()CTeacher:CTeacher(void)()voidCTeacher:display()(COUt我是老师,姓名为:getName()display();pe=&st2;pe-display();return0;)二、实验要求(1)调试、补充完整程序中缺失的语句;(2)改写、调试程序,使其能够输出图示运行结果。三、实验结果截图(请将实际上机运行的结果截图并上传)PwtfMm*v-莉Vi?SSO
5、3Buu0JT)FW43StM,1.j必修父-5Wym*EHD;,.*,/JJ.I】,.?3,;魂一;:iMUJMma9*jljfQEt卜.iryWT3IfVimBlTm.9(:5tfiWCJIWflB0j*/Ilnclde,r4af.h*Oind三*5!atart.hIintldr*Tecr.hrc,.T(Xarfv)11edrrMQifi*)HKIrIlHBlS停.WnHWhuMNmftlteM*If2f*htac,M11MtUdt*MTlt*m*DetnK0)tM0f.MfBflf三三三用iBs;生i.4,iQM5tflMwWutwlTccmhl*UUJ;门7血wjMhAJMMAJ*JP
6、f09J16OjiMakzwOjSMtppNWaMROjwWfMUWJBKftpublicCFe(*le(dNrtNK)*tPwlM4).KiS,:gMN10public中Md-o.har(etlae()tra*5t9tVIUKtR*lnrtWYHI,MH11*lMBlhWtaMUfPM9l*1j*Cocwt7tZhVMrnnMJir1由Hb3fl2wR“1f*1)1m5Jrq,“多椎Hr(171)31m11m.NMM091IMAA金jtIlVVlM肋mMx*-iejnuwdWMWjwt11HUWJttxCF2kR9le(cbrMat)Wjyne,M)CPel:Epk(Td)voi4CPe9l
7、e:!j*JIMMai*F*jTwr*18NopkXWtj*MPjTkwxw(dzsttz4MAtowinclu*,f.atincluFwrt.htinclu*Tc*d)三.h*Tm.t三a(intr.TWtrr()(CStvtaitUthantun)CTwtar211W3.CPelepek:Ar!P*M19lw()X三AttlpMiwlOrtwn.,、H三sN.CIJt*MWItnrm*MtaU*曲UU*MB.ouEBs5astW,丽S1.tAbDiBMM皿JfcMGH-5iJJAJJ,:e0如5;r;Jzt:*-rrj/4”.e),aCVIm,r”I(HIh1a0日S103理:;,VUBlKAKvK,,hMhRh内h”“。H三h三HiSMn*MnrtMBitmmtMtal(*tISfI4KwH1mMim*MtMkfDIlttQh;2,皿M3皿。NWSSdIAD91,WhiKCoM:);MNmimMIkbeIllmrlIXbtoIT*-9111111p11KWt外rXMM11cmuMMI,上OTImMHIEJU,MilHD1.1三W.