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
#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:
#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();
}