100 likes | 115 Views
Problem Session. Working in pairs of two, solve the following problem. Problem. Design an Address class to represent street addresses e.g., 1313 Mockingbird Lane Universal City, CA 12345 Identify the function members needed to operate on such objects;
E N D
Problem Session Working in pairs of two, solve the following problem...
Problem Design an Address class to represent street addresses e.g., 1313 Mockingbird Lane Universal City, CA 12345 Identify • the function members needed to operate on such objects; • the data members needed to represent addresses. Implement as much of your design as time permits.
Coding: 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 Section // ... still in Date.h private: int myHouseNumber; string myStreet, myCity, myState; int myZipCode; }; inline Address::Address() { myHouseNumber = myZipCode = 0; myStreet = myCity = mystate = “”; }
Coding (Ct’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 (Ct’d) // ... still in Date.h inline string Address::State() const { return myState; } inline int Address::ZipCode() const { return myZipCode; }
Coding (Ct’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 (Ct’d) // ... Date.cpp continued // display using three-line format ostream & 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 (Ct’d) // ... Date.cpp continued // assume three-line format istream & 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 (Ct’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; }