The purpose of this lab is to gain further experience with defining, implementing, and using classes in C++, in particular, with overloading operators. This lab also involves further aspects of managing multiple-source-file programs.


Students with WebCT accounts should can complete this lab by filling out the Quiz Form. If you do not have a WebCT account, you can turn in a paper version of the lab. However, you should also notify your TA that you do not have a WebCT account so we can get one for you!


For this lab, you will modify a multi-file program. You will use a pre-defined class called POINT whose declaration and definition are stored in separate files. You will also modify a pre-defined class called LINE and write a main() program which uses these classes.

You will be given classes for POINT and LINE that are extensions of the structs you developed for those concepts a few weeks ago. Today, you will take the given definitions of those classes and extend them to define your own ">>" and "<<" operators.

Download the following files for the lab. Create a new folder (name it something appropriate) and move these files into it. Start up Visual C++ and create a new project. Add the files POINT.cpp, LINE.cpp and testDriver.cpp to the project, but not the others. Make sure the .h fiels have been moved (or copied) into the same directory as your project. Examine the POINT.h and LINE.h files. On your lab sheet, record the prototypes of all methods. Do you recognize any differences in these prototypes from those used previously? Record your answer on your lab sheet. Run the program and record what you see from the output to the lab worksheet. The new POINT class allows direct output of a point to cout by overloading the "<<" operator. Examine the prototype for this operator and record it on your lab sheet. Examine the definition of this operator in the POINT.cpp file and record it on your lab sheet. What facilitator method is used by the "<<" operator? Record its name on your lab sheet. Examine the definition of this facilitator method and record it on your lab sheet. Answer the following questions on your lab sheet: Apparently, a very similar definition of the "<<" operator and corresponding facilitator can be made for the LINE class, to allow direct output to cout. How can you do this? Record your answer on your lab sheet. Make the changes, and test and run the program. (You will need to remove the "//" comment delimiters from the following line from the driver
//   cout << "\nLine1: " << line1 << ";\nLine2: " << line2 << endl;
to test this.)

Continue until your code works properly. Following the logic used in overloading the "<<" operator, see if you can write the " >>" operator and corresponding facilitator "Extract()" for the POINT class. (Hint: do you think you will use ostream for input, or perhaps some other type?)
Note: the format for inputing a point is a '(', followed by an int, followed by a ',', followed by another int, and followed by a ')' . You may get some clues from the Rational class defined in the textbook (for which the format is an int, followed by a '/', followed by another int). You may view the implementation of this class in the files Test your code by removing the comments at the beginning of the main() function that surround the code
cout << "Please input the first point in the format (x,y): "; 
cin >> p1; 
Compile it again and run the program. Finish the LINE class for the ">>" operator and corresponding facilitator "Extract()". (Hint: you may find it useful to use the ">>" operator given by the POINT class in your definition of the ">>" operator for the LINE class.)

The ability to overload standard operators such as ">>" and "<<" allows user-defined classes in C++ to behave like first-class objects that are built-in to the language. The process of creating a public facilitator method which is a member of the class, then defining the overloaded operator by calling the facilitator, permits the overloaded operator to access private data objects while allowing their actual implementation details to remain hidden.