How can I define '±' to use as a operator?

I'm a noob, who is trying to create a simple calculator to practice the whats in the "Basics of C++", but I'm finding it hard to create an operator for ± so I don't have to type (x-y),(x+y). Can you please help me think up any ideas?
Last edited on
± is not a proper mathematical operator, it's an abbreviated way of writing two separate expressions as one. It's only sort of meaningful when writing equations (e.g. x = ±1 means that x is either 1 or -1), it doesn't have a well-defined meaning when expressing a calculation.

For example, if input "1 ± 2 ± 3" into your hypothetical program, which of these do I mean?
* Calculate (1 + 2 + 3) and (1 - 2 - 3).
* Calculate (1 + 2 - 3) and (1 - 2 + 3).
* Calculate all four of the above.
You really don't want such ambiguities in a calculator.
The standard defines a list of operators that can be overloaded

+ - * / % ^
& | ~ ! , =
< > <= >= ++ --
<< >> == != && ||
+= -= /= %= ^= &=
|= *= <<= >>= [] ()
-> ->* new new [] delete delete []

You can use any of these to attain your objective, for example:
1
2
3
4

return-type class::operator %(class& const rhs)const{
                   (*this->x - rhs.y),(*this->x + rhs.y),
      return .... }


Though I'm struggling to imagine what exactly the function should return if the expression is (x-y),(x+y). If it returns void what use would be such an operator unless you just wish to print to console in which case it would be:
1
2
3
4

void class::operator %(class& const rhs)const{
                cout<<" ("<<*this->x<<" -"<< rhs.y<<"),("<<*this->x<<" +"<< rhs.y<<"),/n";
      }



Are you sure it shouldn't be (x-y)*(x+y) in which case the return type would depend on the types of x, y.


Topic archived. No new replies allowed.