122 homework week 7-8


Note: parts a and b are not in any particular order. Some students will find it easier to do B first, others A first.

Part A

  1. Write a client program (timepcex10.cpp) that uses the Time class to solve programming problem 10, p. 421 (Note that you can start with an existing timepcex file and modify it, or start from scratch. Your choice.

  2. Required files: timepc2.h, timepc2.cpp, and timepcex10.cpp Make sure you turn in all three files, along with sample output.

I/O specification

(You are welcome to improve the output of the time table. This is intended as a minimal user interface):

Enter time and zone (c-d to exit): 12:00 pm PST
Time zone equivalents: 12:00 pm PST, 1:00 pm MST, 2:00 pm CST, 3:00 pm EST
Enter time and zone (c-d to exit): 4:00 AM EST
Time zone equivalents: 1:00 am PST, 2:00 am MST, 3:00 am CST, 4:00 am EST
Enter time and zone (c-d to exit): ^d
Exiting...

One Possible Algorithm

Convert the input time to GMT and then display the table of equivalencies. Operations you could add to the Time class:

  • void toGMT(char zone);
  • void displayUStimeTable();

Part B

  1. Modify the Time class (timepc2.h and timepc2.cpp) by overloading operator<< to display objects of type Time.

  2. Modify timepcex2.cpp to use operator<< rather than calling Time::display directly.

  3. Required files: timepc2.h, timepc2.cpp, and timepcex2.cpp.

How to overload operator<< and operator>> for I/O of class types

Example: Class X

If a class includes appropriate display() and read() functions, overloading << and >> becomes quite straightforward, andI/O for class types becomes just as easy as built-in types.

By defining this (inline) public member function in X.h:


void display(ostream& out = cout){

   //print data members representing an X

   out << name;

   . . .

}



and then declaring operator<< as a non-member function that accepts 

args of type X:



//declared in X.h but after class X {...};

ostream& operator<< (ostream& out, X& x);

Then define the operator function in X.cpp:
//operator<< just calls X::display

ostream& operator<< (ostream& out, X& x){

	x.display(out);

	return out;   //always return left operand

}

To provide input for X using >>, declare and define a member function for input, in X.h:


void read(istream& in){

   //read data members representing an X

   in >> name;

}



and then declare operator<< as a non-member function:



//declared in X.h but after class X {...};

istream& operator>> (istream& in, X& x);
Then define the operator function in X.cpp:
//operator>> just calls X::read

istream& operator>> (istream& in, X& x){

	x.read(in);

	return in;   //always return left operand

}

 

How To Complete This Project in Just Three Steps Using Copy and Paste

  1. Open X.h in a browser, and copy these lines:

    //declare non-member functions that accept args of type X
    ostream& operator<< (ostream& out, X& x);

    Paste them into your timepc2.h file. Replaces the references to type X with type Time.

  2. Open X.cpp in a browser and copy these lines:

    //operator<<() -------------------------------------------------------------------
    //overloads operator<< to accept args of type X
    //note that class X must provide a display function.
    ostream& operator<< (ostream& out, X& x){
        x.display(out);
        return out; //return left argument
    }

    Paste them into your timepc2.cpp file. Replaces the references to type X with type Time.

  3. In your timepcex2.cpp, replace all calls of the form "watch.display();" with calls of "cout << watch;" You can also try using calls of the form "operator<<(cout, watch);" for the sheer sport of it ;-)