我有一个 C++ 学校项目,但我被困在一个方面:我必须重载运算符 + 和 * 才能处理几何图形。这没问题,但在这里它不起作用:我必须将运算符声明为纯虚方法,在所有其他类派生的抽象类中。
#include<iostream>
using namespace std;
class Figabs {
protected:
int fel;
public:
int getFEL() { return fel; }
virtual Figabs operator +()=0; /*this is where I get an error: function returning abstract class “Figabs” is not allowed : function Figabs::operator+ is a pure virtual function */
};
class Coord {
public:
int cx, cy;
public:
Coord (){
cx = cy = 0;
}
Coord (const int x, const int y) {
cx = x;
cy = y;
}
Coord (const Coord &din) {
cx = din.cx;
cy = din.cy;
}
~Coord () { }
void setX(const int val) { cx = val; } ;
void setY(const int val) { cy = val; };
int getX() { return cx; }
int getY() { return cy; }
};
class Point : public Coord, public Figabs { //one of the figures
public:
Point() {
setX(0);
setY(0);
fel = 0;
}
Point(const int x, const int y): Coord (x,y) {
fel = 0;
}
Point(const Point &din): Coord (din) {
fel = din.fel;
}
~Point() { }
Point operator +(const Coord &vector) { /*this works perfectly when I delete the declaration from the abstract class Figabs, but I don’t know how to make them work together */
int xp = cx + vector.cx;
int yp = cy + vector.cy;
return (Point (xp, yp));
}
Point operator *(const Coord &vector) {
Point temp;
temp.cx = cx * vector.cx;
temp.cy = cy * vector.cy;
return (temp);
}
};
谢谢,请耐心等待,这是我第一次接触 C++。