Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pure virtual operator

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++.

like image 638
Casandra Avatar asked May 22 '13 10:05

Casandra


People also ask

What is a pure virtual function?

A pure virtual function or pure virtual method is a virtual function that is required to be implemented by a derived class if the derived class is not abstract. Classes containing pure virtual methods are termed "abstract" and they cannot be instantiated directly.

What is the use of pure virtual function in C++?

A pure virtual function is a virtual function in C++ for which we need not to write any function definition and only we have to declare it. It is declared by assigning 0 in the declaration. An abstract class is a class in C++ which have at least one pure virtual function.

Can pure virtual function have body C++?

Pure virtual functions (when we set = 0 ) can also have a function body.

What is a pure virtual function in C++ Mcq?

Explanation: Pure virtual function is a virtual function which has no definition/implementation in the base class.


2 Answers

As other posters have pointed out, the assignment is far from trivial, and operator+ isn't normally a member. There are two issues which should be addressed:

  1. If you support `FigAbs + Coord`, then you should also support `Coord + FigAbs`. The first can be a member (there's no real problem there); the second, if it is to be a member, must be a member of `Coord`, which is probably not what is wanted.
  2. Any reasonable implementation of `operator+` must return by value. And you can't (normally) return a polymorphic class by value; you need something like the letter-envelope idiom for this to work: the base class must look something like:
    class Figure : BinaryOperators<Figure, Coord>
    {
        Figure* myImpl;
    public:
        Figure& operator+=( Coord const& translation )
        {
            myImpl->operator+=( translation );
            return *this;
        }
    };
    
    Of course, you'll need factory methods for correctly instantiating `Figure` for each different type, a virtual `clone` function, and copy constructor, assignment and destructor which support deep copy. (`BinaryOperators` is a template class which implements `operator+` in terms of `operator+=`; this is the usual way to provide the binary operators.)

Finally, I would argue that this is operator overloading abuse. The notion of addition doesn't apply to geometrical figures. What you're doing is called translation, and the logical solution is to provide a member function which does it, not to overload addition.

like image 164
James Kanze Avatar answered Oct 13 '22 00:10

James Kanze


Figabs contains a pure virtual member function virtual Figabs operator +()=0; this means you cannot instantiate Figabs

consider:

virtual Figabs& operator +()=0; 
/*Now you will not be returning an actual instance but can return derived class instances*
like image 29
DuncanACoulter Avatar answered Oct 12 '22 23:10

DuncanACoulter