// Source: "Software Design ...", John A Robinson, Newnes, 2004, page 126. // // Prints out zones of locations in a location database. Format of the database is: // First line: Integer number of records // Remaining lines: One record per line: Loc name (20 chars max), latitude, // ns character modifier for latitude, // longitude, ew character modifier for longitude // fstream required because file input/output streams are used #include #include using namespace std; // An enumerated type is created to associate meaningful names with the index values of // the zones array immediately below: enum { nfrigid, ntemperate, torrid, stemperate, sfrigid, error }; // The zones array is simply static array of pointers to constant strings that // specify names of the Earth's zones. const char *zones[6] = { "North frigid", "North temperate", "Torrid", "South temperate", "South frigid", "None" // Flag to indicate error return }; // Now follows the definition of the location class. Most of the functions are defined // inside because they are short. Only member function zone is long enough to have its // definition outside the class definition class location { char name[20]; float latitude; char ns; float longitude; char ew; public: location() {}; // Null constructor - nothing to initialize ~location() {}; // Null destructor - nothing to tidy up on destruction void load(istream& in) // Load data items from an input istream object. // This will usually be a file (and is in the main // program below), but it could be cin { in >> name >> latitude >> ns >> longitude >> ew; } void save(ostream& out) const // Save data items to output stream object // (usually a file) // Not used in this example { out << name << ' ' << latitude << ' ' << ns << ' ' << longitude << ' ' << ew << '\n'; } const char *getname() const { return(name); } const char *zone() const;// Large member function - defined outside class defn. }; // The member function *zone() was declared inside the location class definition, but not // defined. Here is its definition: // The zone member function returns a pointer to a string stating which zone of the earth // this particular place is located const char *location::zone() const { if (!name[0]) // No location loaded in this object return zones[error]; // Error return if (latitude < 23.5) return zones[torrid]; // Using the values of enum to index the zones array if (latitude < 66.5) { if (ns == 'N') return zones[ntemperate]; return zones[stemperate]; } if (ns == 'N') return zones[nfrigid]; return zones[sfrigid]; } // // Here is the main program which uses a location object to read in locations from a // database one by one, and print out their zones. // int main(int argc, char *argv[]) { if (argc != 2) { cout << "Usage: tellzone locationsfile\n"; return -1; } ifstream infile(argv[1]); // Opening the file specified on the command line as an // input file stream object if (!infile) { cerr << "Can't open " << argv[1] << " for reading\n"; return -1; } int numrecords; infile >> numrecords; // The first line of the database contains a single number // specifying the number of lines or "records" in the file location current_location; for (int i = 0; i < numrecords; i++) { current_location.load(infile); // Load the next line of infile into the // current_location object cout << "Location " << current_location.getname() << " is in the " << current_location.zone() << " zone\n"; } infile.close(); // Close the file input stream when all lines are read return 1; }