Question: In C++ #ifndef PERSON_H #define PERSON_H #include using namespace std; class Person { string name; Person * best_friend; int popularity; public: Person(): name(), best_friend(0), popularity(0)

In C++ #ifndef PERSON_H #define PERSON_H #include  using namespace std; class Person { string name; Person * best_friend; int popularity; public: Person(): name(""), best_friend(0), popularity(0) { } Person(string n): name(n), best_friend(0), popularity(0) { } string get_name(); string get_best_friend(); int get_popularity(); void set_best_friend(Person *); }; #endif 

 #include "Person.h" string Person::get_name() { return name; } string Person::get_best_friend() { if (best_friend != 0) // check for null pointer return best_friend->name; else return ""; } int Person::get_popularity() { return popularity; } void Person::set_best_friend(Person * bf) { best_friend = bf; (bf->popularity)++; } 
 #include  #include  // for setw #include  // for ifstream #include  #include  #include "Person.h" using namespace std; main() { string name; vector people; // vector of pointers to objects of type Person Person * person_pointer; ifstream in; in.open("names"); if (!in.is_open()) { cout << "Couldn't open 'names' file. "; return 1; } in >> name; while (!in.eof()) { person_pointer = new Person(name); people.push_back(person_pointer); // no check for duplicate names in >> name; } in.close(); int i, j; // for each person prompt for a best friend name for (i = 0; i < people.size(); i++) { cout << "Who is " << people[i]->get_name() << "'s best friend? "; cin >> name; // search for best friend for (j = 0; j < people.size(); j++) if (people[j]->get_name() == name) break; if (j < people.size()) // found a best friend people[i]->set_best_friend(people[j]); else cout << "Couldn't find best friend " << name << endl; } // list of name, best friend, and popularity count for (i = 0; i < people.size(); i++) { person_pointer = people[i]; cout << left << setw(10) << person_pointer->get_name(); cout << left << setw(10) << person_pointer->get_best_friend(); cout << right << setw(2) << person_pointer->get_popularity() << endl; } // clean up for (i = 0; i < people.size(); i++) delete people[i]; }

4. Modify the program to prevent duplicate names being inserted in the vector and modify the program to prompt the user for another name if best friend is not found.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!