Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ classes, constructors, function overloading You have to write a class DynamicArray to store a dynamic array of integers and allow manipulations on that array.

C++ classes, constructors, function overloading

image text in transcribed

image text in transcribed

You have to write a class "DynamicArray to store a dynamic array of integers and allow manipulations on that array. It would have the following private attributes: int *arr: A pointer that points to the array int arraySize: An integer that stores the size of the array. It should be greater or equal to one int numOfElements: An integer that tracks how many elements are in the array The constructors will be defined as follows . Default constructor: the default constructor should set the arraySize to one and initialize the dynamic array of integers DynamicArray) Constructor: In constructor a valid arraySize (at least 1) should be passed. If any number less than 1 is passed to the constructor, you should set arraySize to 1 by default. DynamicArray(int arraySize) For both constructors above, the initial number of elements is set to O. Copy Constructor: In the copy constructor, you need to implement Deep Copy, so that all elements of the original DynamicArray object are copied in the second object. DynamicArray const DynamicArray a) Overloaded assignment operator ():the operatorwl allow to copy an object into another. Remember that the implementation requires a deep copy of the original object. DynamicArray& operator-(const DynamicArray& rhs) The class would also include following public accessor functions and methods: int getArraySize() const; int getNumOfElements) consti toString() method this function will return a string which stores all elements currently stored in the dynamic array separated by a comma (and no space before or after the numbers). If there are no elements in the array, it prints out 'No element(without double quotes). Example: if there are 1 2 3 in the Dynamic array then it outputs 1,2,3 (without double quotes) string toString) const; Important: This method is used by more than one test case to compare output of other methods etc, so unable to implement this method correctly would result in failing more than one test cases. You are also required to overload toString so that it will be able to receive an integer parameter and use it to retrieve the element at that position. The function will return the element stored in the given position as a string. If the position is less than zero or greater than/equal to the number of elements, it should print "Invalid index. For example, An array has three elements 10,100,1000. And the position is given 1, it should print the value stored at that index. Here it should print 100. . string toString (int pos) consti Add element this option asks the user to input a number to store into the array, then inserts the number in the array. The number should be added at the end of the array. If the array does not have room to add another element, the array should be expanded to 2 times its current size before adding the element void addElement (int num) Hint anytime the array size is changed, you should copy the old array elements in a temporary array and de-allocate the old one to prevent memory leaks. Then, resize the original array and fill with elements from temporary copy. Finally, delete temporary copy

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Donald A. Carpenter Fred R. McFadden

1st Edition

8178088045, 978-8178088044

More Books

Students also viewed these Databases questions