Answer to Question #273873 in C++ for SUNIL

Question #273873

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.


1
Expert's answer
2021-12-06T16:44:06-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS