# Week 3 Wednesday Tutorial Exercises

1. Modify last tutorial's List class so that it can be automatically output using the << operator, as shown in today's lecture. Test your code to make sure it works.

2. Modify your List class so that it can be automatically converted to a bool (true corresponds to a non-empty list), as shown in today's lecture. Test your code to make sure it works.

3. Modify your List class so that it can be automatically input using the >> operator. The method should input integers one at a time, separated by whitespace, until end-of-file or some other error occurs. Your operator function should not need to be a friend function of the List class. Test your code to make sure it works.

4. Modify your List class so that it overloads the >>= operator so that it can used to add elements to the back of the list, and the <<= operator so that it adds elements to the front of the list. That is, the following code
List l;
l >>= 34;
l >>= 48;
l >>= 23;
l <<= 12;
l <<= 3;

is equivalent to
List l;
l.insert_at_back(34);
l.insert_at_back(48);
l.insert_at_back(23);
l.insert_at_front(12);
l.insert_at_front(3);

Your operator method should return a reference to the modified List object, and should take a single parameter which is an int. Test your code to make sure it works.

As an aside, the << and >> operators usually correspond to left and right shifts (bitwise). For integers, a left shift corresponds to multiplying by 2 and a right shift corresponds to dividing by 2. So then

int i = 7;
i <<= 3;   // Corresponds to  i = i << 3;
cout << i << endl;

would output 56. Notice that here we have overloaded the <<= and >>= operators for List to mean what we want them to mean, in this case, adding an element to the front or back of the list. Similarly, the ostream class overloads the << operator for output, and the istream class overloads the >> operator for input.

5. Modify your List class so that it can be automatically converted into a single double value. This double value should be the average of all of the values in the list. Test your code to make sure it works.

6. Write a Rational class which stores a rational number number m/n (eg. 3/4) as a separate numerator and denominator, ie. two long integers m and n. Provide appropriate constructors, eg:
Rational();
Rational(const Rational &o);
Rational(const long &o);
Rational(const double &o);

Overload the appropriate operators to make the class easy to use, namely: =, +, - (subtraction), - (negation), *, /, ==, !=, <, >, <=, >=, !, +=, -=, *=, /=, bool, long, double, <<, >>