Write a program in C++ that creates a class vect, which contains a pointer to an integer (int *ptr) and an integer (size).The integer pointer (ptr) will point to a dynamic array of integers and size represents the total capacity of that dynamic array. The class vect should behave as an array with practically unlimited entries.
a) Write a default constructor that will initialize integer pointer (ptr) to NULL
and integer variable (size) to zero.
b) Write a parameterized constructor that will initialize integer variable (size)
to a value passed as parameter. Initialize the integer pointer (ptr) to a
dynamic array of size that equals to the parameter that is passed to
constructor.
#include <iostream>
class vect
{
int* ptr;
int size;
public:
vect(): ptr(nullptr), size(0){}
vect(int _size): size(_size)
{
ptr = new int[size]{};
}
~vect()
{
delete [] ptr;
}
};
int main()
{
vect v(2);
return 0;
}
Comments
Leave a comment