I have an issue with creating and adding objects into an array This is my base class:
class CPerson
{
std::string first_name;
std::string last_name;
int birth_year;
char sex;
public:
CPerson();
CPerson(std::string, std::string,int,char);
with some extra setters and getters. Then I have this derived class:
class CData :
public CPerson
{
int nrOfPersons;
CPerson *Data_array;
public:
CData(int);
~CData();
};
In the constructor I want to fill the Data_array with person objects , and I do the following:
CData::CData(int x) : nrOfPersons(x)
{
for (int i = 0; i < nrOfPersons; i++)
Data_array[i] = CPerson();
}
But it crashes halfway there. I made the default CPerson() constructor to cout a message "Inserted" everytime I use it. So if I call in main CData database(4) it should print "Inserted" 4 times on the screen. Instead it only prints 2 times and then it crashes.
Data_array?