《C++程序设计》实验报告实验名称:继承与组合姓名:钟玲学号:15081508专业:通信工程实验时间:2016.11.15杭州电子科技大学通信工程学院一、实验目的1.了解继承在面向对象程序设计中的重要作用。2.进一步理解继承与派生的概念。3.掌握通过继承派生出一个新类的方法。4.了解虚基类的作用和用法。5.掌握类的组合。二、实验内容1、请先阅读下面的程序,分析程序运行的结果,然后再上机运行程序,验证自己分析的结果是否正确4、按下列要求编写程序。(1)定义一个分数类score。它有3个数据成员:Chinese//语文课成绩English//英语课成绩Mathematics//数学课成绩2个构造函数:无参的和带参数的。3个成员函数:是否带参数根据需要自定。sum()//计算三门课总成绩prin()//输出三门课成绩和总成绩modify()//修改三门课成绩(2)定义一个学生类student。它有3个数据成员:Num//学号Name//姓名MyScore//成绩I2个构造函数:无参的和带参数的3个成员函数:是否带参数根据需要自定。Sum()//计算某学生三门课总成绩Print()//输出某学生学号、姓名和成绩modify()//修改某学生学号、姓名和成绩(3)在主函数中,先定义一个学生类对象数组,再通过for循环给对象数组赋上实际值,最后输出对象数组各元素的值。三、实验过程及实验结果第1题:程序1#includeiostreamusingnamespacestd;classA{public:A(){coutA::A()called.\n;}~A(){coutA::~A()called.\n;}};classB:publicA{public:B(inti){coutB::B()called.\n;buf=newchar[i];}virtual~B(){delete[]buf;coutB::~B()called.\n;}private:char*buf;};intmain(){Bb(10);return0;}实验结果:程序2#includeiostreamusingnamespacestd;classA{public:A(inta,intb):x(a),y(b){coutAconstructor...endl;}voidAdd(inta,intb){x+=a;y+=b;}voiddisplay(){cout(x,y);}~A(){coutdestructorA...endl;}private:intx,y;};classB:privateA{private:inti,j;AAobj;public:B(inta,intb,intc,intd):A(a,b),i(c),j(d),Aobj(1,1){coutBconstructor..endl;}voidAdd(intx1,inty1,intx2,inty2){A::Add(x1,y1);i+=x2;j+=y2;}voiddisplay(){A::display();Aobj.display();cout(i,j(endl;}~B(){coutdestructorB...endl;}};intmain(){Bb(1,2,3,4);b.display();b.Add(1,3,5,7);b.display();return0;}实验结果:程序3#includeiostreamusingnamespacestd;classA{public:A(inta):x(a){coutAconstructor...xendl;}intf(){return++x;}~A(){coutdestructorA...endl;}private:intx;};classB:privatevirtualA{private:inty;AAobj;public:B(inta,intb,intc):A(a),y(c),Aobj(c){coutBconstructor..yendl;}intf(){A::f();Aobj.f();return++y;}voiddisplay(){coutA::f()\tAobj.f()\tf()endl;}~B(){coutdestructorB...endl;}};classC:publicB{public:C(inta,intb,intc):B(a,b,c),A(0){coutCconstructor...endl;}};classD:publicC,publicvirtualA{public:D(inta,intb,intc):C(a,b,c),A(c){coutDconstructor...endl;}~D(){coutDconstructor...endl;}};intmain(){Dd(7,8,9);d.f();d.display();return0;}实验结果:第4题1.score头文件#if!defineSCORE_H#defineSCORE_HclassScore{public:Score();Score(floatx1,floaty1,floatz1);floatsum();voidprint();voidmodify(floatx2,floaty2,floatz2);private:floatcomputer;floatenglish;floatmathematics;};#endif2.score源文件#includeiostream.h#includescore.h#includeiomanip.hScore::Score(){computer=0;english=0;mathematics=0;}Score::Score(floatx1,floaty1,floatz1){computer=x1;english=y1;mathematics=z1;}floatScore::sum(){return(computer+english+mathematics);}voidScore::print(){coutsetw(8)computersetw(8)englishsetw(8)mathematicssetw(8)sum();}voidScore::modify(floatx2,floaty2,floatz2){computer=x2;english=y2;mathematics=z2;}3.student头文件#includescore.hclassStudent{private:intnumber;charname[20];Scoreascore;public:Student();Student(intnumber1,char*pname1,floatscore1,floatscore2,floatscore3);floatsum();voidprint();voidmodify(intnumber2,char*pname2,floatscore21,floatscore22,floatscore23);};4.student源文件#includeiostream.h#includestudent.h#includeiomanip.h#includestring.hStudent::Student():ascore(){number=0;}Student::Student(intnumber1,char*pname1,floatscore1,floatscore2,floatscore3):ascore(score1,score2,score3){number=number1;strncpy(name,pname1,sizeof(name));name[sizeof(name)-1]='\0';}floatStudent::sum(){return(ascore.sum());}voidStudent::print(){coutendl;coutsetw(8)numbersetw(8)name;ascore.print();}voidStudent::modify(intnumber2,char*pname2,floatscore21,floatscore22,floatscore23){number=number2;strncpy(name,pname2,sizeof(name));name[sizeof(name)-1]='\0';ascore.modify(score21,score22,score23);}5.main函数源文件#includeiostream.h#includestudent.h#includeiomanip.hconstsize=3;voidmain(){intnumberi;charnamei[20];floatscore1,score2,score3;StudentaSA[size];for(inti=0;isize;i++){coutpleaseinputthedataofNO.i+1student;cout\number:;cinnumberi;coutname:;cinnamei;coutscoreofcomputer:;cinscore1;coutscoreofenglish:;cinscore2;coutscoreofmathematics:;cinscore3;aSA[i].modify(numberi,namei,score1,score2,score3);}cout\n\n;coutsetw(8)学号setw(8)姓名setw(8)计算机setw(8)英语setw(8)数学setw(8)总分;coutendl;for(intj=0;jsize;j++)aSA[j].print();coutendl;}实验结果:四、实验小结通过本次试验,我初步了解了继承在面向对象程序设计中的重要作用。通过做第一道练习题,更深地理解了继承与派生的概念。后面几道练习题,则让我掌握通过继承派生出一个新类的方法。在程序运行失败的时候,学会了通过检查和一步步的调试来修改程序,直至运行成功为止。对虚基类的作用和类的组合更加熟悉了,对c++了解和运用也愈加熟练了。