// Source: "Software Design ...", John A Robinson, Newnes, 2004, page 150. #include #include "loc.h" #include "mountain.h" int main(int argc, char **argv) { location *ploc; if (argc != 2) { cerr << "Usage: tellzone locationsfile\n"; return -1; } ifstream infile(argv[1]); if (!infile) { cerr << "Can't open " << argv[1] << " for reading\n"; return -1; } int numrecords; infile >> numrecords; location current_location; ploc = ¤t_location; // ploc, a pointer to a location, now points at a location int i; for (i = 0; i < numrecords; i++) { ploc->load(infile); cout << "Location " << ploc->getname() << " is in the " << ploc->zone() << " zone\n"; } infile >> numrecords; mountain current_mountain; ploc = ¤t_mountain; // A pointer to a base class can point to // an object of a derived class. So ploc, a // pointer to a location now points at a // mountain for (i = 0; i < numrecords; i++) { ploc->load(infile); // Calling mountain's load method cout << "Mountain " << ploc->getname() << " is in the " << ploc->zone() << " zone\n"; // The previous two lines call location's // getname and zone methods, which mountain inherits } infile.close(); return 1; }