Answered step by step
Verified Expert Solution
Question
1 Approved Answer
C++ Object-oriented Programming Classes, constructors, and Function overloading 1-Objective The purpose of this assignment is for students to gain a better understanding of object-oriented programming,
C++
Object-oriented Programming Classes, constructors, and Function overloading
1-Objective The purpose of this assignment is for students to gain a better understanding of object-oriented programming, classes, accessors, methoo, destructors, and dynamic allocation and Function overloading. 2- Problem 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 least1) 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 0. 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 operator will 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 Accessors: int getArraySize ) const; int getNumOfElements) const toString0 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 . 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 . tring toString (int pos) const: . 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. .Delete element: this option should ask the user for a number to remove from the array. This function should only delete the first occurrence of the to-be deleted number. If the specified number does not exist inside the array, there should be no change in the array and no output. Anytime an element is deleted from the beginning or middle of the array, all other elements in the array should be shifted over to fill the empty gap. If the number of elements becomes less than or equal to half the size of the array, the array should be shrunk down to half its current size. Refer to the hint above to copy the array into a smaller one without producing memory leaks. void deleteElement (int num) .Destructor: finally, a destructor should be included to release all memory that has been allocated for the object. DynamicArray ( Note: In case of any error in input you don't need to output anything. Either use default values (where it's asked to do so) or ignore that operation/modification for other cases (where there's no default value to set to). 4- Submission guideline You need to submit three files DynamicArray.cpp, where you will implement the class. DynamicArray.h where class declaration will be written and main.cpp.If you encounter an error while submitting, try refreshing your browser to see if your code has been saved in Zybooks (even if it grades a zero for your submission). We won't be able to re-evaluate your code if it's not saved (successfully uploaded) in Zybooks, when you may have a reason to believe that your submission was graded properly. If you figure out that your submission is not saved in Zybooks, please notify your respective TA with a copy of your submission. It doesn't garauntee you a grade, but you would have a proof for submission within deadline 5- How your code is tested We will have test benches (small programs), each of which will check a part of your implementation, by creating object(s) of DynamicArray class and then call accessor/methods etc to modify the object and match them with expected changes. For example if you start with a DynamicArray object of size 2 and add three elements we expect the arraySize should be 4. So if we call getArraySize on that object, the output should be 4 To help you with testing the correctness of your implementation, five sample test benches are provided below. Expected outputs are provided for each test bench which helps you figure out what is testing in each test bench. You can put following code into a main.cpp and run it to test the functionalities of your class. Submission of this main.cpp, but you must submit a main function (can be empty) for the program to be tested correctly in Zybook. Sample Main Function #include "DynamicArray.cpp" int main) [ DynamicArray Darr (1) Darr.addElement (1) Darr.addElement (2) Darr.addElement (3) Darr.addElement (4) Darr.addElement(5) if (Darr.getArraySize -8) cout
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started