1、 面向对象程序设计(C++) 课程设计报告 班级:191152 学号:1001188 姓名:夏体凡 日期:7月4日 目录 一、原创性声明…………………………………………3 二、题目与规定…………………………………………4 三、需求分析…………………………………………….4 四、概要设计……………………………….…………..5 五、详细设计…………………………………….……5 六、测试……………………………………………….….7 七、结论 …………
2、………………………………….…..9 八、附录 ………………………………………….….11 原创性声明: 本人声明报告者中内容和程序为本人独立完毕,引用她人文献、数据、图件、资料均已明确标注出。除标注内容外,不包括任何形式她人成果,无侵 权和抄袭行为,并乐意承担由此而产生后果。 作者签字: 时间: 指引教师评语: 课程成绩: 程序界面
3、 (10 分) 程序构造和功能 (40 分) 程序安全和效率 (40 分) 报告 (10 分) 指引教师签字: 时间 题目与规定 设计如下类,其功能和某些成员如下: Object: 抽象类,所有物体均有价值(profit)属性;Point: 点位置三维空间;Line Segment(线段),Rectangle,Cuboid, Square,Cube,Circle,Cylinder 功能:可以实现上述物体移动(move),放大(z
4、oomin),缩小(zoomout),大小比较(compare),打印物品信息(cout<<编号、面积、容积和价值)等操作,且所有物品对象实现自动编号。 移动:Line类对象移动其中点,Rectangle、Square和Circle: 移动重心,Cubiod、Cube和Cylinder:移动底面重心 放大和缩小:以倍数为参数,进行相应组件放大和缩小 判断:空间内某一点(Point)与否在另一物体内;线段(Line)与否和另一物体相交 默认比较方式:Line: 比较长度,Rectangle、Square和Circle:比较面积 Cubiod、Cube和Cylinder:比较体积。同维度
5、或不同维度)空间内不同类物体之间可进行大小比较。 相等返回0,不大于返回-1、不不大于返回1 再设计一种容器类(Container). 容器具备最大容量属性。 功能: 能容纳以上定义各种3D物品(Cylinder,Cube和Cuboid), 实现添加一种物品(add),移除容器里一种物品(remove),重载[], 排序:不变化物品在容器中位置(下标),把物品id按照排序成果(依照物品某一核心字)返回 ; 附加功能:给定一定数量物品(假设物品总容量超过容器最大容量),挑选某些装入容器中,设计算法实现所装物品总价值最大。 需求分析 1
6、本次上机题目重要运用继承和派生建立各种类,这些类都重要一种点类point继承而来,然后每一种类设计一种头文献和一种源文献,然后最后设计一种操作类container,用来解决移动(move),放大(zoomin),缩小(zoomout),大小比较(compare),打印物品信息(cout<<编号、面积、容积和价值)等操作,最后设计一种主函数,用来控制整个系统。 2.系统环境、运营规定:Visual Studio 概要设计 从设计规定可知,要有一种最底层抽象基类即Object类,然后就是各种类之间关系,Point继承Object——然后Line继承Point,添加leng
7、th新成员;Circle继承Point,添加新成员R;Square继承Point,添加L1新成员;Rectangle继承Point,添加L1,L2新成员;最后Cylinder继承Circle,添加height新成员;Cube继承Square,添加high新成员;Cuboid继承Rectangle,添加high新成员。这样就完毕了继承与派生类设计。除了这些类,还必要有操作,否则就没故意义了,要实现上述物体move,zoomin,zoomout,compare,打印物品信息等操作,这里要用到运营时多态,即需要基类指针,将在下一条详细讲,这里要做是函数成员设计,这规定在每个类要有同名函数,即(ove
8、rriding a virtual function),返回值类型,参数类型等都得同样,并且基类要是virtual function。再设计一种容器类Container,所有物品对象实现自动编号。这里容器要用vector来做,咱们在Container类中定义vector
9、长度(lenth),体积(volume)计算办法,同步定义各个类放大,缩小,移动,打印功能执行办法。 在操作类中定义所有操作移动(move),放大(zoomin),缩小(zoomout),大小比较(compare),打印物品信息,其中,移动,放大,缩小几种功能返回该类所在原函数进行, 大小比较在操作函数中进行,采用办法是有体积比较体积,没体积比面积,没面积比长短, 打印操作还分为所有打印和单个打印。 在主函数中包括 了一种功能菜单,用来以便操作,功能菜单通过指针连向所有功能函数。 功能实现示意图 圆类 菜 单 操作 圆柱类
10、 继承 线 类 显示 点 类 正方体 矩形类 长方体类 测试 添加物体信息 移动物体位置 放大或缩小物体 比较 打印 删除 结论 本学期较上学期来说做了更多课程设计,有了更多实践经验,对于本次设计,我感觉知识综合能力较强,毕竟c++已经学习了一种学期,对于类设计也不能仅仅局限于一种或俩个,要学会从分散到集中,从少到多。
11、本次上机重要目是熟悉继承与派生(inheritance and polymorphism),这是c++核心内容,也是比较难掌握,但总来说,这次上机实践我收获还是很大,我从中找到我错误和局限性之处。对于这个面向对象程序设计,我个人以为这个程序长处在于:
1.在做过程中使用了多文献构造,便于编译和改错;
2.类设计,在程序中共有10个类,,每个类均有自己独立数据成员和函数成员,声明和定义都是分开,都是按照类原则设计来做;
3.实现了运营时多态,在Container类中用了vector
12、上浮现了某些错误,再加上理解题意出了问题,导致派生关系弄得不对或复杂了,尚有就是关于继承类构造函数没有写好,忽视了核心点,即基类成员和新增成员使用和关系,尚有就是有功能没有实现,像给定一定数量物品,假设物品总容量超过容器最大容量,挑选某些装入容器中,设计算法实现所装物品总价值最大。程序安全性局限性,存在内存泄漏等等, C++课本教学知识已经学完了,但对于我来说还差得很远很远,光靠这一年时间是远远不够,需要更多知识灌溉和实践,C++之路曼曼其修远兮,吾将上下而求索。在后来学习生活中多练习,用勤补拙,不断提高自己c++水平。要成为一名合格程序员尚有许多路要走,我会始终坚持下去!
13、
附录
**************************point.h*******************************
#ifndef POINT_H
#define POINT_H
#include
14、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 double getvolume() {
15、 return 0;}
virtual void movexyz();
virtual void zoom();
virtual void print();
~Point() {}
};
#endif
************************line.h************************
#ifndef LINE_H
#define LINE_H
#include
16、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); double getlength(); double getvolume() { return 0;} double getare
17、a() { return 0;}
void movexyz();
void zoom();
void print();
~Line() {}
};
#endif
*********************circle.h*****************************
#ifndef CIRCLE_H
#define CIRCLE_H
#include
18、s 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
19、}
void movexyz();
void zoom();
void print();
~Circle() {}
};
#endif
*********************container.h********************
#ifndef CONTAINER_H
#define CONTAINER_H
#include
20、ude"Cuboid.h"
using namespace std;
class Container {
private:
Point *pi[20];
Point po[20];
Line li[20];
Circle ci[20];
Cylinder cy[20];
Square sq[20];
Rectangle re[20];
Cuboid cub[20];
Cube cu[20];
const double V = 50;
double Vc = 0;
vector
21、 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 22、>
#include 23、0;}
double getlength() { return 0;}
double getvolume();
void movexyz();
void zoom();
void print();
~Cube() {};
};
#endif
****************************cuboid.h******************************
#ifndef CUBOID_H
#define CUBOID_H
#include 24、clude"Rectangle.h"
using 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_High);
double getvolume();
25、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 26、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,double c,double d,double e,double f);
double ge 27、tvolume();
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 28、ude 29、area();
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 30、
#include"Point.h"
using namespace std;
class Square :public Point {
protected:
double R;
public:
Square();
Square(Square & c) :Point(c) {
R = c.R;
}
Square(double p,double x1,double x2,double x3,double r);
double getarea();
double getlength() { return 0;}
double getvol 31、ume() { return 0;}
void movexyz();
void zoom();
void print();
~Square() {};
};
#endif
*****************************object.h*********************
#ifndef OBJECT_H
#define OBJECT_H
#include 32、
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,dou 33、ble 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坐标:" << m_z << endl;
}
void Point::movexyz() {
double a,b,c;
cout << "请重新输入坐标:" << endl;
cin >> a >> b >> c;
m_x = a;
m_y = b;
m_z = c;
}
void Po 34、int::zoom() {
cout << "点不能放大或缩小" << endl;
}
void Point::print() {
cout << "点:" << endl;
cout << "价值:" << Profit << endl;
cout << "坐标:" << "(" << m_x << "," << m_y << "," << m_z << ")" << endl;
}
*************************line.cpp************************
#include"Line.h"
35、
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 << "请重新输入中点坐标:" << endl;
cin >> a >> b >> c;
m_x = a;
m_y = b;
m_z = c;
}
void Line::zoom() {
cou 36、t << "这是一种线段,请输入你放大或缩小时倍数" << endl;
double a;
cin >> a;
m_length = a*m_length;
}
void Line::print() {
cout << "线段" << endl;
cout << "价值:" << Profit << endl;
cout << "坐标:" << "(" << m_x << "," << m_y << "," << m_z << ")" << endl;
cout << "长度:" << m_length << endl;
}
****** 37、circle.cpp**********************
#include"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 38、
cin >> a >> b >> c;
m_x = a;
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 << "坐标:" << "(" << m_x << "," << m_y << ", 39、" << m_z << ")" << endl;
cout << "面积:" << PI*m_R*m_R << endl;
}
***********************************container.cpp******************************
#include"Container.h"
#include"Point.h
void Container::Addobject() {
int a = 1,b = 0,i = 0;
double p,x,y,z,l,l1,l2;
while (a) {
for ( 40、int j = 0;j<(int)Po.size();j++) {
Vc += Po[j]->getvolume();
}
if (Vc < 50) {
cout << "请选取要添加物品: 1 点, 2 线, 3 圆, 4 圆柱, 5 正方形, 6 长方形, 7 正方体, 8 长方体:" << endl;
int j;
cin >> j;
switch (j) {
case 1:{
cout << "请输入点数据(价值 坐标):" << endl;
cin >> p >> x >> y >> z;
41、 Point po1(p,x,y,z);
pi[b] = &po1;
Po.push_back(pi[b]);
po[b]= po1;
Po[i] = &po[b];
i++;
b++;
}break;
case 2:{
cout << "请输入线数据(价值 中点坐标 长度):" << endl;
cin >> p >> x >> y >> z >> l;
Line li1(p,x,y,z,l);
Point s(p,x,y,z);
pi[b] = &s;
42、 Po.push_back(pi[b]);
li[b] = li1;
Po[i] = &li[b];
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);
pi[b] = &s;
Po.push_back(pi[b]);
ci[b] = ci1; 43、
Po[i] = &ci[b];
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;
pi[b] = &s;
if (Vc + S->getvolume() <= 50) {
Po.p 44、ush_back(pi[b]);
cy[b] = cy1;
Po[i] = &cy[b];
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);
pi[b] = &s;
P 45、o.push_back(pi[b]);
sq[b] = sq1;
Po[i] = &sq[b];
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);
pi[b] = &s;
Po.push_back(pi[b]);
re 46、[b] = re1;
Po[i] = &re[b];
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) {
pi[b] = &s 47、
Po.push_back(pi[b]);
cu[b] = cu1;
Po[i] = &cu[b];
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);
Poin 48、t s(p,x,y,z),*S;
S = &cub1;
if (Vc + S->getvolume() <= 50) {
pi[b] = &s;
Po.push_back(pi[b]);
cub[b] = cub1;
Po[i] = &cub[b];
i++;
b++;
}
else cout << "容器已满" << endl;
}break;
}
cout << "与否要继续 (1 继续 ,0 结束)" << endl;
cin >> a;
49、
}
else { cout << "容器已满" << endl;break;}
}
}
void Container::Moveposition() {
int a = 1;
while (a) {
int b;
cout << "请输入需要移动序号:" << endl;
cin >> b;
Po[b]->print();
Po[b]->movexyz();
Po[b]->print();
delete Po[b];
cout << "与否要继续 (1 继续 ,0 结束)" << endl;
cin > 50、> a;
}
}
void Container::Zoominorout() {
int i = 1;
while (i) {
cout << "请输入想要放大或缩小物体序号:" << endl;
int a;
cin >> a;
Po[a]->print();
Po[a]->zoom();
Po[a]->print();
cout << "与否要继续 (1 继续 ,0 结束):" << endl;
cin >> i;
}
}
void Container::Compare() {
cout << "请输






