Implement a single-arg constructor that sets the initial


Write a C++ program that defines, implements, and tests all member functions of a class called DynamicArray which mimics the capabilities of a C++ vector of ints. You may not use the standard C++ vector class or any code from the (decompiled) implementation of the standard vector class in your implementation. Use a primitive array of ints as the internal data structure in your class.

You will need to implement the member functions and overloaded operators listed below under Detailed Requirements. As per the standing requirements, each member function and any nonmember (related) functions such as the << operator overload, must be used in at least one test case in main.

Standing Requirements for all Projects

SR1. Provide a standard comment block at the top of each file documenting the file name and other details listed below. Failure to provide the information will result in a 5-point deduction per file.

/*
File Name: .cpp or .h Author: your first and last name
Course: CSC 402 (502 for MSCS students) Date: date completed
*/

SR2. All code you submit should compile without errors, and should be free of runtime errors for typical or expected input values. For instance, if a function is written to expect an integer from 1 to 1000, any number in that range should be handled gracefully. Each syntax or runtime error will result in a 5-point deduction with a maximum of 25 points total.

SR3. Observe C++ stylistic conventions demonstrated in class or in the textbook and use standard C++ constructs, as opposed to C language features that may be supported in C++ due to its origin. For example, prefer cout to printf. Also, be consistent in your approach to indentation, block structure, etc. Gross violations may result in a 5-point deduction.

SR4. In general, each function in a program will require at least one test case that proves that it works. In other words, code that is never executed in the typical program run is considered untested and may result in point deductions based on the extent of the problem.

Project-specific Requirements

R1. Separate the class declaration, class implementation, and test driver (main) into 3 files: DynamicArray.h, DynamicArray.cpp, and TestDynamicArray.cpp. [Use the Rational.h, Rational.cpp, and TestRational.cpp files in the Chapter 5 code examples as a model of what to place in the class declaration, class implementation, and test driver.]

R2. Use standard "header guards" in the .h files or the #pragma once directive (if your compiler supports it).

R3. Implement a single-arg constructor that sets the initial capacity of the array, with a default of 10, which also serves as the default (no-arg) constructor. The size of the array, unless it is created from an existing array, is initialized to 0. Test two ways of constructing DynamicArray objects using this constructor syntax:

DynamicArray d1 ; DynamicArray d2(20) ;

R4. Implement a second constructor that takes a primitive array of ints and its length as the arguments and initializes the DynamicArray as a deep copy of the passed-in array. The size of the dynamic array is determined by the length of the passed-in array. Test this constructor call with a small array initialized with non-zero values.

int iArr[10] = {10,9,8,7,6,5,4,3,2,1} ;
DynamicArray d3(iArr, 10) ;

R5. Implement the Big Three: Destructor, Copy Constructor, and operator = with appropriate test cases for all three.

R6. Implement the following accessor member functions that will mirror the functions of the vector class with the same name: Note that front() and back() for an empty vector are undefined in the specification. For our class, return negative INT_MAX if the vector is empty

• int size()
• int capacity()
• bool empty()
• int front(), int back()

R7. Implement the following additional accessor member function:

• int find ( int val )

searches the dynamic array for the value specified and returns the index if found, -1 if not found

R8. Implement the following mutator functions:

• void clear( )

sets the array's size to 0, but does not necessarily need to deallocate the memory occupied.

• void push_back( int val )

adds val to the end of the array. Capacity is extended if necessary.

• int insert ( int pos, int val )

inserts int value val at position pos and returns the position if successful, -1 if unsuccessful. Existing content is shifted to the right and the capacity is extended if necessary.

• void pop_back ()

ostensibly removes the last element in the array. Size is adjusted, but you do not have to reclaim the memory. Behavior for an empty vector is undefined in the standard, but it generally silently does nothing.

R9. Implement an overloaded << operator such that the following code will function correctly:

DynamicArray d1(10) ;

cout << d1 << endl ;

R10. Implement an overloaded == operator such that d1 == d2 will return true if and only if the variables d1 and d2 are DynamicArrays with the same size and the same elements. Capacity is not a factor.

R11. Provide inline comments in the class declaration, implementation, and main to describe the purpose of blocks of code, to set apart sections of the class declaration, etc., following the code examples for Chapter 5. There is no set minimum or maximum. Use your best judgment.

R12. The internal data members are to be named _size, _capacity, and _arr to disambiguate them from member functions size() and capacity() or possible parameters named arr.

R13. Upload the 3 files to the BlackBoard drop box associated with Project 2 as one zip file named __Project2.zip.

You may want to write a utility member function called diag() that simply prints the internal state of a DynamicArray instance, showing capacity, size, front and back elements, and so on, in an easy to read format.

Solution Preview :

Prepared by a verified Expert
C/C++ Programming: Implement a single-arg constructor that sets the initial
Reference No:- TGS01285486

Now Priced at $50 (50% Discount)

Recommended (94%)

Rated (4.6/5)