Point2D class objects
Point2D class objects
这是我需要做的:添加一个新的数据成员,字符串颜色到你的Point2D类,以及一个新的getter和setter函数。创建一个 Point2D 对象并设置其颜色。然后创建一个 Point3D 颜色并尝试设置其颜色。此 setColor 行为是否可用于 Point3D 类?为什么或为什么不呢?
这是我的代码:
#include<iostream>
#include<vector>
using namespace std;
class Point2D
{
friend class SPALops;
protected:
float x;
float y;
protected:
float getX(){
return x;
};
float getY(){
return y;
};
void setX(float xc){
x = xc;
};
void setY(float yc){
y = yc;
};
Point2D(int xcoord, int ycoord){
x = xcoord;
y = ycoord;
};
};
class Point2D : Point2D
{
friend class SPALops;
public:
Point2D(int x, int y) : Point2D(x,y){}
Point2D() : Point2D(0,0){}
float getX(){
return this->Point2D::getX();
};
float getY(){
return this->Point2D::getY();
};
void setX(float x){
this->Point2D::setX(x);
};
void setY(float y){
this->Point2D::setY(y);
};
};
class RectangleImplementation{
friend class SPALops;
protected:
Point2D ll;
Point2D ur;
RectangleImplementation(float llx, float lly, float urx, float ury){
ll.setX(llx); ll.setY(lly);
ur.setX(urx); ur.setY(ury);
}
void setLLx(float x){
ll.setX(x);
};
void setLLy(float y){
ll.setY(y);
};
void setURx(float x){
ur.setX(x);
};
void setURy(float y){
ur.setY(y);
};
float getLLx(){
return ll.getX();
};
float getLLy(){
return ll.getY();
};
float getURx(){
return ur.getX();
};
float getURy(){
return ur.getY();
};
vector<vector<float> > getPointList(){
vector<vector<float> > v(4);
vector<float> llv(2); llv[0] = ll.getX(); llv[1] = ll.getY();
v[0] = llv;
vector<float> luv(2); luv[0] = ll.getX(); luv[1] = ur.getY();
v[1] = luv;
vector<float> ruv(2); ruv[0] = ur.getX(); ruv[1] = ur.getY();
v[2] = ruv;
vector<float> rlv(2); rlv[0] = ur.getX(); rlv[1] = ll.getY();
v[3] = rlv;
return v;
};
void printPointList(){
vector<vector<float>> v = this->getPointList();
cout << "ll = " << v[0][0] << " , " << v[0][1] << endl;
cout << "lu = " << v[1][0] << " , " << v[1][1] << endl;
cout << "ru = " << v[2][0] << " , " << v[2][1] << endl;
cout << "rl = " << v[3][0] << " , " << v[3][1] << endl;
};
};
class Rectangle : RectangleImplementation{
friend class SPALops;
public:
Rectangle(Point2D &p, Point2D &q) : RectangleImplementation(p.getX(), p.getY(), q.getX(), q.getY()){};
Rectangle(float llx, float lly, float urx, float ury): RectangleImplementation(llx,lly,urx,ury){};
float getLLx(){
return this->RectangleImplementation::getLLx();
};
float getLLy(){
return this->RectangleImplementation::getLLy();
};
float getURx(){
return this->RectangleImplementation::getURx();
};
float getURy(){
return this->RectangleImplementation::getURy();
};
void setLLx(float x){
this->RectangleImplementation::setLLx(x);
};
void setLLy(float y){
this->RectangleImplementation::setLLy(y);
};
void setURx(float x){
this->RectangleImplementation::setURx(x);
};
void setURy(float y){
this->RectangleImplementation::setURx(y);
};
void printPointList(){
cout << "In rectangle: " << endl;
cout << "ll = " << ll.getX() << " , " << this->RectangleImplementation::getLLy() << endl;
cout << "ru = " << this->getURx() << " , " << this->RectangleImplementation::getURy() << endl;
};
};
class SPALops{
public:
static bool touches(Rectangle &r, Point2D &p){
vector<vector<float> > v = r.RectangleImplementation::getPointList();
if((v[0][0] == p.getX() and v[0][1] == p.getY()) or
(v[1][0] == p.getX() and v[1][1] == p.getY()) or
(v[2][0] == p.getX() and v[2][1] == p.getY()) or
(v[3][0] == p.getX() and v[3][1] == p.getY()))
{
return true;
}
else
{
return false;
}
};
};
int main(){
Point2D p(10,10);
Point2D q(15,15);
Point2D s(20,20);
Point2D t(10,12);
Rectangle r(p,q);
r.printPointList();
cout << "Do rectangle 'r' and point 'p' touch = " << SPALops::touches(r,p) << endl;
cout << "Do rectangle 'r' and point 's' touch = " << SPALops::touches(r,s) << endl;
cout << "Do rectangle 'r' and point 't' touch = " << SPALops::touches(r,t) << endl;
return 0;
}
似乎我有很多错误阻止我成功运行。如果我得到任何反馈,我将不胜感激。
以下是我在您的帖子中发现的一些问题:
1)重复类。
您首先有一个Point2D
类,然后是第二个Point2D
类。
也许您希望第二类Point3D
.
2)公共继承。您的class Point2D: Point2D
是私人继承。
您可能希望使用公共继承:
class Point3D : public Point2D
3)不需要this->
符号。
访问类的成员或函数时,请直接访问它们:
int getX() const
{ return x; }
4) 不要重复父函数。无需在子类中重复父函数。 例如,子类不需要getX
方法。 这就是继承的用途 - 所以你不需要复制方法。
5) 子构造函数调用父构造函数。构造函数应如下所示:
Point3D(int new_x, int new_y, int new_z)
: Point2D(new_x, new_y),
z(new_z)
{ ; }
太多其他错误无法在一篇文章中讨论。 以上内容应该可以帮助您入门,并为您提供应用于其他类的模式。
相关文章:
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- C++核心准则 C35 对于接口类"A base class destructor should be either public and virtual, or protected and nonv
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- "Undefined class"作为is_base_of的论据
- 如何将class.cpp和class.hpp编译为一个.o文件
- '{'标记之前的预期类名,然后在预声明时无效使用不完整的类型'class class_name'
- 如何使用"derived input class"创建派生类?
- 错误:"Left of getValue must have class/struct/union"
- ' class a : b ' 和 ' class a : public b ' 之间的继承类不同
- Pybind11 Class Definition
- 在C++中使用没有合作伙伴Class/Cpp文件的头文件是否实用
- C2011 'CMemDC':Visual Studio 2019中的'class'类型重新定义
- TypeError: [c++ addon class] 不是构造函数
- C++ class template
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- Clearing Class Foo with new(pFoo) Foo()
- C++ class vs a library
- 继承类时"invalid use of incomplete type ‘class tree_node_t’"
- 使用"class"关键字,后跟未声明的标识符
- Point2D class objects