problem session working in pairs of two, solve the following problem
TRANSCRIPT
Problem SessionProblem Session
Working in pairs of two,Working in pairs of two,
solve the following problem...solve the following problem...
ProblemProblem
Design an Address class to represent street Design an Address class to represent street addresses e.g.,addresses e.g.,
1313 Mockingbird Lane1313 Mockingbird LaneUniversal City, CA 12345Universal City, CA 12345
IdentifyIdentify– the function members needed to operate on such the function members needed to operate on such
objects;objects;
– the data members needed to represent addresses.the data members needed to represent addresses.
Implement as much of your design as time Implement as much of your design as time permits.permits.
Coding: InterfaceCoding: Interface// Address.h declares a ‘bare bones’ address class// #include directives have been omitted ...
class Address{ public: Address(); Address(int houseNumber, const string & street, const string & city, const string & state, int zipCode); int HouseNumber() const; string Street() const; string City() const; string State() const; int ZipCode() const; friend istream & operator>>(istream & in, Address & addr); friend ostream & operator<<(ostream & out, const Address & addr); // ...
Coding: Private SectionCoding: Private Section// ... still in Date.h
private: int myHouseNumber; string myStreet, myCity, myState; int myZipCode;};
inline Address::Address(){ myHouseNumber = myZipCode = 0; myStreet = myCity = mystate = “”;}
Coding (Coding (Ct’dCt’d))// ... still in Date.h
inline int Address::HouseNumber() const{ return myHouseNumber;}
inline string Address::Street() const{ return myStreet;}
inline string Address::City() const{ return myCity;}
Coding (Coding (Ct’dCt’d))// ... still in Date.h
inline string Address::State() const{ return myState;}
inline int Address::ZipCode() const{ return myZipCode;}
Coding (Coding (Ct’dCt’d))// Date.cpp defines non-trivial Date function members.
// ...
#include “Date.h” // class Date
inline Address::Address(int houseNumber, const string & street, const string & city, const string & state, int zipCode){ assert(houseNumber >= 0 && zipCode > 0); myHouseNumber = houseNumber; myStreet = street; myCity = city; myState = state; myZipCode = zipCode;}
Coding (Coding (Ct’dCt’d))// ... Date.cpp continued
// display using three-line formatostream & operator<<(ostream & out, const Address & addr){ out << addr.myHouseNumber << ‘ ‘ // line 1 << addr.myStreet << ‘\n’ << addr.myCity << “, “ // line 2 << addr.myState << ‘\n’ << addr.myZipCode; // line 3
return out; }
Coding (Coding (Ct’dCt’d))// ... Date.cpp continued// assume three-line formatistream & operator>>(istream & in, Address & addr){ int number, zipCode; string street = “”, city = “”, state = “”; in >> number; // beginning of line 1 assert(in.good()); getline(in, street); // rest of line 1 is street char separator; string word; do { // line 2: in >> word; // city may consist of city += word; // multiple words in.get(separator); // terminated by } while (separator != ‘,’); // a comma
// ...
Coding (Coding (Ct’dCt’d))// ... Date.cpp continued
// ... do { // line 2 (ctd) in >> word; // state may consist of state += word; // multiple words in.get(separator); // terminated by } while (separator != ‘\n’); // a newline
in >> zipCode; // line 3: zipcode
if (in.good()) // change addr if all is well
addr = Address(number, street, city, state zipCode);
return in;}