Pointers and Dynamic Memory Allocation, using Classes

So, I am in my first ever C++ lab now, and I am working on this lab. I am not understanding how to even begin this. I was told to read the instructions (below) in their entirety before beginning. Any help would be appreciated.


1. Design a class that has a pointer to the string array and the array size as instance data fields.

2. Your class must have a default constructor (no argument) which creates an array of a default size and another constructor that creates an array of a size equal to this constructor’s single parameter of type int. Both constructors must create arrays dynamically and populate these arrays with arbitrary generated strings. Ask the user for the desired size of the string array.

3. Create a member function with the following prototype:
string* add(int &size, string toAdd);

where the parameter:
- size is the size of the new array dynamically created inside the function - toAdd is the string to be added to the new array

The function should:
a. create dynamically a new string array with the size one element larger than the existing one;
b. copy all elements from the existing array into the new array;
c. add the toAdd string parameter to the new array;
d. assign the new array size to the size parameter;
e. release the memory used by the existing array;
f. assign the new array pointer to the existing array pointer and appropriately adjust the private array size field;
g. return the pointer to the new array.

Note:
The term “existing array” refers to the array that is a private instance data member of the class created.

4. Create another member function with the following prototype: string* del(int &size, string toDel);

where the parameter:
- size is the size of the new array dynamically created (if necessary) inside the function
- toDel is the string to be removed (if found) from the existing array

If the toDel string is found in the existing array, the function should:
a. create dynamically a new string array with the size one element smaller than the existing one;
b. copy all elements with the exception of the one equal to toDel from the existing array into the new array;
c. assign the new array size to the size parameter;
d. release the memory used by the existing array;
e. assign the new array pointer to the existing array pointer and appropriately adjust the private array size field;
f. return the pointer to the new array.
If the toDel string is not found in the existing array, the function should return the NULL pointer indicating that the toDel string was not found.

Notes:
a. If the existing array has more than one occurrence of the toDel string it is enough to delete the first occurrence only.
b. The term “existing array” refers to the array that is a private instance data member of the class created.

5. Overload the insertion operator << to allow the human readable output of all elements of the string array with a single cout << statement.

6. Test created two functions in your main() function. The initial size of the string array as well as toAdd and toDel parameter strings should be provided by the user.

7. To prove that your code works, output all created arrays using the overloaded operator << in an easy to read format.
8. Make sure that you release all allocated memory as soon as possible (inside the functions add and del as well as in the class destructor) and definitely before exiting main() so that your program does not create any memory leaks.

9. Implement error processing to recover from invalid user data, and invalid system calls.

10. Every method must have a header showing name of the method, describing its purpose, and any input/output along with its Pre- & Post-condition. This information must be included in the method’s implementation and not with its prototype.

11. Make sure your code is very well commented, and the comments makes sense.

12. Every .cpp and .h file you create, must have a header showing name of the file, date it was created, description and any other information seems necessary.

13. Every class must have its own .h and .cpp file, and in addition:
a. class definitions must be in a .h file;
b. method implementations must be in .cpp file

14. In order to show your program is fully working, run your program with different inputs, and create series of screenshots showing its results.

Design a class that has a pointer to the string array and the array size as instance data fields.


Do that. Show it to us.
Last edited on
Topic archived. No new replies allowed.