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
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 One Possible AlgorithmConvert the input time to GMT and then display the table of equivalencies. Operations you could add to the Time class:
Part B
How to overload operator<< and operator>> for I/O of class typesExample: Class XIf 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
|