面向对象程序设计报告_第1页
面向对象程序设计报告_第2页
面向对象程序设计报告_第3页
面向对象程序设计报告_第4页
面向对象程序设计报告_第5页
已阅读5页,还剩38页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、面向对象程序设计(c+) 课程设计报告班级:191152学号:20151001188姓名:夏体凡日期:2016年7月4日 目录一、原创性申明3二、题目与要求4三、需求分析.4四、概要设计.5五、 详细设计.5六、测试.7七、结论 .9八、附录 .11 原创性声明:本人声明报告者中的内容和程序为本人独立完成,引用他人的文献、数据、 图件、资料均已明确标注出。除标注内容外,不包含任何形式的他人成果,无侵 权和抄袭行为,并愿意承担由此而产生后果。作者签字: 时间:指导教师评语:课程成绩:程序界面 (10 分)程序结构和功能 (40 分)程序安全和效率 (40 分)报告 (10 分)指导教师签字: 时

2、间 题目与要求设计如下类,其功能和部分成员如下:object: 抽象类,所有的物体都有价值(profit)属性;point: 点的位置三维空间;line segment(线段),rectangle,cuboid, square,cube,circle,cylinder功能:能够实现上述物体的移动(move),放大(zoomin),缩小(zoomout),大小比较(compare),打印物品信息(cout编号、面积、容积和价值)等操作,且所有物品的对象实现自动编号。移动: line类对象移动其中点,rectangle、square和circle: 移动重心,cubiod、cube和cylinde

3、r: 移动底面重心放大和缩小:以倍数为参数,进行相应组件的放大和缩小判断:空间内某一点(point)是否在另一物体内;线段(line)是否和另一物体相交默认比较方式:line: 比较长度,rectangle、square和circle:比较面积cubiod、cube和cylinder: 比较体积。同维度(或不同维度)空间内的不同类物体之间可进行大小比较。 相等返回0,小于返回-1、大于返回1再设计一个容器类(container). 容器具有最大容量属性。功能: 能容纳以上定义的各种3d物品(cylinder,cube和cuboid), 实现添加一个物品(add),移除容器里的一个物品(remo

4、ve),重载, 排序:不改变物品在容器中的位置(下标),把物品的id按照排序结果(根据物品某一关键字)返回 ;附加功能:给定一定数量的物品(假设物品的总容量超过容器最大容量),挑选部分装入容器中,设计算法实现所装物品的总价值最大。 需求分析1.本次上机题目主要运用继承和派生建立多个类,这些类都主要一个点类point继承而来,然后每一个类设计一个头文件和一个源文件,然后最后设计一个操作类container,用来处理移动(move),放大(zoomin),缩小(zoomout),大小比较(compare),打印物品信息(cout编号、面积、容积和价值)等操作,最后设计一个主函数,用来控制整个系统。

5、2.系统环境、运行要求:visualstudio2015 概要设计从设计的要求可知,要有一个最底层的抽象基类即object类,然后就是多个类之间的关系,point继承object然后line继承point,添加length新成员;circle继承point,添加新成员r;square继承point,添加l1新成员;rectangle继承point,添加l1,l2新成员;最后cylinder继承circle,添加height新成员;cube继承square,添加high新成员;cuboid继承rectangle,添加high新成员。这样就完成了继承与派生类的设计。除了这些类,还必须有操作,否则就

6、没有意义了,要实现上述物体的move,zoomin,zoomout,compare,打印物品信息等操作,这里要用到运行时的多态,即需要基类的指针,将在下一条详细讲,这里要做的是函数成员的设计,这要求在每个类要有同名函数,即(overridingavirtualfunction),返回值类型,参数类型等都得一样,而且基类的要是virtualfunction。再设计一个容器类container,所有物品的对象实现自动编号。这里的容器要用vector来做,我们在container类中定义vectorpo数据成员既可实现自动编号又可实现运行时的多态。详细设计在所有的类point,line segmen

7、t(线段),rectangle,cuboid, square,cube,circle,cylinder中包含一个构造函数,一个析构函数,并且在每个类中定义该类面积(area),长度(lenth),体积(volume)的计算方法,同时定义各个类放大,缩小,移动,打印功能的执行方法。在操作类中定义所有的操作移动(move),放大(zoomin),缩小(zoomout),大小比较(compare),打印物品信息,其中,移动,放大,缩小几个功能返回该类所在的原函数进行,大小比较在操作函数中进行,采用的方法是有体积比较体积,没体积比面积,没面积比长短,打印操作还分为全部打印和单个打印。在主函数中包含 了

8、一个功能菜单,用来方便操作,功能菜单通过指针连向所有功能函数。功能实现示意图圆类菜单操作圆柱类 继承线类显示点类正方体矩形类长方体类测试添加物体的信息移动物体的位置放大或缩小物体比较 打印删除结论本学期较上学期来说做了更多的课程设计,有了更多的实践经验,对于此次的设计,我感觉知识的综合能力较强,毕竟c+已经学习了一个学期,对于类的设计也不能仅仅局限于一个或俩个,要学会从分散到集中,从少到多。本次上机的主要目的是熟悉继承与派生(inheritanceandpolymorphism),这是c+的核心内容,也是比较难掌握的,但总的来说,这次上机实践我的收获还是很大的,我从中找到我的错误和不足之处。对

9、于这个面向对象的程序设计,我个人认为这个程序的优点在于:1.在做的过程中使用了多文件的结构,便于编译和改错;2.类的设计,在程序中共有10个类,每个类都有自己独立的数据成员和函数成员,声明和定义都是分开的,都是按照类的标准设计来做的;3.实现了运行时的多态,在container类中用了vectorpo,即指向基类的指针容器,。在缺点方面,首先是在继承与派生时对概念弄得不是很清晰,逻辑上出现了一些错误,再加上理解题意出了问题,导致派生关系弄得不对或复杂了,还有就是关于继承类的构造函数没有写好,忽略了关键点,即基类成员和新增成员的使用和关系,还有就是有功能没有实现,像给定一定数量的物品,假设物品的

10、总容量超过容器最大容量,挑选部分装入容器中,设计算法实现所装物品的总价值最大。程序安全性不足,存在内存泄漏等等, c+课本的教学知识已经学完了,但对于我来说还差得很远很远,光靠这一年的时间是远远不够的,需要更多的知识灌溉和实践,c+之路曼曼其修远兮,吾将上下而求索。在以后的学习生活中多练习,用勤补拙,不断提高自己的c+水平。要成为一名合格的程序员还有许多的路要走,我会一直坚持下去! 附录*point.h*#ifndef point_h#define point_h#include#include#includeobject.husing namespace std;class point :p

11、ublic object protected:double m_x, m_y, m_z;public:point() :object() m_x = m_y = m_z = 0;point(point & c) :object(c) m_x = c.m_x;m_y = c.m_y;m_z = c.m_z;point(double a, double b, double c, double d);void getpoint();virtual double getlength() return 0; virtual double getarea() return 0; virtual doubl

12、e getvolume() return 0; virtual void movexyz();virtual void zoom();virtual void print(); point() ;#endif*line.h*#ifndef line_h#define line_h#include#include#includepoint.husing namespace std;class line :virtual public point protected:double m_length;public:line() :point() m_length = 0;line(line & c)

13、 :point(c) m_length = c.m_length;line(double a, double b, double c, double d, double e);double getlength();double getvolume() return 0; double getarea() return 0; void movexyz();void zoom();void print(); line() ;#endif*circle.h*#ifndef circle_h#define circle_h#include#include#includepoint.husing nam

14、espace std;class circle :virtual public point protected:double m_r;public:circle() :point() m_r = 0;circle(circle & c) :point(c) m_r = c.m_r;circle(double a, double b, double c, double d, double e);double getarea();double getlength() return 0; double getvolume() return 0; void movexyz();void zoom();

15、void print(); circle() ;#endif*container.h*#ifndef container_h#define container_h#include#include#includeline.h#includecylinder.h#includecube.h#includecuboid.husing namespace std;class container private:point *pi20;point po20;line li20;circle ci20;cylinder cy20;square sq20;rectangle re20;cuboid cub2

16、0;cube cu20;const double v = 50;double vc = 0;vector po;public:void addobject();void moveposition();void zoominorout();void compare();void print();void deleteobject();point* & operator(int i);#endif*cube.h*#ifndef cube_h#define cube_h#include#include#includesquare.husing namespace std;class cube :pu

17、blic square protected:double m_high;public:cube();cube(double p, double x1, double x2, double x3, double r, double m_high);cube(cube & c) :square(c) m_high = c.m_high;double getarea() return 0; double getlength() return 0; double getvolume();void movexyz();void zoom();void print(); cube() ;#endif*cu

18、boid.h*#ifndef cuboid_h#define cuboid_h#include#include#includerectangle.husing namespace std;class cuboid :public rectangle protected:double m_high;public:cuboid();cuboid(cuboid & c) :rectangle(c) m_high = c.m_high;cuboid(double p, double x1, double x2, double x3, double r1, double r2, double m_hig

19、h);double getvolume();double getlength() return 0; double getarea() return 0; void movexyz();void zoom();void print(); cuboid() ;#endif*cylinder.h*#ifndef cylinder_h#define cylinder_h#include#include#includecircle.husing namespace std;class cylinder :public circle private:double m_high;public:cylind

20、er() :circle() m_high = 0;cylinder(cylinder & c) :circle(c) m_high = c.m_high;cylinder(double a, double b, double c, double d, double e, double f);double getvolume();double getlength() return 0; double getarea() return 0; void movexyz();void zoom();void print();cylinder() ;#endif*rectangle.h*#ifndef

21、 rectangle_h#define rectangle_h#include#include#includepoint.husing namespace std;class rectangle :public point protected:double r1, r2;public:rectangle();rectangle(rectangle & c) :point(c) r1 = c.r1;r2 = c.r2;rectangle(double p, double x1, double x2, double x3, double r1, double r2);double getarea(

22、);double getlength() return 0; double getvolume() return 0; void movexyz();void zoom();void print(); rectangle() ;#endif*square.h*#ifndef square_h#define square_h#include#include#includepoint.husing namespace std;class square :public point protected:double r;public:square();square(square & c) :point

23、(c) r = c.r;square(double p, double x1, double x2, double x3, double r);double getarea();double getlength() return 0; double getvolume() return 0; void movexyz();void zoom();void print(); square() ;#endif*object.h*#ifndef object_h#define object_h#include#includeconst double pi = 3.14;using namespace

24、 std;class object protected:double profit;public:object() :profit(0) object(double a) :profit(a) object() virtual void print() = 0;#endif*point.cpp*#includepoint.hpoint:point(double a, double b, double c, double d) :object(a), m_x(b), m_y(c), m_z(d) void point:getpoint() cout x坐标: m_x y坐标: m_y z坐标:

25、m_z endl;void point:movexyz() double a, b, c;cout 请重新输入坐标: a b c;m_x = a;m_y = b;m_z = c;void point:zoom() cout 点不能放大或缩小 endl;void point:print() cout 点: endl;cout 价值: profit endl;cout 坐标: ( m_x , m_y , m_z ) endl;*line.cpp*#includeline.hline:line(double a, double b, double c, double d, double e) :po

26、int(a*e, b, c, d), m_length(e) ;double line:getlength() return m_length;void line:movexyz() double a, b, c;cout 请重新输入中点坐标: a b c;m_x = a;m_y = b;m_z = c;void line:zoom() cout 这是一个线段,请输入你放大或缩小时的倍数 a;m_length = a*m_length;void line:print() cout 线段 endl;cout 价值: profit endl;cout 坐标: ( m_x , m_y , m_z )

27、 endl;cout 长度: m_length endl;*circle.cpp*#includecircle.hcircle:circle(double a, double b, double c, double d, double e) :point(a*pi*e*e, b, c, d), m_r(e) ;double circle:getarea() return(pi*m_r*m_r);void circle:movexyz() double a, b, c;cout 请重新输入圆心坐标: a b c;m_x = a;m_y = b;m_z = c;void circle:zoom()

28、 cout 这是一个圆,请输入你放大或缩小时的半径的倍数 a;m_r = a*m_r;void circle:print() cout 圆: endl;cout 价值: profit endl;cout 坐标: ( m_x , m_y , m_z ) endl;cout 面积: pi*m_r*m_r endl;*container.cpp*#includecontainer.h#includepoint.hvoid container:addobject() int a = 1, b = 0, i = 0;double p, x, y, z, l, l1, l2;while (a) for (

29、int j = 0; jgetvolume();if (vc 50) cout 请选择要添加的物品: 1 点, 2 线, 3 圆, 4 圆柱, 5 正方形, 6 长方形, 7 正方体, 8 长方体: j;switch (j) case 1: cout 请输入点的数据(价值 坐标): p x y z;point po1(p, x, y, z);pib = &po1;po.push_back(pib);pob= po1;poi = &pob;i+;b+;break;case 2: cout 请输入线的数据(价值 中点坐标 长度): p x y z l;line li1(p, x, y, z, l)

30、;point s(p, x, y, z);pib = &s;po.push_back(pib);lib = li1;poi = &lib;i+;b+;break;case 3: cout 请输入圆的数据(价值 圆心坐标 半径): p x y z l;circle ci1(p, x, y, z, l);point s(p, x, y, z);pib = &s;po.push_back(pib);cib = ci1;poi = &cib;i+;b+;break;case 4: cout 请输入圆柱的数据(值价 底面圆心坐标 圆半径 高): p x y z l l1;cylinder cy1(p,

31、x, y, z, l, l1);point s(p, x, y, z), *s;s = &cy1;pib = &s;if (vc + s-getvolume() = 50) po.push_back(pib);cyb = cy1;poi = &cyb;i+;b+;else cout 容器已满 endl;break;case 5: cout 请输入正方形的数据(价值 坐标 边): p x y z l;square sq1(p, x, y, z, l);point s(p, x, y, z);pib = &s;po.push_back(pib);sqb = sq1;poi = &sqb;i+;b+

32、;break;case 6: cout 请输入长方形的数据(价值 坐标 长 宽): p x y z l l1;rectangle re1(p, x, y, z, l, l1);point s(p, x, y, z);pib = &s;po.push_back(pib);reb = re1;poi = &reb;i+;b+;break;case 7: cout 请输入正方体的数据(价值 坐标 底面边 高(与底面边相等)): p x y z l l1;cube cu1(p, x, y, z, l, l1);point s(p, x, y, z), *s;s = &cu1;if (vc + s-ge

33、tvolume() = 50) pib = &s;po.push_back(pib);cub = cu1;poi = &cub;i+;b+;else cout 容器已满 endl;break;case 8: cout 请输入长方体的数据(价值 坐标 长 宽 高): p x y z l l1 l2;cuboid cub1(p, x, y, z, l, l1, l2);point s(p, x, y, z), *s;s = &cub1;if (vc + s-getvolume() = 50) pib = &s;po.push_back(pib);cubb = cub1;poi = &cubb;i+

34、;b+;else cout 容器已满 endl;break;cout 是否要继续 (1 继续 ,0 结束) a;else cout 容器已满 endl; break; void container:moveposition() int a = 1;while (a) int b;cout 请输入需要移动的序号: b;pob-print();pob-movexyz();pob-print();delete pob;cout 是否要继续 (1 继续 ,0 结束) a;void container:zoominorout() int i = 1;while (i) cout 请输入想要放大或缩小的物

35、体序号: a;poa-print();poa-zoom();poa-print();cout 是否要继续 (1 继续 ,0 结束): i;void container:compare() cout 请输入需要比较的2个物品的编号: a b;if (poa-getvolume() != 0 &pob- getvolume() != 0) if (poa-getvolume()pob-getvolume()cout a 物体空间大小 b 物体空间大小 getvolume() = pob-getvolume()cout a 物体空间大小 = b 物体空间大小 endl;else cout a 物体空

36、间大小 b 物体空间大小 getvolume() = 0 & pob-getvolume() = 0) if (poa-getarea() != 0 & pob-getarea() != 0) if (poa-getarea()pob-getarea()cout a 物体平面大小 b 物体平面大小 getarea() = pob-getarea()cout a 物体平面大小 = b 物体平面大小 endl;else cout a 物体平面大小 b 物体平面大小 getarea() = 0 & pob-getarea() = 0) if (poa-getlength()pob-getlength

37、() cout a 物体 长于 b 物体 getlength() = pob-getlength() cout a 物体 等于 b 物体 endl;else cout a 物体 小于 b 物体 getarea() = 0) cout a 物体平面大小 b 物体大小 endl;else cout a 物体大小 b 物体平面大小 getvolume() = 0) cout a 物体空间大小 b 物体空间大小 endl;else cout a 物体空间大小 b 物体空间大小 endl;void container:print() cout 打印单个还是所有 (1 所有, 2 单个) a;if (a

38、= 1) for (int i = 0; i(int)po.size(); i+) cout po i print();*/else if (a = 2) cout 请输入你想打印的编号: b;cout b print();else cout 输入错误 endl;void container:deleteobject() int i = 1;while (i) cout 你要删除的量 a;for (vector:iterator iter = po.begin(); iter != po.end();) if (*iter)-getvolume() = a) iter = po.erase(i

39、ter);else iter+;cout 是否要继续 (1 继续 ,0 结束) i;point* & container:operator(int i) if (i= (int)po.size()cout 下标出界 endl;else return poi;*cube.cpp*#includecube.hcube:cube() :square() m_high = 0;cube:cube(double p, double x1, double x2, double x3, double r, double m_high) : square(p*m_high*m_high*m_high, x1, x2, x3, r), m_high(m_high) double cube:getvolume() return(m_high*m_high*m_high);void cube:movexyz() do

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论