Classes C++ and Arrays

Write your question here.
Hello, I am creating a class that has a private array on the heap with a constructor that takes the size of the array and initializes it on the heap. Later I have to make a deconstructor delete the space and print out free after.

In my code, I was able to heap a private array and make a deconstructor, but I don't know how to take the size of the array and initialize it on the heap.

My guess is this:
int* size = new int();




Also when you initialize size on the heap, don't you also have to delete it too? If so, where, in the code, do you do that?



Here is my code so far.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  Class Student
{
     private: 
          int size;
          int* array = new int[size];
     public:
          Student(); // Constructor
          ~Student(); // Deconstructor

}

Student::Student()

{
// How do you make a constructor that takes the size of the array and initializes it on the heap
}

Student::~Student()
{
     delete[] array;
     cout << "Free!" << endl;
}
do this in the constructor.
1
2
3
4
5
Student::Student(int arraySize)
{
  size = arraySize;
  array = new int[size];
}
Just one more question. Aren't you supposed to initialize arraySize? and is this all I have to do?
Hi jshm,

First you can not do this : -
int* array = new int[size];

means you cant initialize here this array pointer.

simply define : - int* array;


As we know constructor is to initialize your object, So you can initialize here the value of size and array pointer;
So, finally your constructor become:-

Student::Student() //default constructor
{
size = 10; //initialize size with default value
array = new int[size]; //allocate memory for 10 elements in array on heap
}

Now,If you want to initialize size from user,You have to write parameterized constructor,

Student::Student(int arraySize) //parameterized constructor
{
size = arraySize; //initialize size with default value
array = new int[size]; //allocate memory for 'size' elements in array on heap
}

int main()
{
Student objS;

//default constructor call n allocate memory for array having 10 elements

Student objS1(7);

//parameterized constructor call n allocate memory for array 7 //elements


.....
}

@ ak16: please use code tags when posting code.

ak16 wrote:
First you can not do this : -
int* array = new int[size];

This depends.

In the 1998 and 2003 standards of C++ (named C++98 and C++03) it is true that you can't do this.

However, in the 2011 standard of C++ (named C++11), you can:
http://www.stroustrup.com/C++11FAQ.html#member-init

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
#include <iostream>

struct Whine
{
    Whine() {std::clog << this << " Whine::Whine()\n";}
    ~Whine() {std::clog << this << " Whine::~Whine()\n";}
};

class WhineMuseum
{
private:

    Whine *pw = new Whine[5];

public:

    ~WhineMuseum()
    {
        delete[] pw;
    }
};

int main()
{
    WhineMuseum wm;
}
0x380f9c Whine::Whine()
0x380f9d Whine::Whine()
0x380f9e Whine::Whine()
0x380f9f Whine::Whine()
0x380fa0 Whine::Whine()
0x380fa0 Whine::~Whine()
0x380f9f Whine::~Whine()
0x380f9e Whine::~Whine()
0x380f9d Whine::~Whine()
0x380f9c Whine::~Whine()
cout<<"thanx";
Topic archived. No new replies allowed.