Topical Information

This lab will help you practice with operator overloading (mixed with classes).

Program Information

A friend has a point class that needs some help with its interface. You volunteer to help add operator overloading to the class. (No, really, you did. Don't you remember?)

You decide to overload operators for the distance between two points (operator-), input (operator>>), output (operator<<), equality (operator==), and inequality (operator!=). After a bit of arguing, you decide to also overload for midpoint (operator/).

You decide to also fix up your friend's class by adding operator= and 'missing' constructors.

Don't forget to write a small test application to show that the class still works -- new operators and all!

Thought Provoking Questions

  1. Which operators are members and which are non-members? Do any have to be members?

  2. Which operators should be const? What other methods might well be made const? In general, what is the rule which determines if a method should be made const?

  3. What type do equality and and inequality return? Input? Output? Assignment?

  4. Do you agree with your friend's decision to use operator/ for midpoint? Why/Why not?

  5. Why didn't you overload operators for less than, greater than, etc.?

  6. Your friend wanted to overload operators for the flip and shift methods, too (~ and += respectively). Why did you talk them out of it? Why wasn't this a good idea?

  7. Just because you've added operators, should you necessarily remove the old methods that did these jobs?

This assignment is (Level 2).

Options

Add (Level 1) to overload operator[] to return the x or y part of the point. For instance:

   Point p(3.2, 9.8);
   double my_x;
   my_x = p['x'];   // my_x should be 3.2

would store the Point p's x value (3.2) for storage in my_x.

More Thought Provoking Questions

  1. Should the programmer be able to do:
        p['X'] = 2.0;
    
    to change the X-coordinate of a Point object p?

  2. If you were going to allow such behavior, how would you do it?