I have a project for school in C++ and I am stuck on one part: I have to overload the operators + and * to work with geometrical figures. That was no problem, but here it where it doesn’t work: I have to declare the operator as a pure virtual method, in an abstract class that all other classes derive from.
#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);
}
};
Thank you and please be patient with me, it is my first contact with C++.
virtual Figabs operator +(const Figabs& rhs)=0
perhaps? :) – Tenoperator+
should return an object (not a reference), so it can't return an abstract class (whichFigabs
is here because it has a pure virtual function). This is tricky to solve within the constraints of the assignment, at least partly because the assignment is enforcing bad design.operator+
should generally not be a member function (GOTW #4, part 5). – PrecognitionPoint
is a abstract class and as such cannot be value-returned from the operator you are defining (which is clearly different than what is specified in your abstract-base). The reason it "works" when that op is removed is simply becausePoint
is no longer abstract. James' answer hits pretty solid on this. – Quintuplicateoperator+
over a class hierarchy: this problem is addressed in Advanced Programming Styles and Idioms_, by James Coplien. The book itself is pretty dated, but as far as I know, no one has come up with a better solution for this particular problem since it was written. – PomfretBase*
as an argument. All of the constructors of derived classes are private, and there are public static factory functions, along the lines ofBase Derived::construct(...) { return Base( new Derived ); }
. The Base uses the clone function to do a deep copy in assignment and the copy constructor, and deletes what it points to in the destructor. – Pomfretoperator+
. – Organza