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;
}
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...If you are searching for a standard operator+() implementation, then unfortunately that is impossible:
This code cannot compile, because the compiler cannot return an abstract X class by value.
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), whilebaba::operator+(baba& ali)
is a binary operator (accepts two operands) onbaba
. You need to decide which to use and use it. If you want to use the binary + (which given your definition inbaba
is what I think you want) then you declare inana
:and in
baba
:Why this needs to be a pure virtual method in
ana
, sincex
is defined inana
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 inbaba
you have:void operator=(baba &ali)
. The arguments are different sincebaba&
is not the same asana&
; so the pure virtual declaration is again not resolved. In order to fix this, you probably want to change the declaration inana
to:and in
baba
: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.
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:
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/aconst base&
-- note that other answers have also omitted thisconst
; 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 beforemain
:The syntax for a pure virtual function would be something like:
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).