c++面向对象菱形
C++ Object Orientated Rhombus
我有问题与我已经给出的任务,问题是我找不到任何资源,无论如何类似于我已经给出的代码。我已经通读了那么多的文件,试图找到相似之处,但没有找到任何有用的。
我需要帮助试图理解这段代码,以及如何使用它来创建一个菱形。我唯一不能掌握的是如何创建属于shape类的菱形形状。在菱形上应用一个质心,然后使用push_back方法添加顶点。不幸的是,这个推回方法需要使用,我失败的考试只是使用drawLine(10,10,40,10);在我想要的地方画线。
我要花整整一周的时间来处理这个问题,所以我应该迅速做出回应。
//This is the rhombus.cpp file
#include "rhombus.h"
Rhombus::Rhombus(Vertex point, int radius) : Shape(point)
{
if((radius>centroid.getX()/2) || (radius>centroid.getY()/2)) // Inteded to be a y?
{
cout << "Object must fit on screen." << endl;
system("pause");
exit(0);
}
Rhombus shape1(20, 20);
shape1.plotVertices();
}
void Rhombus::plotVertices()
{
//vertices.push_back(Vertex(centroid.getX(), centroid.getY() + radius));
//vertices.push_back(Vertex(centroid.getX(), centroid.getY()));
//vertices.push_back(Vertex(centroid.getX(), centroid.getY()));
//vertices.push_back(Vertex(centroid.getX(), centroid.getY()));
}
// This is the rhombus.h file
#include "shape.h"
class Rhombus : public Shape
{
int radius;
void plotVertices();
Rhombus(Vertex point, int radius = 10);
int area();
int perimeter();
};
// This is the shape.cpp file
#include "shape.h"
Shape::Shape(Vertex point) : centroid(point)
{
// constructs a shape
}
void Shape::drawShape()
{
list<Vertex>::iterator current = vertices.begin();
list<Vertex>::iterator previous = vertices.begin();
while(current!=vertices.end())
{
Console::gotoXY((*current).getX(),(*current).getY());
cout << "*";
if(current!=vertices.begin())
drawLine((*current).getX(),(*current).getY(), (*previous).getX(), (*previous).getY());
previous = current;
current++;
}
previous = vertices.begin();
//Debug assertion error here.
drawLine(vertices.back().getX(), vertices.back().getY(), vertices.front().getX(), vertices.front().getY());
}
void Shape::drawLine(int x1, int y1, int x2, int y2)
{
bool steep = (abs(y2 - y1) > abs(x2 - x1));
if(steep)
{
swap(x1, y1);
swap(x2, y2);
}
if(x1 > x2)
{
swap(x1, x2);
swap(y1, y2);
}
int dx = x2 - x1;
int dy = abs(y2 - y1);
float error = dx / 2.0f;
int ystep = (y1 < y2) ? 1 : -1;
int y = y1;
int maxX = x2;
for(int x=x1; x<maxX; x++)
{
if(steep)
{
Console::gotoXY(y,x);
cout << "*";
}
else
{
Console::gotoXY(x,y);
cout << "*";
}
error -= dy;
if(error < 0)
{
y += ystep;
error += dx;
}
}
}
double Shape::round(double x)
{
if (ceil(x+0.5) == floor(x+0.5))
{
int a = (int) ceil(x);
if (a%2 == 0)
return ceil(x);
else
return floor(x);
}
else
return floor(x+0.5);
}
void Shape::outputStatistics()
{
}
// This is the shape.h file
#pragma once
#include "console.h"
#include "vertex.h"
#include <iostream>
#include <list>
#include <cstdlib>
#include <cmath>
using namespace std;
#define PI 3.14159265358979323846
class Shape
{
list<Vertex>::iterator itr;
protected:
list<Vertex> vertices;
Vertex centroid;
void drawLine(int x1, int y1, int x2, int y2);
Shape(Vertex point);
double round(double x);
public:
void drawShape();
virtual int area() = 0;
virtual int perimeter() = 0;
virtual void outputStatistics();
void rotate(double degrees);
void scale(double factor);
};
正如您所看到的,Rhombus
已经是Shape
(class Rhombus : public Shape
)的子类,因此您只需创建Rhombus
的实例即可实现所有的神奇。
Shape
的定义使得传递给它的Vertex
(作为point
参数)用于自动初始化centroid
实例变量;所以你可以使用centroid
作为任何需要质心相关数据的操作的质心,无论是从Shape
还是从它的一个子类(如Rhombus
)。
同样,列表vertices
作为实例变量可用于Shape
及其所有子类。如果你看一下其余的代码(例如Shape::drawShape
),你会注意到vertices
是如何被用来操纵当前形状的顶点的。
你在这里要做的与那非常相似。例如,
Rhombus::Rhombus(Vertex point, int radius) : Shape(point)
{
if((radius>centroid.getX()/2) || (radius>centroid.getY()/2)) // Inteded to be a y?
{
cout << "Object must fit on screen." << endl;
system("pause");
exit(0);
}
// create vertices for a rhombus with horizontal and vertical diagonals
vertices.push_back(Vertex(point.getX() - radius, point.getY()));
vertices.push_back(Vertex(point.getX(), point.getY() - radius));
vertices.push_back(Vertex(point.getX() + radius, point.getY()));
vertices.push_back(Vertex(point.getX(), point.getY() + radius));
}
当你在Rhombus::Rhombus
(构造函数)内部时,你已经在中刚刚创建的菱形;您不必再次创建Rhombus
对象。你只需要通过添加顶点和定义质心(这已经完成了)来"装饰"实例。
想象一下你正在从Shape
中创建一个Rhombus
;您需要创建4个顶点,并将它们添加到跟踪所有顶点列表的结构中。
- 分段错误(内核转储) C++面向对象编程
- 由于缺少类模板,NVCC 编译面向对象代码时出现问题
- 在C++面向对象程序中,谁负责删除传递给构造函数的对象
- 是否有一种设计模式或面向对象的基本原则来处理这种共享资源的情况?
- 对"车辆的 vtable"的未定义引用 - 面向对象的编程练习
- 关于 C++ 中面向对象中的 decltype 类型
- 多态性的面向对象方法
- 成员功能在面向对象的框架中返回std :: vector.size()的效率
- 以面向对象的方式解析自定义数据包
- 面向对象编程 - 位域私有结构的公共参考
- 面向对象的编程-C 练习
- 如何使用面向对象的技术在 win32 和 c++ 中创建编辑控件
- C++面向对象的字典程序
- 面向对象架构,用于简单的基于着色器的GL程序
- 面向对象 成本计算C++不起作用
- 面向对象编程和对象组合,如何放置组合属性?
- 我在哪里可以找到一些 c++ 中的好例子来研究面向对象的概念
- 面向对象的设计选择
- 面向对象编程错误
- 程序语言和面向对象语言的游戏设计差异