MCS273 Introduction to C++ Programming (January 2013)

Operator overloading


Overview

Your task is to add some operator overloading to the List class, testing as you go.

Tasks

  • To get started, you should first download the file List.zip to convenient location and import it into Eclipse. After making and running the program, read through the files to the point that you understand what is going on. It is not exactly the implementation we wrote in class yesterday, but it is very similar. For your convenience, I am also providing you the file OverloadedPoint3D.zip, which is the example I did on class on Tuesday. If you import this file into Eclipse, you can use it to guide your tasks below.

  • First add the following member function:

        ListItem& operator[](int n);
    
    Note, in particular, that this operator differs from the member function getNth in that it returns a reference to the value, not just the value itself. It should also differ by returning the (n+1)th value; just like arrays, you should begin the indexing at 0. Add the appropriate code to testList.cc in order to test that it works properly.

  • Add the following friend function:

        ostream& operator<<(ostream&, const List&);
    
    Note that you will need to make it a friend of both Node and List. Add the appropriate code to testList.cc in order to test that it works properly. In particular, I want it to do the exact same thing as List::display() does, except that it should not add a newline. Thus, if list is a List variable, then the following call should be equivalent to list.display():
        cout << list << endl;
    

  • Check-off (2 points): When you have written and tested these procedures, ask me to check you off.