I have to solve this problem Urgently ! i need help please

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:

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
And what's the problem you're having with your code?
how i am supposed to declare best in the class ?

class person {

private :

string firstname ;
int popularity

this is what i have wrote so far

#include <iostream>
#include <fstream>
#include <cstring>

using namespace std ;

class person {

private :

string firstname
person * = best ;
int popularity

public :

person () ;
int increment (popularity );
string getName ();
void print (ostream & s) ;

} ;

person :: person (firstname)
firstname =" " ;
best =NULL ;
popularity =0 ;


int person :: increment (popularity ){
popularity = popularity +1 ;

void person ::print (ostream &s )
s << "name :"<< firstname ;
s << "bestfriend "<< best ;
s << "popularity "<< popularity ;

string person :: getName
Firstly, when you post code here, it would be much more helpful if you could enclose it in code tags. You can use the "<>" button to the right of the edit window. It will make your code much more readable, and will also provide line numbers to make it easier to refer to parts of your code.

One error I can immediately see is that you have a stray '=' character in the declaration of best.

Edit: A second is that you're passing a string called firstname into the constructor for person, but not using it.

A third is that you're passing popularity into the increment method by value. This means that there will be a local copy of that variable inside the method that gets incremented, but the original variable in the calling code will not be changed by the method.

Also, you can write
popularity = popularity +1 ;
more succinctly as:
which means exactly the same thing.

Last edited on
Thank you for the help ! I highly appreciate it .
Topic archived. No new replies allowed.