




已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
4.1测试一个名为rectangle的矩形类,其属性为矩形的左下角与右上角两个点的坐标,能计算矩形的面积。解: 源程序:#include class Rectanglepublic:Rectangle (int top, int left, int bottom, int right);Rectangle () int GetTop() const return itsTop; int GetLeft() const return itsLeft; int GetBottom() const return itsBottom; int GetRight() const return itsRight; void SetTop(int top) itsTop = top; void SetLeft (int left) itsLeft = left; void SetBottom (int bottom) itsBottom = bottom; void SetRight (int right) itsRight = right; int GetArea() const;private:int itsTop;int itsLeft;int itsBottom;int itsRight;Rectangle:Rectangle(int top, int left, int bottom, int right)itsTop = top;itsLeft = left;itsBottom = bottom;itsRight = right;int Rectangle:GetArea() constint Width = itsRight-itsLeft;int Height = itsTop - itsBottom;return (Width * Height);int main()Rectangle MyRectangle (100, 20, 50, 80 );int Area = MyRectangle.GetArea();cout Area: Area n;return 0;程序运行输出:Area: 3000Upper Left X Coordinate: 204.2设计一个程序。 设计一个立方体类Box,它能计算并输出立方体的体积和表面积。#includeusingnamespacestd;classBoxpublic:floatL;floatgetBMJ()returnL*L*6;floatgetTJ()returnL*L;Box(floatin)L=in;voidmain()Boxr(10);cout边长:10n表面积:r.getBMJ()n体积:r.getTJ();4.3设计一个汽车类vehicle,包含的数据成员有车轮个数wheels和车重weight。小车类car是它的派生类,其中包含载人数passenger_load。每个类都有相关数据的输出方法。在主程序中定义一个car类对象,对其车轮个数、车重、载人数进行设置并显示。#include #include class vehicle/汽车类,包含车轮数和车重public:vehicle(int, float);int get_wheels();float get_weight();void show();protected:int wheels;/车轮数float weight;/车重量,单位吨;class car:private vehicle/小车类是汽车类的私有派生类,包含载客数public:car(int wheels, float weight, int passengers);int get_passengers();void show();private:int passenger_load;/额定载客数;class truck:private vehicle/卡车类是汽车类的私有派生类,包含载人数和载重量public:truck(int wheels,float weight,int passengers,float max_load);int get_passengers();void show();private:int passenger_load;/额定载人数float payload;/额定载重量,单位吨;vehicle:vehicle(int wl,float wt)wheels=wl;weight=wt;int vehicle:get_wheels()return wheels;float vehicle:get_weight()return weight;void vehicle:show()cout车轮数:setw(3)wheels 个n车身重:setw(3)weight 吨n;car:car(int wheels, float weight, int passengers):vehicle(wheels, weight)passenger_load=passengers;int car:get_passengers ()return passenger_load;void car:show()cout车 型:小汽车n;vehicle:show();cout载客数:setw(3)passenger_load 人nn;truck:truck(int wheels, float weight, int passengers, float max_load):vehicle(wheels, weight)passenger_load=passengers;payload=max_load;int truck:get_passengers()return passenger_load;void truck:show()cout 车 型:大卡车n;vehicle:show();cout载人数:setw(3)passenger_load 人n;cout载重量:setw(3)payload 吨nn;void main ()cout私有派生类相关验证程序 Microsoft Visual C+构建nsetw(45)(C) 2009/10/28 郭呈祥nn;car car(4, 3.6, 5);/小车类参数分别为“车轮数、车身重以及额定载客数”truck tru(10, 10.0, 3, 35);/卡车类参数分别为“车轮数、车身重、载人数以及额定载重量”cout数据验证结果如下:n;car.show();tru.show();4.4定义一个日期类Date,包含年、月、日三个数据成员,以及一个求第二天日期的成员函数和输出日期的成员函数。#include class Date private:int year,month,day;public:Date(int y, int m, int d)year=y;month=m;day=d;void nextday();void display()coutyear/month/daytotaldaysleapmonth-1)day=1; month+;if(month12)month=1;year+;void main() int d,m,y;coutymd;Date d1(y,m,d); cout今天是:;d1.display();d1.nextday();cout明天是:;d1.display(); 4.5设计一个程序。定义一个圆类,有数据成员半径radis(半径),计算圆的面积和周长,写出主函数测试你编写的类。#includeusingnamespacestd;floatpi=3.14;classRpublic:floatradis;floatgetMJ()returnradis*radis*pi;floatgetZC()returnradis*2*pi;R(floatin)radis=in;voidmain()Rr(10);cout半径:10n周长:r.getZC()n面积:r.getMJ();4.6 编写一个程序。用名为max的函数模板计算三个参数中的最大值,用整数、字符和浮点数测试所编写的程序。#includeusingnamespacestd;templateOMax(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)编写主函数并测试。#includeusingnamespacestd;classbasepublic:virtualfloatgetMJ()returnH*W;floatH,W;classR:publicbasepublic:floatgetMJ()returnH*H*3.14;R(floatin)H=in;classA:publicbasepublic:floatgetMJ()return(H*W)/2;A(floatin_H,floatin_w)H=in_H;W=in_w;classS:publicbasepublic:floatgetMJ()returnH*H;S(floatin)H=in;voidmain()Rr(10);Aa(10,5);Ss(10);cout圆:边长:10n面积:r.getMJ()endln三角:高:10,底:5n面积:a.getMJ()endl35.n正方形:边长:10n面积:s.getMJ();4.8编程实现抽象类Employee,派生类Manger和HourlyWorker,Employee有数据成员姓名name和工号ID,Manger有数据成员sal,代表经理的月工资,HourlyWorker有wage和hours,分别代表钟点工的每小时的工资数和月工作时数,定义的所有类中必须包含构造函数、析构函数、修改和获取所有数据成员的成员函数,以及虚函数来计算职员的工资、输出职员的姓名name和工号ID。(#include#includeusingnamespacestd;classEmployeepublic:stringname;intid;virtualintgetSal()return0;Employee():name(未命名),id(0);Employee()cout析构n;voidset(stringN,intI)id=I;name=N;voidshowSal()coutn姓名:nameendl工号:idendl工资:getSal()endl;classManger:publicEmployeepublic:Manger(intS)sal=S;intgetSal()returnsal;intsal;classHourlyWorker:publicEmployeepublic:HourlyWorker(intW,intH)wage=W;hours=H;intgetSal()returnwage*hours;intwage,hours;voidmain()HourlyWorkerh(10,20);h.set(小时工A,777);h.showSal();Mangerm(999);m.set(经理A,888);m.showSal();4.9定义一个处理日期的类TDate,它有3个私有数据成员:Month,Day,Year和若干个公有成员函数,并实现如下要求:构造函数重载;成员函数设置缺省参数;定义一个友元函数来打印日期;定义一个非静态成员函数设置日期;可使用不同的构造函数来创建不同的对象。includeusingnamespacestd;classTDatepublic:TDate():Year(1900),Month(1),Day(1);TDate(intY,intM=1,intD=1)Month=M;Day=D;Year=Y;voidset(intY=1990,intM=1,intD=1)Month=M;Day=D;Year=Y;riendvoidshow(TDate&in);private:intMonth,Day,Year;voidshow(TDate&in)coutin.Year年in.Month月in.Day日n;voidmain()TDatet1;TDatet2(2014);TDatet3(2015,6,5);show(t1);show(t2);show(t3);t3.set(1999);show(t3);4.10 回答下面问题。下面列出了由三个文件main.cpp、MyClass.h和MyClass.cpp组成的一个程序。文件main.cpp中实现了主函数;文件MyClass.h中定义了类MyClass;文件MyClass.cpp中实现了类MyClass的成员函数。题中没有给出三个文件的完整实现。仔细
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 生态循环农业2025年技术模式与经济效益关联性实证研究报告
- 2025年新能源汽车轻量化技术创新与碰撞安全性能平衡研究报告
- KTV酒吧行业经营管理作业指导书
- 物流行业智能物流仓储与配送优化方案
- 建筑项目设计施工一体化合作协议
- 自考专业(人力资源管理)模拟试题及答案详解(典优)
- 自考公共课测试卷参考答案详解
- 重难点解析北师大版8年级数学上册期中试题【达标题】附答案详解
- QE工程师绩效考核表
- 2025年增强现实(AR)在工业设备操作培训中的应用效果评估报告
- 《KANO模型培训》课件
- 四川省2024年高等职业教育单独招生考试中职类语文试题及答案
- 实验室危化品安全管理培训
- 复苏室患者的交接流程
- 老旧小区改造给排水施工方案
- 【人教版化学】选择性必修1 知识点默写小纸条(答案背诵版)
- DB21-T 2523-2015矿山地质环境恢复治理规程
- 2024天津高考英语试题及答案
- 实验室中央空调施工方案
- 幼儿园 中班语言绘本《章鱼先生卖雨伞》
- 中医学藏象学说课件
评论
0/150
提交评论