Constructor for Array Based List

Hi all,

I am writing a program where the user inputs the filename to a .txt along with an optional integer.

Ie, in the command line:
 
  ./program contacts.txt 9

or
 
  ./program contacts.txt


If the user inputs an integer, an array based list of int size = atoi(argv[2]); will be made from the .txt.

When writing the class for an array based list, I am confused as to how I set the array to the user-specified size. I understand I must create a constructor. However, I am very confused on how to do so.

My class is a mess. fullContact is a struct.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
template<int arraySize>
class abList{
private:
    int count;
    //constructor
    fullContact infoArray[arraySize];
public:
    void arrayConstructor(int size){
        arraySize = size;
    };
    void sort();
    void insertContact(int pos, string item);
    string removeContact(int pos);
    string retrieve(int pos);
    int search(string query);
};



int main(int argc, char* argv[])
{
    bool isAB = true;
    if (argv[2] == NULL)
    {
        isAB = false;
    }
    if (isAB)
    {
        //argv[2] is now size
        int size = atoi(argv[2]);
        cout << "Creating an array based list of size: " << size << endl;
        
    }
        
    ifstream contactFile(argv[1]);
    //if the file is found, open it and import the data into a list
    if (contactFile.is_open())
    {
        string currentLine;
        while ( getline (contactFile,currentLine) )
        {
            stringstream lineStream(currentLine);
            fullContact r;
            getline(lineStream, r.lastName , ',');
            getline(lineStream, r.firstName , ',');
            getline(lineStream, r.phoneNumber , ',');
            getline(lineStream, r.emailAddress , ',');
            cout << r.lastName << endl;
            //add to array based list
            if (isAB)
            {
                cout << "Adding contact to ablist!" << endl;
                //will insert contact here
            }
1. Contructor should not be private in your case. Private constructors are something very specific I do not wish to talk about now. In this example make it public.

2. Constructor MUST have the same name your class or struct does. It won't work any other way. Also it CANNOT be of any type, even void.

3. Constructor should create a new array with specified size:
1
2
3
4
5
6
7
8
9
10
11
class abList
{
    private:
        fullContract * infoArray; // we will use new keyword on this pointer

    public:
        abList (int size); // contructor
        ~abList();         // destructor
    
        // ... other methods and fields;
};

1
2
3
4
abList::abList(int size) // Implementation of contructor
{
    infoArray = new fullContract[size];
}

1
2
3
4
5
int main(int argc, char* argv[])
{
    //...
    abList myAbList(5); // creates a new abList with infoArray of size 5
}


4. If contructor uses new, you should also provide destructor that deletes the infoArray when object is destroyed:
1
2
3
4
abList::~abList() // implementation of destructor
{
    delete infoArray;
}
Last edited on
> If contructor uses new, you should also provide destructor that deletes
Your constructor does not use new. It uses new[]
So instead of delete, you need to delete[]

Also, you need to manage the copy constructor and the assignment operator


@sevenjon: templates are resolved at compile time, so you'll need to use dynamic memory in this case.
For that purpose there is `std::vector'
post your entire program, I can help with the memory allocation and dynamic arrays
Topic archived. No new replies allowed.