Click here to Skip to main content
15,850,360 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Design a class "Person" that has in the private section three fields. The &st "name" is
a string representing the first name of the person* The second "best" is a pointer to
type Person representing the person who is the best friend of this person. The third
"popularity" is an integer indicating how popular this person is. In other words, how many other person have him as a best friend .

The public interface should have the functions : a default constructor , with 1 parameter , the name of the person defaulting to "" , and the constructor will set best to NULL and popularity to 0 . The function "increment " which increments the value of the popularity by 1 . the function "getName", the function "setBest" which takes as a parameter a pointer to type Person .Function "print" which prints the name of the person followed by the name of his best friend followed by the value of his popularity .



Write a main program that declares an array "list" of size 10. Each element of the array is a pointer to type Person.Open the file "person.txt"that contains names of persons each on a separate line. Read the names of the persons and for each name create a new object and set a pointer of the array "list" to point to this person object.
There is a second file "friends.txt" where every line contains the name of a person
followed by the name of his best &end. Read each line and adjust the objects in "list"
accordingly. In other words you have to set the best friend for an object and you have to increment the popularity of the corresponding person.

If "persons.txt" contains:
John
Steve
Mark
Mike
Ziad
Tarek

And if "friends.txtontains:
John Steve
Steve Mark
Mark Steve
Mike John
Ziad John
Tarek Ziad

Then the program should output to the file "result.txt" the following information:

John Best friend: Steve Popularity = 2
Steve Best friend: Mark Popularity = 2
Mark Best friend: Steve Popularity = 1
Mike Best friend: John Popularity = 0
Ziad Best friend: John Popularity = 1
Tarek Best friend: Ziad Popularity = 0


What I have tried:

#include <iostream>
#include <string>
using namespace std;

class person {
private:
	string name;
	person* best;
	int popularity;

public:

	person(string name)
	{
		name = "";
		best = NULL;
		popularity = 0;
	}

	void increment()
	{
		popularity++;
	}
	void getName(string a)
	{
		a = name;
	}
	void setBest(person *person)// not very sure about this
	{
		person = best;
	}

	void print()
	{
		cout << "name:" << name << endl;
		cout << "bestfriend" << best << endl;
		cout << "popularity" << popularity << endl;
	}
};

int main()
{
	  int list[10] person; //??
	
}
Posted
Updated 12-May-18 4:22am
v2
Comments
Dave Kreskowiak 11-May-18 14:00pm    
So was there a question you forgot to ask?
Member 13822923 11-May-18 14:05pm    
i actually put it in the comments that I'm not sure about the setBest function, and I'm stuck in the main i didn't know how to make an array that points to persons.
Rick York 11-May-18 17:30pm    
Remarks in your comments do not constitute asking a question. That's what the upper text box is for - YOUR question.

Quote:
understand what you meant about the constructor but how does the array that you wrote point to persons? like im having huge difficulty with pointers
You have to explicitely manage (allocate/deallocate) memory. Try
C++
#include <fstream>
#include <sstream>
#include <iostream>
#include <string>

using namespace std;

class Person {
private:
  string name;
  const Person* best;
  int popularity;

public:
  Person(const string & name = "") : name(name), best(nullptr), popularity(0)
  {
  }
  void increment()
  {
    ++popularity;
  }
  string getName() const
  {
    return name;
  }
  void setBest(const Person * person)
  {
    best = person;
  }
  void print()
  {
    cout << "name " << name << ", bestfriend " << best->getName() << ", popularity " << popularity << endl;
  }
};

int load(const string & filepath, Person * p[])
{
  ifstream ifs(filepath);
  string name;
  int n = 0;
  while ( getline(ifs, name) )
  {
    cout << "n " << n << endl;
    p[n] = new Person(name);
    ++n;
  }
  return n;
}
void loadFriends( const string & filepath, Person * p[], int size)
{
  string line;
  ifstream ifs(filepath);
  while ( getline(ifs, line))
  {
    Person *p_person = nullptr, *p_friend = nullptr;
    string person_name, friend_name;
    istringstream iss(line);
    iss >> person_name >> friend_name;
    for (int n=0; n<size; ++n)
    {
      string name = p[n]->getName();
      if ( name == person_name)
        p_person = p[n];

      if ( name == friend_name)
        p_friend = p[n];
    }
    if ( p_person )
    {
      p_person->setBest(p_friend);
      if (p_friend)
        p_friend->increment();
    }
  }
}

int main()
{
  Person * p[10];
  int persons = load("persons.txt", p);
  cout << "persons " << persons << endl;
  loadFriends("friends.txt", p, persons);
  for (int n=0; n<persons; ++n)
  {
    p[n]->print();
  }

  for (int n=0; n<persons; ++n)
    delete p[n];
}




Please note, with modern C++ you could ('should',
as other people would say... :-) ) avoid explicit memory management:
C++
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <memory>
using namespace std;

class Person {
private:
  string name;
  shared_ptr<Person> best;
  int popularity;

public:
  Person(const string & name = "") : name(name), best(nullptr), popularity(0)
  {
  }
  void increment()
  {
    ++popularity;
  }
  string getName() const
  {
    return name;
  }
  void setBest(const shared_ptr<Person> & person)
  {
    best = person;
  }
  void print() const
  {
    cout << "name " << name << ", bestfriend " << best->getName() << ", popularity " << popularity << endl;
  }
};

vector < shared_ptr < Person > > load(const string & filepath)
{ 
  ifstream ifs(filepath);
  string name;
  vector < shared_ptr < Person > > vp;
  while ( getline(ifs, name) ) 
    vp.push_back(make_shared < Person > ( name ) );
  
  return vp;
}
void loadFriends( const string & filepath, const vector <  shared_ptr < Person > > & vp)
{
  string line;
  ifstream ifs(filepath);

  while ( getline(ifs, line))
  {
    shared_ptr< Person> p_person, p_friend;
    string person_name, friend_name;
    istringstream iss(line);
    iss >> person_name >> friend_name;
    for (auto & p  : vp)
    {
      string name = p->getName();
      if ( name == person_name)
        p_person = p;
      if ( name == friend_name)
        p_friend = p;
    }
    if ( p_person )
    {
      p_person->setBest(p_friend);
      if (p_friend)
        p_friend->increment();
    }
  }
}

int main()
{
  auto vp = load("persons.txt");
  cout << "persons " << vp.size() << endl;
  loadFriends("friends.txt", vp);

  for ( const auto & p : vp)
    p->print();
}
 
Share this answer
 
Try this:
C++
int main()
   {
   person myPeople[10];
   ...
   }
But read the question again: in order to construct an array or Person objects, the Person class needs the default constructor you are specifically asked to include:
Quote:
The public interface should have the functions : a default constructor , with 1 parameter , the name of the person defaulting to ""
You need to rewrite your constructor to this:
person(string parName = "")
{
    name = parName;
    best = NULL;
    popularity = 0;
}
 
Share this answer
 
Comments
Member 13822923 11-May-18 14:12pm    
I understand what you meant about the constructor but how does the array that you wrote point to persons? like im having huge difficulty with pointers

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900