c++ polymorphism of operator overloading
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 abstr开发者_JAVA百科act 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;
}
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;
}
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.
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
};
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.
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).
#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?
精彩评论