Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A array is an Abstract Data Type (ADT) with operations such as get , set , and size . This assignment focuses upon building and

A array is an Abstract Data Type (ADT) with operations such as get , set ,

and size . This assignment focuses upon building and using an array. Your task

is to implement an array template of integers and characters in C++. This

array will be different from C++ built-in arrays in the following ways:

The initial size of the array can be a run-time parameter, to implement

this you'll need to use the C++ new and delete operators.

Operations on the array will be range-checked thus, if you try to get or

set an array element that is out of range the operation will return a "failure"

status.

Your task is to write C++ methods that operate upon objects of class Array.

Array.h, Array.cpp, and main.cpp are included below.

The Array.h includes class declaration of ADT Array.

The main.cpp is an empty test driver allows you to test your code.

The Array.cpp is where you will add the methods to implement the Array ADT.

================================================================

#ifndef Array_h

#define Array_h

#include

template class Array { public: // === Initialization and termination methods ===

//! Constructor (uninitialized) // Dynamically create an uninitialized array. // Throw if allocation fails Array (size_t size);

//! Constructor (initialized) // Dynamically initialize an array. // Throw if allocation fails Array (size_t size, const T& default_value);

//! Copy constructor // Perform initialization by using another instance. // Throw if allocation fails Array (const Array& s);

//! Assignment operator '=' // Assignment operator performs an assignment by // making a copy of the contents of parameter // e.g., *this == s will return true. // Note that // if the of is >= than // we can copy it without reallocating. // However, if is < than // we must delete the // and reallocate a new Array &operator= (const Array &s); Array& operator= (const Array& s);

//! Destructor // Clean up the array (e.g., delete dynamically allocated memory) ~Array (void);

// === Set/get method ===

//! Transformer (Setter) // Set an item in the array at location index. // Return -1 if index is larger than the size() of the array, // else return 0; int set (const T& new_item, size_t index);

//! Observer (Getter) // Get an item in the array at location index. // Return -1 if index is larger than the size() of the array, // else return 0; int get (T& item, size_t index) const;

//! Item retrieving operator '[index]' // Returns a reference to the element in the // without checking for range errors. const T& operator[] (size_t index) const;

//! Item setting operator '[index]' // Set an item in the array at location index without // checking for range errors; T& operator[] (size_t index);

//! Returns the current size of the array size_t size (void) const;

//! Comparison (equality) operator '==' // Compare this array with for equality. // Return true if the size()'s of the two arrays are equal and // all the elements from 0 ... size() are equal, else false. bool operator== (const Array& s) const;

//! Comparison (inequality) operator '!=' // Compare this array with for inequality // such that <*this> != is always the complement of the // boolean return value of <*this> == . bool operator!= (const Array& s) const;

private: //! Within range checking // Returns true if is within range, // i.e, 0 <= < , else return false. bool in_range (size_t index) const;

//! Maximun size of the array // i.e., the total number of elements in . size_t max_size_;

//! Current size of the array // This starts out being == to // . However, if we are assigned a smaller array, // then will become less than . // The purpose of keeping track of both sizes is to avoid // reallocatiing memory if we don't have to. size_t cur_size_;

//! Pointer to the array's storage buffer T* array_; };

#endif //Array_h

===================================================

#include "Array.h" #include using namespace std;

template Array::Array (size_t size) { /* Your code here */ }

template Array::Array (size_t size, const T& default_value) { /* Your code here */ }

template Array::Array (const Array& s) { /* Your code here */ }

template Array Array::operator= (const Array& s) { /* Your code here */ }

template Array::~Array (void) { /* Your code here */ }

template int Array::set (const T &new_item, size_t index) { /* Your code here */ }

template int Array::get (T &item, size_t index) const { /* Your code here */ }

template const T& Array::operator[] (size_t index) const { /* Your code here */ }

template T& Array::operator[] (size_t index) { /* Your code here */ }

template size_t Array::size (void) const { /* Your code here */ }

template bool Array::operator== (const Array& s) const { /* Your code here */ }

template bool Array::operator!= (const Array& s) const { /* Your code here */ }

template bool Array::in_range (size_t index) const { /* Your code here */ }

=================================================

#include "Array.h" #include "Array.cpp"

#include using namespace std;

int main(int argc, char* argv[]) { cout << " " <<" "; return 0; }

=================================================

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

The Structure Of The Relational Database Model

Authors: Jan Paredaens ,Paul De Bra ,Marc Gyssens ,Dirk Van Gucht

1st Edition

3642699588, 978-3642699580

More Books

Students also viewed these Databases questions