// CSE143 Sp01 Homework 2 Sample Solution // placelist.cpp - implementaiton of class PlaceList // HP 4/01 #include using namespace std; #include "placelist.h" #include "location.h" // Construct an empty PlaceList PlaceList::PlaceList() { size = 0; } // Add place with given name and coordinates to this PlaceList // Assumption: this PlaceList is not full void PlaceList::add(double x, double y, string name) { Location where(x,y); list[size].loc = where; list[size].name = name; size++; } // = "this PlaceList contains a place with the specified name bool PlaceList::contains(string name) { return indexOf(name) < size; } // = Location of the place with the specified name; if the name is not // found, the result is a Location whose coordinates are not defined. Location PlaceList::locationOf(string name) { int index = indexOf(name); if (index < size) { return list[index].loc; } else { Location nowhere; return nowhere; } } // = index of first Place containing specified name, or current size if // the name is not found int PlaceList::indexOf(string name) { int k = 0; while (k < size && list[k].name != name) k++; return k; }