欢迎来到天天文库
浏览记录
ID:47426716
大小:91.00 KB
页数:10页
时间:2020-01-10
《c++编程题》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
4.1测试一个名为rectangle的矩形类,其属性为矩形的左下角与右上角两个点的坐标,能计算矩形的面积。解:源程序:#includeclassRectangle{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();cout<<"Area:"<using namespace std; class Box {public: float L; float getBMJ(){return L*L*6;} float getTJ(){return L*L;} Box(float in){L=in;} }; void main() { Box r(10); cout<<"边长:10 表面积:"<#includeclassvehicle //汽车类,包含车轮数和车重{ 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<<"车轮数:"<classDate{private:intyear,month,day;public:Date(inty,intm,intd){year=y;month=m;day=d;}voidnextday(); voiddisplay(){cout<totaldays[leap][month-1]){day=1;month++;if(month>12){month=1;year++;}}}voidmain(){intd,m,y;cout<<"请输入年、月、日: ";cin>>y>>m>>d;Dated1(y,m,d);cout<<"今天是:";d1.display();d1.nextday();cout<<"明天是:";d1.display();}4.5设计一个程序。定义一个圆类,有数据成员半径radis(半径),计算圆的面积和周长,写出主函数测试你编写的类。#include using namespace std; float pi = 3.14; class R {public: float radis; float getMJ(){return radis*radis*pi;} float getZC(){return radis*2*pi;} R(float in){radis=in;} }; void main() { R r(10); cout<<"半径:10 周长:"< using namespace std; template O Max(O a,O b, O c){return a>b?a>c?a:c:b>c?b:c;} void main() { cout< using namespace std; class base {public: virtual float getMJ(){return H*W;} float H,W; }; class R:public base {public: float getMJ(){return H*H*3.14;} R(float in){H=in;} }; class A:public base {public: float getMJ(){return (H*W)/2;} A(float in_H,float in_w){H=in_H;W=in_w;} }; class S:public base {public: float getMJ(){return H*H;} S(float in){H=in;} }; void main() { R r(10); A a(10,5); S s(10); cout<<"圆:边长:10 面积:"< #include using namespace std; class Employee {public: string name; int id; virtual int getSal(){return 0;} Employee():name("未命名"),id(0){}; ~Employee(){cout<<"析构 ";} void set(string N,int I){id=I; name=N; } void showSal() { cout<<" 姓名:"< using namespace std; class TDate { public: TDate():Year(1900),Month(1),Day(1){;} TDate(int Y, int M=1, int D=1){Month=M;Day=D;Year=Y;}void set(int Y=1990, int M=1, int D=1){Month=M;Day=D;Year=Y;} riend void show(TDate& in); private: int Month,Day,Year; }; void show(TDate& in){cout<#include"MyClass.h"voidmain(){ MyClassobj(3.3); cout<
此文档下载收益归作者所有
举报原因
联系方式
详细说明
内容无法转码请点击此处