面向对象程序设计报告_第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

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

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

5、计一个主函数,用来控制整个系统。2.系统环境、运行要求:Visual Studio 2015  概要设计从设计的要求可知,要有一个最底层的抽象基类即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,打印物品信息等操作,这里要用到运行时的多态,即需要基类的指针,将在下一条详细讲,这里要做的是函数成员的设计,这要求在每个类要有同名函数,即(overriding a virtual function),返回值类型,参数类型等都得一样,而且基类的要是virtual function。再设计一个容器类Container,所有物品的对象实现自动编号。这里的容器要用vector来做,我们在Container类中定

7、义vector<Point *> Po数据成员既可实现自动编号又可实现运行时的多态。详细设计在所有的类Point,Line Segment(线段),Rectangle,Cuboid, Square,Cube,Circle,Cylinder中包含一个构造函数,一个析构函数,并且在每个类中定义该类面积(area),长度(lenth),体积(volume)的计算方法,同时定义各个类放大,缩小,移动,打印功能的执行方法。在操作类中定义所有的操作移动(move),放大(zoomin),缩小(zoomout),大小比较(compare),打印物品信息,其中,移动,放大,缩小

8、几个功能返回该类所在的原函数进行,大小比较在操作函数中进行,采用的方法是有体积比较体积,没体积比面积,没面积比长短,打印操作还分为全部打印和单个打印。在主函数中包含 了一个功能菜单,用来方便操作,功能菜单通过指针连向所有功能函数。功能实现示意图圆类菜单操作圆柱类 继承线类显示点类正方体矩形类长方体类测试添加物体的信息移动物体的位置放大或缩小物体比较 打印删除结论本学期较上学期来说做了更多的课程设计,有了更多的实践经验,对于此次的设计,我感觉知识的综合能力较强,毕竟c+已经学习了一个学期,对于类的设计也不能仅仅局限于一个或俩个,要学会从分散到集中,从少到多。本次上机的主要目的是熟悉继承与派生(i

9、nheritance and polymorphism),这是c+的核心内容,也是比较难掌握的,但总的来说,这次上机实践我的收获还是很大的,我从中找到我的错误和不足之处。对于这个面向对象的程序设计,我个人认为这个程序的优点在于:1.在做的过程中使用了多文件的结构,便于编译和改错;2.类的设计,在程序中共有10个类,每个类都有自己独立的数据成员和函数成员,声明和定义都是分开的,都是按照类的标准设计来做的;3.实现了运行时的多态,在Container类中用了vector<Point*> Po,即指向基类的指针容器,。在缺点方面,首先是在继承与派生时对概念弄

10、得不是很清晰,逻辑上出现了一些错误,再加上理解题意出了问题,导致派生关系弄得不对或复杂了,还有就是关于继承类的构造函数没有写好,忽略了关键点,即基类成员和新增成员的使用和关系,还有就是有功能没有实现,像给定一定数量的物品,假设物品的总容量超过容器最大容量,挑选部分装入容器中,设计算法实现所装物品的总价值最大。程序安全性不足,存在内存泄漏等等, C+课本的教学知识已经学完了,但对于我来说还差得很远很远,光靠这一年的时间是远远不够的,需要更多的知识灌溉和实践,C+之路曼曼其修远兮,吾将上下而求索。在以后的学习生活中多练习,用勤补拙,不断提高自己的c+水平。要成为一名合格的程序员还有许多的路要走,我

11、会一直坚持下去! 附录*point.h*#ifndef POINT_H#define POINT_H#include<iostream>#include<vector>#include"Object.h"using namespace std;class Point :public 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

12、_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 double getvolume() return 0; virtual void movexyz();virtual void zoom();virtual void print(); Point() ;#endif*line.h*#ifndef LINE_H#define LINE

13、_H#include<iostream>#include<vector>#include"Point.h"using namespace std;class Line :virtual public Point protected:double m_length;public:Line() :Point() m_length = 0;Line(Line & c) :Point(c) m_length = c.m_length;Line(double a, double b, double c, double d, double e);doub

14、le 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<iostream>#include<vector>#include"Point.h"using namespace std;class Circle :virtual public Point prot

15、ected: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();void print(); Circle() ;#endif*container.h*#if

16、ndef CONTAINER_H#define CONTAINER_H#include<iostream>#include<vector>#include"Line.h"#include"Cylinder.h"#include"Cube.h"#include"Cuboid.h"using namespace std;class Container private:Point *pi20;Point po20;Line li20;Circle ci20;Cylinder cy20;Square

17、 sq20;Rectangle re20;Cuboid cub20;Cube cu20;const double V = 50;double Vc = 0;vector<Point*> 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<iost

18、ream>#include<vector>#include"Square.h"using namespace std;class Cube :public 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 get

19、length() return 0; double getvolume();void movexyz();void zoom();void print(); Cube() ;#endif*cuboid.h*#ifndef CUBOID_H#define CUBOID_H#include<iostream>#include<vector>#include"Rectangle.h"using namespace std;class Cuboid :public Rectangle protected:double m_High;public:Cuboid

20、();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_High);double getvolume();double getlength() return 0; double getarea() return 0; void movexyz();void zoom();void print(); Cuboid() ;#endif*cylinder.h*#ifndef CYL

21、INDER_H#define CYLINDER_H#include<iostream>#include<vector>#include"Circle.h"using namespace std;class Cylinder :public Circle private:double m_high;public:Cylinder() :Circle() m_high = 0;Cylinder(Cylinder & c) :Circle(c) m_high = c.m_high;Cylinder(double a, double b, doubl

22、e 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 RECTANGLE_H#define RECTANGLE_H#include<iostream>#include<vector>#include"Point.h"using na

23、mespace 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();double getlength() return 0; double getvolume() return 0; void m

24、ovexyz();void zoom();void print(); Rectangle() ;#endif*square.h*#ifndef SQUARE_H#define SQUARE_H#include<iostream>#include<vector>#include"Point.h"using namespace std;class Square :public Point protected:double R;public:Square();Square(Square & c) :Point(c) R = c.R;Square(d

25、ouble 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<iostream>#include<vector>const double PI = 3.14;using

26、 namespace std;class Object protected:double Profit;public:Object() :Profit(0) Object(double a) :Profit(a) Object() virtual void print() = 0;#endif*point.cpp*#include"Point.h"Point:Point(double a, double b, double c, double d) :Object(a), m_x(b), m_y(c), m_z(d) void Point:getPoint() cout &

27、lt;< "x坐标:" << m_x << " " << "y坐标:" << m_y << " " << "z坐标:" << m_z << endl;void Point:movexyz() double a, b, c;cout << "请重新输入坐标:" << endl;cin >> a >> b >> c;m_

28、x = a;m_y = b;m_z = c;void Point:zoom() cout << "点不能放大或缩小" << endl;void Point:print() cout << "点:" << endl;cout << "价值:" << Profit << endl;cout << "坐标:" << "(" << m_x << ","

29、<< m_y << "," << m_z << ")" << endl;*line.cpp*#include"Line.h"Line:Line(double a, double b, double c, double d, double e) :Point(a*e, b, c, d), m_length(e) ;double Line:getlength() return m_length;void Line:movexyz() double a, b, c;cout <

30、;< "请重新输入中点坐标:" << endl;cin >> a >> b >> c;m_x = a;m_y = b;m_z = c;void Line:zoom() cout << "这是一个线段,请输入你放大或缩小时的倍数" << endl;double a;cin >> a;m_length = a*m_length;void Line:print() cout << "线段" << endl;cout <&l

31、t; "价值:" << Profit << endl;cout << "坐标:" << "(" << m_x << "," << m_y << "," << m_z << ")" << endl;cout << "长度:" << m_length << endl;*circle.cpp*#i

32、nclude"Circle.h"Circle: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 << "请重新输入圆心坐标:" << endl;cin >> a >> b >> c;m_x = a

33、;m_y = b;m_z = c;void Circle:zoom() cout << "这是一个圆,请输入你放大或缩小时的半径的倍数" << endl;double a;cin >> a;m_R = a*m_R;void Circle:print() cout << "圆:" << endl;cout << "价值:" << Profit << endl;cout << "坐标:" << &qu

34、ot;(" << m_x << "," << m_y << "," << m_z << ")" << endl;cout << "面积:" << PI*m_R*m_R << endl;*container.cpp*#include"Container.h"#include"Point.hvoid Container:Addobject() int a = 1

35、, b = 0, i = 0;double p, x, y, z, l, l1, l2;while (a) for (int j = 0; j<(int)Po.size(); j+) Vc += Poj->getvolume();if (Vc < 50) cout << "请选择要添加的物品: 1 点, 2 线, 3 圆, 4 圆柱, 5 正方形, 6 长方形, 7 正方体, 8 长方体:" << endl;int j;cin >> j;switch (j) case 1: cout << "请输入

36、点的数据(价值 坐标):" << endl;cin >> 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 << "请输入线的数据(价值 中点坐标 长度):" << endl;cin >> p >> x >> y >> z >> l;

37、Line li1(p, x, y, z, l);Point s(p, x, y, z);pib = &s;Po.push_back(pib);lib = li1;Poi = &lib;i+;b+;break;case 3: cout << "请输入圆的数据(价值 圆心坐标 半径):" << endl;cin >> p >> x >> y >> z >> l;Circle ci1(p, x, y, z, l);Point s(p, x, y, z);pib = &s;P

38、o.push_back(pib);cib = ci1;Poi = &cib;i+;b+;break;case 4: cout << "请输入圆柱的数据(值价 底面圆心坐标 圆半径 高):" << endl;cin >> p >> x >> y >> z >> l >> l1;Cylinder cy1(p, x, y, z, l, l1);Point s(p, x, y, z), *S;S = &cy1;pib = &s;if (Vc + S->get

39、volume() <= 50) Po.push_back(pib);cyb = cy1;Poi = &cyb;i+;b+;else cout << "容器已满" << endl;break;case 5: cout << "请输入正方形的数据(价值 坐标 边):" << endl;cin >> p >> x >> y >> z >> l;Square sq1(p, x, y, z, l);Point s(p, x, y, z);pib

40、 = &s;Po.push_back(pib);sqb = sq1;Poi = &sqb;i+;b+;break;case 6: cout << "请输入长方形的数据(价值 坐标 长 宽):" << endl;cin >> 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;

41、Poi = &reb;i+;b+;break;case 7: cout << "请输入正方体的数据(价值 坐标 底面边 高(与底面边相等)):" << endl;cin >> 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->getvolume() <= 50) pib = &s;Po.push

42、_back(pib);cub = cu1;Poi = &cub;i+;b+;else cout << "容器已满" << endl;break;case 8: cout << "请输入长方体的数据(价值 坐标 长 宽 高):" << endl;cin >> p >> x >> y >> z >> l >> l1 >> l2;Cuboid cub1(p, x, y, z, l, l1, l2);Point s(p, x

43、, y, z), *S;S = &cub1;if (Vc + S->getvolume() <= 50) pib = &s;Po.push_back(pib);cubb = cub1;Poi = &cubb;i+;b+;else cout << "容器已满" << endl;break;cout << "是否要继续 (1 继续 ,0 结束)" << endl;cin >> a;else cout << "容器已满" <&l

44、t; endl; break; void Container:Moveposition() int a = 1;while (a) int b;cout << "请输入需要移动的序号:" << endl;cin >> b;Pob->print();Pob->movexyz();Pob->print();delete Pob;cout << "是否要继续 (1 继续 ,0 结束)" << endl;cin >> a;void Container:Zoominorout

45、() int i = 1;while (i) cout << "请输入想要放大或缩小的物体序号:" << endl;int a;cin >> a;Poa->print();Poa->zoom();Poa->print();cout << "是否要继续 (1 继续 ,0 结束):" << endl;cin >> i;void Container:Compare() cout << "请输入需要比较的2个物品的编号:" <<

46、endl;int a, b;cin >> a >> b;if (Poa->getvolume() != 0 &&Pob-> getvolume() != 0) if (Poa->getvolume()>Pob->getvolume()cout << a << "物体空间大小" << ">" << b << "物体空间大小" << endl;else if (Poa->getvolum

47、e() = Pob->getvolume()cout << a << "物体空间大小" << "=" << b << "物体空间大小" << endl;else cout << a << "物体空间大小" << "<" << b << "物体空间大小" << endl;else if (Poa->getvolume(

48、) = 0 && Pob->getvolume() = 0) if (Poa->getarea() != 0 && Pob->getarea() != 0) if (Poa->getarea()>Pob->getarea()cout << a << "物体平面大小" << ">" << b << "物体平面大小" << endl;else if (Poa->getarea() = Po

49、b->getarea()cout << a << "物体平面大小" << "=" << b << "物体平面大小" << endl;else cout << a << "物体平面大小" << "<" << b << "物体平面大小" << endl;else if (Poa->getarea() = 0 &&

50、amp; Pob->getarea() = 0) if (Poa->getlength()>Pob->getlength() cout << a << "物体" << "长于" << b << "物体" << endl;else if (Poa->getlength() = Pob->getlength() cout << a << "物体" << "等于&quo

51、t; << b << "物体" << endl;else cout << a << "物体" << "小于" << b << "物体" << endl;else if (Pob->getarea() = 0) cout << a << "物体平面大小" << ">" << b << "物体

52、大小" << endl;else cout << a << "物体大小" << "<" << b << "物体平面大小" << endl;else if (Pob->getvolume() = 0) cout << a << "物体空间大小" << ">" << b << "物体空间大小" <<

53、 endl;else cout << a << "物体空间大小" << "<" << b << "物体空间大小" << endl;void Container:Print() cout << "打印单个还是所有 (1 所有, 2 单个)" << endl;int a;cin >> a;if (a = 1) for (int i = 0; i<(int)Po.size(); i+) cout <

54、;< Po i << " "/*Poi->print();*/else if (a = 2) cout << "请输入你想打印的编号:" << endl;int b;cin >> b;cout << b << " "Pob->print();else cout << "输入错误" << endl;void Container:Deleteobject() int i = 1;while (i) cout

55、 << "你要删除的量" << endl;double a;cin >> a;for (vector<Point*>:iterator iter = Po.begin(); iter != Po.end();) if (*iter)->getvolume() = a) iter = Po.erase(iter);else iter+;cout << "是否要继续 (1 继续 ,0 结束)" << endl;cin >> i;Point* & Containe

56、r:operator(int i) if (i<0 | i >= (int)Po.size()cout << "下标出界" << endl;else return Poi;*cube.cpp*#include"Cube.h"Cube: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

57、, x3, r), m_High(m_High) double Cube:getvolume() return(m_High*m_High*m_High);void Cube:movexyz() double a, b, c;cout << "请重新输入底面中心坐标:" << endl;cin >> a >> b >> c;m_x = a;m_y = b;m_z = c;void Cube:zoom() cout << "这是一个正方体,请输入你放大或缩小时的边长和高的倍数(l h)" << endl;double a;cin >> a;R = a*R;m_High = a*m_High;void Cube:print() cout << "正方体"<< endl; cout << "价值:" << Profit << en

温馨提示

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

评论

0/150

提交评论