C++ Code 9.16 Lab 06: Dynamic Arrays (Part 2) Step 1a: Preparation The suite of Part 1 tests has been moved to its own function. The code in main() is focused on testing the new operators you'll write in this part. main() relies on a change you're going to make to the DynamicArray::print() member function (coming up in Step 1b). Instead of directly using cout, this member function is now going to print to a stringstream. This allows my test code to examine the output and check it automatically. How 3 representative lines of the new main program work:

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter8: Arrays And Strings
Section: Chapter Questions
Problem 28SA
icon
Related questions
Question

C++ Code 9.16 Lab 06: Dynamic Arrays (Part 2)

Step 1a: Preparation

  • The suite of Part 1 tests has been moved to its own function.
  • The code in main() is focused on testing the new operators you'll write in this part.
  • main() relies on a change you're going to make to the DynamicArray::print() member function (coming up in Step 1b). Instead of directly using cout, this member function is now going to print to a stringstream. This allows my test code to examine the output and check it automatically.

How 3 representative lines of the new main program work:

// Use stringstream to capture your print function's output.
stringstream testoutput;

// Use the same testoutput variable over and over, so we reset it to the empty string between tests using its str() member function.
testoutput.str(""); // Reset test output string

// Call the print function on your array, and it prints to testoutput instead of cout.
a.print(testoutput);

// Use the RunIndividualTest function also included in main.cpp to compare the output you got to the desired output.
// We call the str member function with no parameters as it converts the stringstream's contents to a normal string.
pass &= RunIndividualTest("sampledesiredoutput", testoutput.str(), "NameOfTest");

Step 1b: Modify some methods

Make changes to 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;
    • int cap() const { return capacity; }
  2. Modify print to take an output stream as a parameter instead of assuming cout. The new prototype for the .h file is:
    • void print(std::ostream& s) const;

Change the implementation in the .cpp file by replacing "cout" with "s" then recompile your code. 

Compile and run the new program and verify that it shows you still pass the Part 1 tests.

Step 2: Copy Constructor

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 contents from the other object as needed.

Modify it for this class and add it to the header file.

  • Classname(const Classname& source);

For definition, follow the prompts:

// 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 
    // objects referencing the same underlying storage.

    // 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++) {
      
    }

Then 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;

What's happening in your code in order:

  1. Created an empty DynamicArray, a.
  2. Created a DynamicArray, b, as a copy of a (also empty).
  3. Ran the Part 1 tests on a, which added and removed a bunch of values; leaving the array with only one element: 152.
  4. Run the same tests on b, with the same result.

Compile this code and verify that you passed the tests.

Step 3: Equality Comparison Operator

We don't need arr == other.arr for the arrays to be equal. 

For declaration: The general form of the prototype is below. Modify it for this class and add it to the header file.

  • bool operator==(const Classname& source) const;

Follow the prompts below for definition:

    // Check for matching length

    // Check to make sure that each element matches

    // Be sure you've returned true if and only if the length matches and every individual element up to the length also matches. Otherwise, make sure you've returned false.

When you've defined this operator, un-comment the equality comparison test in main (to just before some append and remove calls), recompile, and rerun.

Step 4: Assignment Operator

Modify general form of the prototype for this class and add it to the header file:

  • Classname& operator= (const Classname& source);

The last line of your assignment operator definition will always be:

return *this; // return the thing "this" points to; i.e. the current object

Follow the prompts below for definition:

    // For member variables that are not pointers, do the same as the copy constructor

    // Before deallocating current array, double-check that the client isn't doing something like myarr = myarr;

    //     1. Deallocate current object's array
    //     2. Reallocate array to the new capacity
    //     3. Copy data from old array
 
    // The safest thing is to just return now in this case is already equal to itself and there's no work to do.

    if (this == &source) {
        // get out!!
    }
   
    // Reallocate and copy (same code as copy constructor)
    
    // Need a return statement

Uncomment the code that prints the final output and ensure that it matches the expected output.

Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Concept of pointer parameter
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning