Step 1b: Modify some methods Make the following changes to your DynamicArray code: 1. Add the word const at the end of the at, sum, print, and cap function headers in both the .cpp and the .h files. For example: int sum) const; O O int cap const { return capacity; } 2. Modify print to take an output stream as a parameter instead of assuming cout. Here's the new prototype for the .h file: o void print(std::ostream& s) const; Change the implementation in the .cpp file too. All you have to do is replace "cout" with "s". Recompile your code. If it still compiles, you should be good to go. If it doesn't, the answer might be that you made an unnecessary/incorrect modification to the array within one of these functions! Compile and run the new program, and verify that it shows you still pass the Part 1 tests. Step 2: Copy Constructor The next step is to define a copy constructor for DynamicArray. How do we implement a copy constructor? Technically, it's up to you, since you're the one writing the class. But 90+% of the time, you'll do the following for each member variable: 1. If the member variable is not a pointer: copy it directly from the other object 2. If the member variable IS a pointer to dynamically allocated memory: allocate your own space in memory, and copy the contents from the other object as needed Note that, in the example used in class (Point2D), the copy constructor was very simple because both member variables were not pointers. However, our DynamicArray class has a member variable that is a pointer. For declaration: Note the general form of the prototype below. Modify it for this class and add it to the header file. Classname(const Classname& source); For definition: follow the prompts below: // capacity and len are ints, so they're covered by case 1 capacity= len = // arr points to dynamically allocated memory. // Directly copying arr = other.arr doesn't actually // COPY that memory -- it just means we have two DynamicArray //objects referencing the same underlying storage. // So we have to first allocate new memory for this new object, // and then copy over the values from the other object. arr = // Copy the elements from other array to our new space for (int i = 0; i< len; i++) { // This code only copies the actual elements, not the garbage // in the empty slots at the end of the array When you are done, un-comment these lines in main.cpp: DynamicArray b(a); cout << "*** Lab 6 (Part 1) tests on array created with copy constructor: " << (RunPart1Tests (b) ? "passed": "failed") << endl; Here's what's happening in your code now, in order: 1. You create an empty DynamicArray, a. 2. You create a DynamicArray, b, as a copy of a (so it's also empty). 3. You run the Part 1 tests on a, which add and remove a bunch of values. They leave the array with only one element: 152. 4. You run the same tests on b, with the same result. Compile this code and verify that you passed the tests.
C++ Dynamic Array Project Part 2
Algorithm for DynamicArray Class with Copy Constructor
1. Create a class named DynamicArray with private data members: arr (int pointer), capacity (int), and len (int).
2. Define the following public member functions:
- DynamicArray(): Constructor that initializes arr to nullptr, capacity to 0, and len to 0.
- DynamicArray(const DynamicArray& source): Copy constructor that creates a deep copy of the source object.
- ~DynamicArray(): Destructor that frees the dynamically allocated memory in arr.
- int at(int index) const: Returns the element at the specified index or -1 if the index is out of bounds.
- int sum() const: Returns the sum of all elements in the array.
- void print(std::ostream& s) const: Prints the elements of the array to the specified output stream.
- int cap() const: Returns the capacity of the array.
3. In the DynamicArray constructor, initialize arr to nullptr, capacity to 0, and len to 0.
4. In the copy constructor:
- Copy the capacity and len from the source object to the current object.
- Allocate a new int array of size capacity for the current object.
- Copy the elements from the source object's arr to the current object's arr.
5. In the destructor, free the memory allocated for arr using 'delete[]'.
6. In the at(int index) function:
- Check if the index is out of bounds (index < 0 or index >= len).
- If out of bounds, return -1.
- Otherwise, return the element at the specified index.
7. In the sum() function, initialize a result variable to 0, and then iterate over the elements in arr and add them to the result. Return the result.
8. In the print(std::ostream& s) function, iterate over the elements in arr and print them to the specified output stream 's'.
9. In the cap() function, return the capacity of the array.
Algorithm for RunPart1Tests Function (Sample):
1. Create a function named RunPart1Tests that takes a reference to a DynamicArray object as a parameter.
2. Within the function, add elements to the DynamicArray object, 'arr', to test its functionality.
- Add elements using the DynamicArray's methods (e.g., push_back).
- Remove elements using the DynamicArray's methods (e.g., pop_back).
- Perform various operations to test the array's behavior.
3. Test the at() and sum() methods by accessing and calculating the sum of elements at various indices.
4. Print the contents of the array using the print() method to verify the changes.
5. Evaluate the test conditions and return 'true' if the tests pass.
End of Algorithm.
Step by step
Solved in 4 steps with 4 images