




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、4.1测试一个名为rectangle的矩形类,其属性为矩形的左下角与右上角两个点的坐标,能计算矩形的面积。解: 源程序:#include <iostream.h>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()
2、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
3、 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()1 / 11Rectangle MyRectangle (100, 20, 50, 80 );int Area = MyRectangle.GetArea();cou
4、t << "Area: " << Area << "n"return 0;程序运行输出:Area: 3000Upper Left X Coordinate: 204.2设计一个程序。 设计一个立方体类Box,它能计算并输出立方体的体积和表面积。#include <iostream>using namespace std; class Box public: float L;
5、0;float getBMJ()return L*L*6; float getTJ()return L*L; Box(float in)L=in; void main() Box r(10); cout<<"边长:10n表面积:"<<r.getBMJ()<<
6、;"n体积:"<<r.getTJ(); 4.3设计一个汽车类vehicle,包含的数据成员有车轮个数wheels和车重weight。小车类car是它的派生类,其中包含载人数passenger_load。每个类都有相关数据的输出方法。在主程序中定义一个car类对象,对其车轮个数、车重、载人数进行设置并显示。#include <iostream.h>#include <iomanip.h>class vehicle /汽车类,包含车轮数和车重 public: vehi
7、cle(int, float); int get_wheels(); float get_weight(); void show(); protected: int wheels; /车轮数 float weight; /车重量,单位吨;class car:private vehicle /小车类是汽车类的私有派生类,包含载客数 public:
8、;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
9、,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;
10、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
11、=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
12、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<
13、<" 人n" cout<<"载重量:"<<setw(3)<<payload<<" 吨nn"void main () cout<<"私有派生类相关验证程序 Microsoft Visual C+构建n" <<setw(45)<<"(C) 2009/10/28 郭呈祥nn" car car(4, 3.6, 5);
14、 /小车类参数分别为“车轮数、车身重以及额定载客数” truck tru(10, 10.0, 3, 35); /卡车类参数分别为“车轮数、车身重、载人数以及额定载重量” cout<<"数据验证结果如下:n" car.show(); tru.show();4.4定义一个日期类Date,包含年、月、日三个数据成员,以及一个求第二天日期的成员函数和输出日期的成员函数。#include <iostream.h> class Date private:int yea
15、r,month,day;public:Date(int y, int m, int d)year=y;month=m;day=d;void nextday();void display()cout<<year<<"/"<<month<<"/"<<day<<endl;void Date:nextday() int totaldays212=31,28,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31;da
16、y+;int leap=(year%400=0|year%4=0&&year%100!=0);if(day>totaldaysleapmonth-1)day=1; month+;if(month>12)month=1;year+;void main() int d,m,y;cout<<"请输入年、月、日:n"cin>>y>>m>>d;Date d1(y,m,d); cout<<"今天是:"d1.display();d1.nextday();cout<<&q
17、uot;明天是:"d1.display(); 4.5设计一个程序。定义一个圆类,有数据成员半径radis(半径),计算圆的面积和周长,写出主函数测试你编写的类。#include <iostream> using namespace std; float pi = 3.14; class R public: float radis; float getMJ()retu
18、rn radis*radis*pi; float getZC()return radis*2*pi; R(float in)radis=in; void main() R r(10); cout<<"半径:10n周长:"<<r.getZC()<<"n面积:"
19、<<r.getMJ(); 4.6 编写一个程序。用名为max的函数模板计算三个参数中的最大值,用整数、字符和浮点数测试所编写的程序。 #include <iostream> using namespace std; template <typename O> O Max(O a,O b, O c)return a>b?a>c?a:c:b>
20、;c?b:c; void main() cout<<Max(9,1,8)<<endl; cout<<Max(7.0,3.2,9.9)<<endl; cout<<Max('a','z','b'); 4.7编写一个程序计算“三角形、正方形、圆形”三种图形的面积。要求:a)抽象出一个基类base;b)在其
21、中说明一个虚函数用来求面积;c)利用派生类定义“三角形、正方形、圆形”;d)编写主函数并测试。#include <iostream> using namespace std; class base public: virtual float getMJ()return H*W; float H,W; class R:public base public: &
22、#160;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
23、0;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<<"圆:边长:10n面积:"<<r.getM
24、J()<<endl <<"n三角:高:10,底:5n面积:"<<a.getMJ()<<endl 35. <<"n正方形:边长:10n面积:"<<s.getMJ(); 4.8编程实现抽象类Employee,派生类Manger和HourlyWorker,Employee有数据成员姓名name和工号ID,Manger有数据成员sal,代表经理的月工资,HourlyWorker有wage和hours,分别代表钟点工的
25、每小时的工资数和月工作时数,定义的所有类中必须包含构造函数、析构函数、修改和获取所有数据成员的成员函数,以及虚函数来计算职员的工资、输出职员的姓名name和工号ID。(#include <iostream> #include <string> using namespace std; class Employee public: string name; int id; &
26、#160;virtual int getSal()return 0; Employee():name("未命名"),id(0); Employee()cout<<"析构n" void set(string N,int I)id=I; name=N; void showSal() cout<&
27、lt;"n姓名:"<<name<<endl <<"工号:"<<id<<endl <<"工资:"<<getSal()<<endl; class Manger:public Employee public: M
28、anger(int S)sal=S; int getSal()return sal; int sal; class HourlyWorker:public Employee public: HourlyWorker(int W,int H)wage=W;hours=H; int getSal()return wage*hours; int
29、60;wage,hours; void main() HourlyWorker h(10,20); h.set("小时工A",777); h.showSal(); Manger m(999); m.set("经理A",888); m.showSal(); 4.9定义一个处理日期的类TDate,它有3个私有数据成员:Month,D
30、ay,Year和若干个公有成员函数,并实现如下要求:构造函数重载;成员函数设置缺省参数;定义一个友元函数来打印日期;定义一个非静态成员函数设置日期;可使用不同的构造函数来创建不同的对象。include <iostream> using namespace std; class TDate public: TDate():Year(1900),Month(1),Day(1); TDate(int Y, int M=1,
31、 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<<in.Year<<"年"
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 柑橘种植园农业生物技术应用研究考核试卷
- 箱包行业智能制造装备考核试卷
- 船舶机舱监控系统的高效运行与优化考核试卷
- 演出市场多元化发展研究考核试卷
- 林木育种的抗鳞茧蜂选择与策略考核试卷
- 2025年医用卫生材料敷料合作协议书
- 2025年SDH光纤传输系统项目合作计划书
- 人大公务员考试题及答案
- 法考等级考试试题及答案
- 2025年GSM移动通信手机项目建议书
- 物联网技术及在油气生产中的应用(2015石油论坛)
- 【黄芪的化学成分与药理作用研究进展综述报告6700字(论文)】
- 单位工程施工组织设计实训任务书
- 1.技术交流PPT-输电线路分布式故障诊断装置
- 医院护理培训课件:《跌倒坠床PDCA分析》
- 七年级历史下册图片题剖析
- 中医内科方歌大全
- 管线打开作业安全管理标准
- 沟通与谈判第讲非语言沟通
- Unit+6+Section+A+3a-3c 人教版八年级英语下册
- 肾移植术后十宜十不宜专家讲座
评论
0/150
提交评论