11
votes

How can I make pure virtual function a operator+(); function. wheh ı do like this in base class int operator+()=0; compiler gives error . in derive class operator+() function compiler say that derive class cannot make . because following class is abstract I know that I cannot create object of abstract classes but now I try to make derive class object.

Here is the code

#include <iostream>
using namespace std;
class ana {
    protected :
    int x;

    public :
    virtual int operator+()=0;
    virtual void operator=(ana&)=0;
    };

class baba : public ana{
    public:
    baba(int k){x=k;}
    int   operator+(baba &ali){
        int k;
        k=x+ali.x;
        return k;
    }
   void operator=(baba &ali){
       x=ali.x;
       }


    };

int main(){
    baba k(4);

    return 0;
    }
6
Please show us the code.Timo Geusch
It is rather unlikely that you have found a sensible design involving operators implemented as virtual member functions. I've never seen such beasts. What do you want to achieve doing that?sbi
To whoever upvoted this - in what sense is this a good question?anon
@Neil Butterworth: After Troubadour posted the "code" from the "answer" below, I upvoted from -1 to 0. Not sure it deserves to go higher, but I didn't think that with this part resolved it deserved a negative score. I expect if somebody disagrees, they'll vote it back down again.andand
@SamHammamy: I find the idea of subtracting states dubious. What does it even mean to subtract initial_state from error_state? It certainly seems to violate the first of my basic rules for operator overloading.sbi

6 Answers

13
votes

Your vague mentions of code are essentially impossible to follow. Answering your question "How can I make pure virtual function a operator+(); function", there's absolutely no secret to it, e.g. consider the following trivial program:

#include <iostream>

class base {
public:
  virtual int operator+(int) const = 0;
};

class deriv: public base {
public:
  int operator+(int) const {return 23;}
};

int main() {
  deriv d;
  base& b = d;
  std::cout << b + 15 << std::endl;
  return 0;
}

This compiles and runs just fine and emits 23 as expected. Whatever it is that you're doing wrong, obviously it must therefore be different from this (and probably not connected to the specific issue of having an operator overload be pure virtual).

Edit: (as per comments, added const to the method just in case you want to call it w/a const base& -- note that other answers have also omitted this const; and, also per comments):

If you want to be able to also do 15 + b, just add a free-standing function for that very purpose, say just before main:

inline int operator+(int i, const base&b) {
    return b + i;
}
6
votes

If you are searching for a standard operator+() implementation, then unfortunately that is impossible:

class X { 
public:
     virtual X operator+(X const &rhs) = 0;
};

This code cannot compile, because the compiler cannot return an abstract X class by value.

3
votes

Note : Question have been updated, making this answer less valid.

If you're description is correct, you're just forgetting to use the virtual keyword to specify the operator as virtual...

class Addable{
public:
    virtual int operator+ const ( const Addable& other ) = 0; // pure virtual
    virtual int operator+ const ( int number ) = 0; // pure virtual
};
1
votes

I see two issues that you should address, or at the very least better understand. They both boil down to the fact that you have not resolved the pure virtual declaration in your base class, ana:

1) operator+(): Your class baba defines a different + operator than the base class. In particular, ana::operator+() is a unary + operator (accepts only one operand), while baba::operator+(baba& ali) is a binary operator (accepts two operands) on baba. You need to decide which to use and use it. If you want to use the binary + (which given your definition in baba is what I think you want) then you declare in ana:

virtual int operator+(const ana&) const =0;

and in baba:

virtual int operator+(const ana& ali) const {return x+ali.x; }  

Why this needs to be a pure virtual method in ana, since x is defined in ana is curious. It will do interesting things if you have other derived classes that do things differently (losing commutivity is likely to result). But I expect you have your reasons, so I won't question further.

2) operator=(ana&): You also have a problem with the assignment operator declarations. Again, you're not resolving the pure virtual. In ana you have: virtual void operator=(ana&)=0; while in baba you have: void operator=(baba &ali). The arguments are different since baba& is not the same as ana&; so the pure virtual declaration is again not resolved. In order to fix this, you probably want to change the declaration in ana to:

virtual void operator=(const ana&)=0

and in baba:

virtual void operator=(const ana& ali) { x=ali.x; }

I have similar concerns as to why you want to declare this as a pure virtual since, again, it assumes that different derived classes will implement it differently leading to interesting behavior. Again, I'm sure you have your reasons.

I do hope this helps.

0
votes

The syntax for a pure virtual function would be something like:

class X { 
public:
     virtual int operator+(X const &rhs) = 0;
};

Note, however, that you only rarely want to do this -- you typically want an overload of operator+ to be implemented as a free function, to allow conversion of the left operand (if necessary).

0
votes

 #include 
using namespace std;
class ana {
    protected :
    int x;
    public :

virtual int operator+()=0; virtual void operator=(ana&)=0; };

class baba:public ana{ public: baba(int k){x=k;} int operator+(baba &ali){ int k; k=x+ali.x; return k; } void operator=(baba &ali){ x=ali.x; }

};

int main(){ baba k(4);

return 0; }<code>what is wrong here?