您好,欢迎访问三七文档
C++练习题一、编程题:所有的题需要在主函数中定义对象并测试类的功能。1、编写一个三角形类,有计算面积,显示面积的功能等。要求有无参数的和带参数的构造函数。2、设计一个立方体类Box,它能计算并输出立方体的体积和表面积。要求有无参数的和带参数的构造函数。3、定义一个国家基类Country,包含国名、首都、人口等属性,派生出省类Province,增加省会城市、人口数量属性。4、利用Clock类,派生一个带“AM”、“PM”的新时钟类NewClock。5、建立一个基类Building,用来存储一座楼房的层数、房间数以及它的总平方英尺数。建立派生类Housing,继承Building,并存储下面的内容:卧室和浴室的数量,另外,建立派生类Office,继承Building,并存储灭火器和电话的数目。然后,编制应用程序,建立住宅楼对象和办公楼对象,并输出它们的有关数据。6、设计一个建筑物类Building,由它派生出教学楼Teach-Building和宿舍楼类Dorm-Building,前者包括教学楼编号、层数、教室数、总面积等基本信息,后者包括宿舍楼编号、层数、宿舍数、总面积和容纳学生总人数等基本信息。7、定义一个基类有姓名、性别、年龄,再由基类派生出教师类和学生类,教师类增加工号、职称和工资,学生类增加学号、班级、专业和入学成绩。8、声明一个Shape基类,在此基础上派生出Rectangle和Circle类,二者都有GetArea()函数计算对象的面积。使用Rectangle类创建一个派生类Square。9、使用虚函数编写程序求球体和圆柱体的体积及表面积。由于球体和圆柱体都可以看作由圆继承而来,所以可以定义圆类circle作为基类。在circle类中定义一个数据成员radius和两个虚函数area()和volume()。由circle类派生sphere类和column类。在派生类中对虚函数area()和volume()重新定义,分别求球体和圆柱体的体积及表面积。二、写出程序结果1、某学校对教师每月工资的计算公式如下:固定工资+课时补贴。教授的固定工资为5000元,每个课时补贴50元;副教授的固定工资为3000元,每个课时补贴30元;讲师的固定工资为2000元,每个课时补贴20元。定义教师抽象类,派生不同职称的教师类,编写程序求若干教师的月工资。#includeiostreamusingnamespacestd;classTeacher{protected:doublesalary;intworkhours;public:Teacher(intwh=0){workhours=wh;}virtualvoidcal_salary()=0;voidprint(){coutsalaryendl;}};classProf:publicTeacher{public:Prof(intwh=0):Teacher(wh){}voidcal_salary(){salary=workhours*50+5000;}};classVice_Prof:publicTeacher{public:Vice_Prof(intwh=0):Teacher(wh){}voidcal_salary(){salary=workhours*30+3000;}};classLecture:publicTeacher{public:Lecture(intwh=0):Teacher(wh){}voidcal_salary(){salary=workhours*20+2000;}};intmain(){Teacher*pt;Profprof(200);pt=&prof;pt-cal_salary();prof.print();Vice_Profvice_prof(250);pt=&vice_prof;pt-cal_salary();vice_prof.print();Lecturelecture(100);pt=&lecture;pt-cal_salary();lecture.print();return0;}2、有一个交通工具类vehicle,将它作为基类派生小车类car、卡车类truck和轮船类boat,定义这些类并定义一个虚函数用来显示各类信息。#includeiostream#includecstringusingnamespacestd;classVehicle{public:virtualvoidshowinfo()=0;protected:charName[20];};classCar:publicVehicle{public:Car(char*name){strcpy(Name,name);}voidshowinfo(){coutNameendl;}protected:intRadius;};classTruck:publicVehicle{public:Truck(char*name){strcpy(Name,name);}voidshowinfo(){coutNameendl;}};classBoat:publicVehicle{public:Boat(char*name){strcpy(Name,name);}voidshowinfo(){coutNameendl;}};intmain(){Vehicle*vp;Carcar(奔驰);Trucktruck(运输卡车);Boatboat(游艇);vp=&car;vp-showinfo();vp=&truck;vp-showinfo();vp=&boat;vp-showinfo();return0;}3、定义一个shape抽象类,派生出Rectangle类和Circle类,计算各派生类对象的面积Area()。#includeiostream#includecstringusingnamespacestd;classShape{public:virtualvoidArea()=0;};classRectangle:publicShape{public:Rectangle(doublew,doubleh){width=w,height=h;}voidArea(){coutwidth*heightendl;}protected:doublewidth,height;};classCircle:publicShape{public:Circle(doubler){radius=r;}voidArea(){cout3.1415*radius*radiusendl;}protected:doubleradius;};intmain(){Shape*sp;Rectanglere1(10,6);Circlecir1(4.0);sp=&re1;sp-Area();sp=&cir1;sp-Area();return0;}4、定义猫科动物Animal类,由其派生出猫类(Cat)和豹类(Leopard),二者都包含虚函数sound(),要求根据派生类对象的不同调用各自重载后的成员函数。#includeiostreamusingnamespacestd;classAnimal{public:virtualvoidSpeak()=0;};classCat:publicAnimal{voidSpeak(){coutMynameisCatendl;}};classLeopard:publicAnimal{voidSpeak(){coutMynameisLeopardendl;}};intmain(){Animal*pa;Catcat;pa=&cat;pa-Speak();Leopardleopard;pa=&leopard;pa-Speak();return0;}5、建立一个基类Building,用来存储一座楼房的层数、房间数以及它的总平方英尺数。建立派生类Housing,继承Building,并存储下面的内容:卧室和浴室的数量,另外,建立派生类Office,继承Building,并存储灭火器和电话的数目。然后,编制应用程序,建立住宅楼对象和办公楼对象,并输出它们的有关数据。程序代码:#includeiostreamusingnamespacestd;classBuilding{public:Building(intf,intr,doubleft){floors=f;rooms=r;footage=ft;}voidshow(){coutfloors:floorsendl;coutrooms:roomsendl;couttotalarea:footageendl;}protected:intfloors;introoms;doublefootage;};classHousing:publicBuilding{public:Housing(intf,intr,doubleft,intbd,intbth):Building(f,r,ft){bedrooms=bd;bathrooms=bth;}voidshow(){cout\nHOUSING:\n;Building::show();coutbedrooms:bedroomsendl;coutbathrooms:bathroomsendl;}private:intbedrooms;intbathrooms;};classOffice:publicBuilding{public:Office(intf,intr,doubleft,intph,intex):Building(f,r,ft){phones=ph;extinguishers=ex;}voidshow(){cout\nHOUSING:\n;Building::show();coutphones:phonesendl;coutextinguishers:extinguishersendl;}private:intphones;intextinguishers;};voidmain(){Housinghob(5,7,140,2,2);Officeoob(8,12,500,12,2);hob.show();oob.show();}6、看程序写结果#includeiostreamusingnamespacestd;classparent{inti;protected:intx;public:parent(){x=0;i=0;}voidchange(){x++;i++;}voiddisplay();};classson:publicparent{public:voidmodify();};voidparent::display(){coutx=xendl;}voidson::modify(){x++;}voidmain(){sonA;parentB;coutDisplayderivedclassobjectA:\n;A.display();A.change();A.display();A.modify();A.display();coutDisplaybaseclassobjectB:\n;B.change();B.display();}7、看程序写结果#includeiostreamusingnamespacestd;classbase{public:voidshow(){coutclassbaseshow()iscalled.\n;}};classderived:publicbase{public:voidshow(){coutclassderivedshow()iscalled.\n;}};voidmain(){basedemo1;deriveddemo2;demo1.show();demo2.show(
本文标题:C++练习题
链接地址:https://www.777doc.com/doc-4347579 .html