I am doing homework for my Intro to C++ course, and I am a bit confused as to what I need to do in my function set_best_friend. I copied and pasted my class declaration below, along with the empty function I haven't been able to write yet.
I am supposed to call a set_best_friend method in the main() to update the best_friend pointer and the popularity counter.
class Person{
public:
Person();
Person(string n, int pop, Person *bf);
void set_best_friend(string n);
string get_name() const;
int get_popularity() const;
private:
string name;
int popularity;
Person* best_friend;
};
void Person :: set_best_friend(string n)
{
//update best_friend pointer and popularity
Locate the object matching the friend's name and call a set_best_fiend method to update the best_friend pointer and the popularity counter.
set_friend() does not take a name. The caller (main) has used the name to search the population and it thus has a pointer.
The best friend replaces whatever the person had before; an update of a pointer.
If Bob is the new best friend of John, then it is the popularity of Bob that does change, not John's. (And if that means ditching Sam, Sam should lose popularity.)
Your selection of constructors could be improved. Creation of unnamed persons seems unlikely, and we do know the popularity and best friend of a new Person.
Why doesn't it take a name? In the main function, I am asking the user for the name of someone's best friend, then I'm supposed to increase that best friend's popularity by 1 and update the pointer from best_friend = NULL to best_friend = user's entry.
So should I take the user's best-friend entry as an argument and then in my set_best_friend function use string name of the best friend to find that location in the vector of Persons, increase the popularity of the Person at that location, then change the pointer of the friend to point from null to the best friend?