0

I cannot understand why the unary - operator is not working. I am sure I am missing some concept. Please help.

#include <iostream>
using namespace std;

class Distance {
   private:
      int feet;             // 0 to infinite
      int inches;           // 0 to 12
   public:
      // required constructors
      Distance(){
         feet = 0;
         inches = 0;
      }
      Distance(int f, int i){
         feet = f;
         inches = i;
      }
      // method to display distance
      void displayDistance() {
         cout << "F: " << feet << " I:" << inches <<endl;
      }
      // overloaded minus (-) operator
      Distance operator- () {
         return Distance(-feet, -inches);
      }
};

int main() {
   Distance D1(11, 10), D2(-5, 11);

   -D1;                     // apply negation
   D1.displayDistance();    // display D1

   -D2;                     // apply negation
   D2.displayDistance();    // display D2

   return 0;
}

output is:

F: 11 I:10
F: -5 I:11

And I expected it to be -11 -10 and 5 and -11

Don't know where am I missing a concept. BTW I found it here

Pushan Gupta
  • 3,697
  • 5
  • 23
  • 39
  • 1
    `int n = 5; -5; cout << n << endl;` Would you expect that to print 4? – juanchopanza May 21 '17 at 13:17
  • @juanchopanza no! I would expect it to print 5 and -5 – Pushan Gupta May 21 '17 at 13:18
  • 1
    I like how on that web site they show the exact same output you are showing. Apparently they copied it from the console and it never occurred to them this output shows the discussed operator overloading did not work. Another example of why one [should not](http://stackoverflow.com/q/388242/11683) use bogus tutorials. – GSerg May 21 '17 at 13:25
  • 2
    Good example how shitty tutorialspoint is. Explaining it wrong, showing it wrong and stating thats how it works. – user3054986 May 21 '17 at 13:26
  • Ahh! I feel for new people like me searching the web for exact same link on tutorialspoint I must rename it. May help noobs like me. lol – Pushan Gupta May 21 '17 at 13:29

3 Answers3

5

These lines

-D1;                     // apply negation
-D2;                     // apply negation

do not do what you think they are doing. Let's say we have an int variable:

int var = 3;
-var; // What does this do?

What does this do? -var evaluates to -3, but does not modify var to be equal to -3. If you want to modify var, you assign to it as you normally would:

var = -var; // Now var == -3

Same deal with your Distance type. -D1 does not modify D1, and it should not modify it since that is not what the unary operator- is expected to do. Simply assign the negative version back to itself, like so:

D1 = -D1;
1

Your unary negation operator returns the new Distance object, it doesn't modify the existing object.

You should do e.g.

Distance D3 = -D1;
D3.displayDistance();
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

You return the new object but dont use it to store anywhere.

You should do D1 = -D1;

Sniper
  • 1,428
  • 1
  • 12
  • 28