// File: ex4-9.cpp

#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;


class Person
{
    char* name;
public:
    Person(const char *);    // constructor
    ~Person();
    void print() const;      // display Person's name
};

Person::Person(const char* n) : name(new char[strlen(n)+1])
{
    strcpy(name,n);
}

Person::~Person()
{
    cout << "Person destructor call for " << name << endl;
    delete [] name;
    name = nullptr;
}

void Person::print() const
{
    cout << name << endl;
}


class People
{
private:
    Person** array;
    int count;
    Person* addPerson();         // private member function
public:
    People(int);
    ~People();
    void print() const;
};

People::People(int num) : array(new Person*[num]), count(0)
{
    for (int i = 0; i < num; i++)
        array[count++] = addPerson();
}

People::~People()
{
    cout << "\nPeople destructor called" << endl;
    for (int i = 0; i < count; i++)
    {
        cout << "deleting pointer to Person[" << i << "]\n";
        delete array[i];
    }
    delete [] array;
}

Person* People::addPerson()
{
    char temp[20];
    cout<<"Enter the Persons name => ";
    cin >> temp;
    return new Person(temp);
}

void People::print() const
{
    cout << "\nHere's the People:\n";
    for (int i = 0; i < count; i++) array[i]->print();
}

int main ()
{
    cout << "How many friends do you have? ";
    int numFriends;
    cin >> numFriends;
    People friends(numFriends);
    friends.print();
}