#include #include #include #include using namespace std; class person { private: string fname, lname, phone; public: person(const string & f, const string & l, const string & p): fname(f), lname(l), phone(p) { } string get_fname() const { return fname; } string get_lname() const { return lname; } string get_phone() const { return phone; } void set_fname(const string & s) { fname = s; } void set_lname(const string & s) { lname = s; } void set_phone(const string & s) { phone = s; } bool match(const string & wanted) const { return fname == wanted || lname == wanted; } void print(ostream & out) const { out << fname << " " << lname << ", phone " << phone; } }; template class database { private: vector data; public: void enter(T * p) { data.push_back(p); } T * lookup(const string & wanted) const { for (int i = 0; i < data.size(); i += 1) if (data[i]->match(wanted)) return data[i]; return NULL; } void do_to_all(void (* f)(T *)) { for (int i = 0; i < data.size(); i += 1) f(data[i]); } }; void display_person(person * p) { p->print(cout); cout << "\n"; } int main() { database db; db.enter(new person("Mary", "Marlowe", "8883217645")); db.enter(new person("Dennis", "Dog", "(306)291-6622")); db.enter(new person("Eric", "Earwig", "1-714-130-7014")); db.enter(new person("Tammy", "Teapot", "7352641083")); db.enter(new person("Francine", "Frogge", "+44-20-8154-5486")); db.enter(new person("Geoffrey", "Georaffe", "(205)416-4881")); db.enter(new person("Lolita", "Lambchop", "313-265-8116")); db.enter(new person("Indiana", "Impediment", "9017482361")); db.enter(new person("Germania", "Gastropod", "201-555-7469")); db.enter(new person("Olivia", "Ointment", "1-800-TRY-SMUT")); string line, cmd, name; while (true) { cout << "> "; getline(cin, line); if (cin.fail()) { cout << "\n"; break; } istringstream in(line); in >> cmd; if (in.fail()) continue; if (cmd == "list") db.do_to_all(display_person); else if (cmd == "find") { in >> name; if (in.fail()) { cout << "missing search term\n"; continue; } person * p = db.lookup(name); if (p == NULL) cout << "Not found\n"; else display_person(p); } else if (cmd == "exit") break; else cout << "command '" << cmd << "' not recognised\n"; in >> cmd; if (! in.fail()) cout << "extra things were on the line after the command\n"; } }