Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help overloading the + operator for my CS 2124 class There is a very useful feature that we could add to our ElasticArray

I need help overloading the + operator for my CS 2124 class

There is a very useful feature that we could add to our ElasticArray class at this point: concatenation. In other words, if we have two arrays a and b, where a contains and b contains , then the concatenation of the two, written a + b would be an array containing .

To make this work, we need to overload the addition (+) operator so that it can operate with an ElasticArray on both the left and right. Since the left-hand operand of a binary operator is always the current object (*this), we need to define an operator method with a single parameter to receive the right-hand operand. The prototype should look like this:

ElasticArray operator+(const ElasticArray& rhs) const; 

Add this prototype to your ElasticArrays public interface. Notice that both the left-hand operand (*this) and the right-hand operand are const-qualified. The concatenation operation does not change either of its operands instead, it returns a new array that contains the elements of both.

The implementation should proceed according to the following algorithm:

Declare an ElasticArray instance with a specified size equal to exactly enough elements to contain the values actually stored in the left and right hand arrays (e.g. don't allocate any extra capacity). This will be called the "destination" array from this point forward. Place each element from the left-hand array into the first part of the destination array. (elements 0 to size_of_array_on_left-1) Place each element from the right-hand array into the destination array beginning at the next available element following those filled in the previous step. (elements size_of_array_on_left to total_required_size-1) Return the destination array. 

Hint: You will not need to do any dynamic memory allocation to implement concatenation. All of the tools you need are already provided by the ElasticArray class, or by simple language features (loops, etc.).

Test the implementation of your operator+ concatenation operator. Be sure to verify that the resulting arrays logical size matches its storage capacity.

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

Pro SQL Server Administration

Authors: Peter Carter

1st Edition

1484207106, 9781484207109

More Books

Students also viewed these Databases questions