user5478331
user5478331

Reputation:

Dynamic array of objects vs Dynamic array of pointers

I'm currently working on two C++ projects that are similar. Both use the class personType, and they ask the user for the number of elements to save in the array, and creates an array using the inputted size. Then, the program will prompt the user for personal info for each element(name, address, birthdate, etc.). Finally, after getting all the data for all elements, the program will output the data of each person. The difference between these 2 programs is that the first stores the data into a dynamic array of personType class, while the second asks for a dynamic array of personType class, full of pointers. The dynamic array of pointers in my program only takes one element, and then exits, even if I enter "2" for the number of elements.

I've tried looking at other S.O. answers(c++ dynamic array of pointers and Pointer to dynamic array of pointers to objects), but when I try to add 2 people, after inputting info for 1 person, the program crashes/exits.

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;
class personType
{
public:
    void setVar(string fn, string ln, string adr, double hgt, string dob, char gndr);
    void getVar(string &fn, string &ln, string &adr, double &hgt, string &dob, char &gndr);
    void printVar(string fn, string ln, string adr, double hgt, string dob, char gndr, int num);
private:
    string fName;
    string lName;
    string address;
    double height;
    string DOB;
    char gender;
};

int main(){
    int num = 0;
    string first;
    string last;
    string addr;
    double hght;
    string birthDate;
    char gnder;
    int index = 1;

    cout << "How Many People to Create? ";
    cin >> num;
    cin.ignore();

    personType** personObject = new personType*[num];
    for(int i = 0; i < num; i++)
    {
        personObject[num] = new personType[];
    }

    for(int i = 0; i < num; i++)
    {
        personObject[i]->getVar(first, last, addr, hght, birthDate,  gnder);
        personObject[i]->setVar(first, last, addr, hght, birthDate, gnder);
        cout << endl;
        cin.clear();
        cin.ignore();
    }
    for(int i = 0; i < num; i++)
    {
        personObject[i]->printVar(first, last, addr, hght,        birthDate, gnder, index);
        index++;
    }

    for( int i = 0; i < num; i++)
    {
        delete personObject[num];
    }
    delete [] personObject;
    return 0;
} 

void personType::setVar(string fn, string ln, string adr, double hgt, string dob, char gndr)
{
    fName = fn;
    lName = ln;
    address = adr;
    height = hgt;
    DOB = dob;
    gender = gndr;
}

void personType::getVar(string &fn, string &ln, string &adr, double &hgt, string &dob, char &gndr)
{
    cout << "Enter a First Name:";
    getline(cin, fn); 
    cin.clear();
    cout << "Enter a Last Name:";
    getline(cin, ln);
    cin.clear();
    cout << "Enter an Address:";
    getline(cin, adr);
    cin.clear();
    cout << "Enter a Height(inches):";
    cin >> hgt;
    cin.clear();
    cin.ignore();
    cout << "Enter a Date of Birth(mm/dd/yyyy):";
    getline(cin, dob);
    cin.clear();
    cout << "Enter a Gender(m/f):";
    cin.get(gndr);
    cin.clear();
}

void personType::printVar(string fn, string ln, string adr, double hgt, string dob, char gndr, int num)
{
    cout << "PERSON " << num << ":" << endl;
    cout << "Name: " << fn << " " << ln << endl << "Address: " << adr << endl;
    cout << "Height(in inche): " << hgt << endl;
    cout << "Date of Birth(mm/dd/yyyy): " << dob << endl;
    cout << "Gender(m/f): " << gndr << endl << endl;
}

What is the syntax to create dynamic arrays of class objects, and dynamic arrays of pointers to class objects?

Upvotes: 0

Views: 899

Answers (1)

Batuhan Tosyalı
Batuhan Tosyalı

Reputation: 160

Let's say you did

ClassObject* obj = new ClassObject[some number]

Here; we created an array in the heap space, and used Classobject pointer named obj to point to it so we can know where does the array start.

obj[some number]

with that you can reach the index you want.

In the second part, I understanded as you want to creat a dynamic array of pointers, that would be done with pointer pointer

ClassObject** pointer = new ClassObject*[some number]

please fix me if I mistake your question

Upvotes: 0

Related Questions