您好,欢迎访问三七文档
4.1测试一个名为rectangle的矩形类,其属性为矩形的左下角与右上角两个点的坐标,能计算矩形的面积。解:源程序:#includeiostream.hclassRectangle{public:Rectangle(inttop,intleft,intbottom,intright);~Rectangle(){}intGetTop()const{returnitsTop;}intGetLeft()const{returnitsLeft;}intGetBottom()const{returnitsBottom;}intGetRight()const{returnitsRight;}voidSetTop(inttop){itsTop=top;}voidSetLeft(intleft){itsLeft=left;}voidSetBottom(intbottom){itsBottom=bottom;}voidSetRight(intright){itsRight=right;}intGetArea()const;private:intitsTop;intitsLeft;intitsBottom;intitsRight;};Rectangle::Rectangle(inttop,intleft,intbottom,intright){itsTop=top;itsLeft=left;itsBottom=bottom;itsRight=right;}intRectangle::GetArea()const{intWidth=itsRight-itsLeft;intHeight=itsTop-itsBottom;return(Width*Height);}intmain(){RectangleMyRectangle(100,20,50,80);intArea=MyRectangle.GetArea();coutArea:Area\n;return0;}程序运行输出:Area:3000UpperLeftXCoordinate:204.2设计一个程序。设计一个立方体类Box,它能计算并输出立方体的体积和表面积。#includeiostreamusingnamespacestd;classBox{public:floatL;floatgetBMJ(){returnL*L*6;}floatgetTJ(){returnL*L;}Box(floatin){L=in;}};voidmain(){Boxr(10);cout边长:10\n表面积:r.getBMJ()\n体积:r.getTJ();}4.3设计一个汽车类vehicle,包含的数据成员有车轮个数wheels和车重weight。小车类car是它的派生类,其中包含载人数passenger_load。每个类都有相关数据的输出方法。在主程序中定义一个car类对象,对其车轮个数、车重、载人数进行设置并显示。#includeiostream.h#includeiomanip.hclassvehicle//汽车类,包含车轮数和车重{public:vehicle(int,float);intget_wheels();floatget_weight();voidshow();protected:intwheels;//车轮数floatweight;//车重量,单位吨};classcar:privatevehicle//小车类是汽车类的私有派生类,包含载客数{public:car(intwheels,floatweight,intpassengers);intget_passengers();voidshow();private:intpassenger_load;//额定载客数};classtruck:privatevehicle//卡车类是汽车类的私有派生类,包含载人数和载重量{public:truck(intwheels,floatweight,intpassengers,floatmax_load);intget_passengers();voidshow();private:intpassenger_load;//额定载人数floatpayload;//额定载重量,单位吨};vehicle::vehicle(intwl,floatwt){wheels=wl;weight=wt;}intvehicle::get_wheels(){returnwheels;}floatvehicle::get_weight(){returnweight;}voidvehicle::show(){cout车轮数:setw(3)wheels个\n车身重:setw(3)weight吨\n;}car::car(intwheels,floatweight,intpassengers):vehicle(wheels,weight){passenger_load=passengers;}intcar::get_passengers(){returnpassenger_load;}voidcar::show(){cout车型:小汽车\n;vehicle::show();cout载客数:setw(3)passenger_load人\n\n;}truck::truck(intwheels,floatweight,intpassengers,floatmax_load):vehicle(wheels,weight){passenger_load=passengers;payload=max_load;}inttruck::get_passengers(){returnpassenger_load;}voidtruck::show(){cout车型:大卡车\n;vehicle::show();cout载人数:setw(3)passenger_load人\n;cout载重量:setw(3)payload吨\n\n;}voidmain(){cout私有派生类相关验证程序MicrosoftVisualC++构建\nsetw(45)(C)2009/10/28郭呈祥\n\n;carcar(4,3.6,5);//小车类参数分别为“车轮数、车身重以及额定载客数”trucktru(10,10.0,3,35);//卡车类参数分别为“车轮数、车身重、载人数以及额定载重量”cout数据验证结果如下:\n;car.show();tru.show();}4.4定义一个日期类Date,包含年、月、日三个数据成员,以及一个求第二天日期的成员函数和输出日期的成员函数。#includeiostream.hclassDate{private:intyear,month,day;public:Date(inty,intm,intd){year=y;month=m;day=d;}voidnextday();voiddisplay(){coutyear/month/dayendl;}};voidDate::nextday(){inttotaldays[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};day++;intleap=(year%400==0||year%4==0&&year%100!=0);if(daytotaldays[leap][month-1]){day=1;month++;if(month12){month=1;year++;}}}voidmain(){intd,m,y;cout请输入年、月、日:\n;cinymd;Dated1(y,m,d);cout今天是:;d1.display();d1.nextday();cout明天是:;d1.display();}4.5设计一个程序。定义一个圆类,有数据成员半径radis(半径),计算圆的面积和周长,写出主函数测试你编写的类。#includeiostreamusingnamespacestd;floatpi=3.14;classR{public:floatradis;floatgetMJ(){returnradis*radis*pi;}floatgetZC(){returnradis*2*pi;}R(floatin){radis=in;}};voidmain(){Rr(10);cout半径:10\n周长:r.getZC()\n面积:r.getMJ();}4.6编写一个程序。用名为max的函数模板计算三个参数中的最大值,用整数、字符和浮点数测试所编写的程序。#includeiostreamusingnamespacestd;templatetypenameOOMax(Oa,Ob,Oc){returnab?ac?a:c:bc?b:c;}voidmain(){coutMax(9,1,8)endl;coutMax(7.0,3.2,9.9)endl;coutMax('a','z','b');}4.7编写一个程序计算“三角形、正方形、圆形”三种图形的面积。要求:a)抽象出一个基类base;b)在其中说明一个虚函数用来求面积;c)利用派生类定义“三角形、正方形、圆形”;d)编写主函数并测试。#includeiostreamusingnamespacestd;classbase{public:virtualfloatgetMJ(){returnH*W;}floatH,W;};classR:publicbase{public:floatgetMJ(){returnH*H*3.14;}R(floatin){H=in;}};classA:publicbase{public:floatgetMJ(){return(H*W)/2;}A(floatin_H,floatin_w){H=in_H;W=in_w;}};classS:publicbase{public:floatgetMJ(){returnH*H;}S(floatin){H=in;}};voidmain(){Rr(10);Aa(10,5);Ss(10);cout圆:边长:10\n面积:r.getMJ()endl\n三角:高:10,底:5\n面积:a.getMJ()endl35.\n正方形:边长:10\n面积:s.getMJ();}4.8编程实现抽象类Employee,派生类Manger和HourlyWorker,Employee有数据成员姓名name和工号ID,Manger有数据成员sal,代表经理的月工资,HourlyWorker有wage和hours,分别代表钟点工的每小时的工资数和月工作时数,定义的所有类中必须包含构造函数、析构函数、修改和获取所有数据成员的成员函数,以及虚函数来计算职员的工资、输出职员的姓名name和工号ID。(#includeiostream#includestringusingnamespacestd;classEmployee{public:stringname;intid;virtualintgetSal(){return0;}Employee():name(未命名),id(0){};~Employee(){cout析构\n;}voidset(stringN,intI){id=I;name=N;}voidshowSal(){cout\n姓名:nameendl工号:idendl工资:getSal()endl;}};classManger:publicEmployee{public:Manger(int
本文标题:c++编程题
链接地址:https://www.777doc.com/doc-7028791 .html