Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ The purpose of this assignment is to replace the static array by dynamic arr

ID: 662593 • Letter: C

Question

C++

The purpose of this assignment is to replace the static array by dynamic array. You are required to modify the String class accordingly:

Data: we wish to allow our String class to accommodate strings of different sizes. To that end, instead of storing a string's characters in a fixed-size array, each String object should contain the following data as part of its internal representation:

(1) a character pointer meant to point to a dynamically-allocated array of characters.
(2) a length field that will hold the current length of the string at any given moment.

Operations: same as the methods in String class - phase 2, plus the "big three"( destructor, copy constructor, and assignment operator overloading).

Modularity: The String class code must be put in its own module, i.e., a file mystring.h (the header file) should hold the class definition (use conditional compilation directives) and a file mystring.cpp (the implementation file) should hold the method definitions.

Explanation / Answer

Dynamically allocating arrays

Declaring arrays dynamically allows us to choose their size while the program is running. To allocate an array dynamically, we use the array form of new and delete (often called new[] and delete[]):

1

2

3

4

int nSize = 12;

int *pnArray = new int[nSize]; // note: nSize does not need to be constant!

pnArray[4] = 7;

delete[] pnArray;

Because we are allocating an array, C++ knows that it should use the array version of new instead of the scalar version of new. Essentially, the new[] operator is called, even though the [] isn

1

2

3

4

int nSize = 12;

int *pnArray = new int[nSize]; // note: nSize does not need to be constant!

pnArray[4] = 7;

delete[] pnArray;

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote