Create a program in C++ that does : Implement a class person with the following
ID: 3761505 • Letter: C
Question
Create a program in C++ that does :
Implement a class person with the following fields:
The nam
A pointer to the persons best friend
A popularity counter that indicates how many people have this person as their best friend
Write a program that reads in a list of names, allocates a new Peron for every one of them, and places them in a vector<Person*>. Next, request the name of each best friend for every Person objects. Find the object matching the friend’s name, and call a set_best_friend member function to update the pointer and counter. Lastly, print out every Person objects. Labeling the name, best friend, and popularity counter for all.
Explanation / Answer
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
using namespace std;
class Person
{
public:
Person(string name = "N/A", short popularity = 0, Person* bff = NULL);
string get_name();
void set_name(string name);
short get_popularity();
void set_popularity(short pop);
string get_bff();
void set_bff(Person* bff);
private:
string name;
short popularity;
Person* bff;
};
int main()
{
cout << "Enter length of name ";
short a;
cin >> a;
vector<Person*> list;
for(short k = 0; k < a; k++)
{
cout << "Enter name of individual ";
string name;
cin >> name;
list.push_back(new Person(name));
}
for(short k = 0; k < a; k++)
{
cout << "Enter name of " << list[k]->get_name() << "s friend: ";
string name;
cin >> name;
for(short j = 0; j < a; j++)
{
if(list[j]->get_name() == name)
{
list[j]->set_popularity((list[j]->get_popularity())+1);
list[k]->set_bff(list[j]);
}
}
}
for(short k = 0; k < a; k++)
{
cout << "name: " << setw(8) << list[k]->get_name() << " popularity: " << list[k]->get_popularity() << " bff: " << setw(8) << list[k]->get_bff() << endl;
}
for(short k = 0; k < a; k++)
{
delete list[k];
}
cout << "Please Enter a char ";
string exit;
getline(cin, exit);
getline(cin, exit);
return 0;
}
Person::Person(string name, short popularity, Person* bff):
name(name), popularity(popularity), bff(bff)
{
}
string Person::get_name()
{
return name;
}
void Person::set_name(string name)
{
Person::name = name;
}
short Person::get_popularity()
{
return popularity;
}
void Person::set_popularity(short pop)
{
popularity = pop;
}
string Person::get_bff()
{
return bff->get_name();
}
void Person::set_bff(Person* bff)
{
Person::bff = bff;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.