#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; } }; class employee: public person { private: int salary; string department; public: int get_salary() const { return salary; } string get_department() const { return department; } void set_salary(int s) { salary = s; } void set_department(string d) { department = d; } employee(const string & f, const string & l, const string & p, int s, const string & d): person(f, l, p), salary(s), department(d) { } void print(ostream & out) const { person::print(out); out << ", $" << salary << ", " << department; } }; class criminal: public person { private: string crime; public: string get_crime() const { return crime; } void set_crime(string c) { crime = c; } criminal(const string & f, const string & l, const string & p, const string & c): person(f, l, p), crime(c) { } void print(ostream & out) const { person::print(out); out << ": " << crime; } }; 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 employee("Evangelina", "Smith", "432-777-8787", 64250, "designer")); db.enter(new person("Eric", "Earwig", "1-714-130-7014")); db.enter(new criminal("Murgatroyd", "Madd", "4837652929", "murder")); db.enter(new person("Tammy", "Teapot", "7352641083")); db.enter(new person("Francine", "Frogge", "+44-20-8154-5486")); db.enter(new employee("Ravioli", "Johnson", "6252777341", 57500, "typist")); db.enter(new employee("Colin", "Nostril", "9115438756", 34500, "manager")); db.enter(new person("Geoffrey", "Georaffe", "(205)416-4881")); db.enter(new person("Lolita", "Lambchop", "313-265-8116")); db.enter(new criminal("Basher", "McRough", "232-554-8734", "assault")); 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")); db.enter(new employee("Lumpy", "Morris", "154753857", 52000, "secretary")); db.enter(new criminal("Knuckles", "Badman", "648274621", "burglary")); 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"; } }