1 / 10

Problem Session

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;

lauriep
Download Presentation

Problem Session

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Problem Session Working in pairs of two, solve the following problem...

  2. 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.

  3. 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); // ...

  4. 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 = “”; }

  5. 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; }

  6. Coding (Ct’d) // ... still in Date.h inline string Address::State() const { return myState; } inline int Address::ZipCode() const { return myZipCode; }

  7. 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; }

  8. 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; }

  9. 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 // ...

  10. 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; }

More Related