Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this programming assignment, you will design and implement a standard array class for characters, i.e., char types. The goal of this programming assignment is

For this programming assignment, you will design and implement a standard array class for characters, i.e., char types. The goal of this programming assignment is to provide a refresher for programming abstract data types (ADTs) in C++. You therefore do not have to completely design and implement an array class in C++. Instead, the design of the array class has been provided for you. It is your job to implement the design given its source code shells by implementing the correct functionality for each method. The requirements for each method is specied in the arrays header le (i.e., Array.h).

Please help fill in the functions below for an array. What needs to be done is included in comment form.

#ifndef _ARRAY_H_

#define _ARRAY_H_

#include // for size_t definition

/**

* @class Array

*

* Basic implementation of a standard array class for chars.

*/

class Array

{

public:

/// Default constructor.

Array (void);

/**

* Initializing constructor.

*

* @param[in] length Initial size

*/

Array (size_t length);

/**

* Initializing constructor.

*

* @param[in] length Initial size

* @param[in] fill Initial value for each element

*/

Array (size_t length, char fill);

/**

* Copy constructor

*

* @param[in] arr The source array.

*/

Array (const Array & arr);

/// Destructor.

~Array (void);

/**

* Assignment operation

*

* @param[in] rhs Right-hand side of equal sign

* @return Reference to self

*/

const Array & operator = (const Array & rhs);

/**

* Retrieve the current size of the array.

*

* @return The current size

*/

size_t size (void) const;

/**

* Retrieve the maximum size of the array.

*

* @return The maximum size

*/

size_t max_size (void) const;

/**

* Get the character at the specified index. If the index is not

* within the range of the array, then std::out_of_range exception

* is thrown.

*

* @param[in] index Zero-based location

* @exception std::out_of_range Invalid \a index value

*/

char & operator [] (size_t index);

/**

* @overload

*

* The returned character is not modifiable.

*/

const char & operator [] (size_t index) const;

/**

* Get the character at the specified index. If the \a index is not within

* the range of the array, then std::out_of_range exception is thrown.

*

* @param[in] index Zero-based location

* @return Character at \index

* @exception std::out_of_range Invalid index value

*/

char get (size_t index) const;

/**

* Set the character at the specified \a index. If the \a index is not

* within range of the array, then std::out_of_range exception is

* thrown.

*

* @param[in] index Zero-based location

* @param[in] value New value for character

* @exception std::out_of_range Invalid \a index value

*/

void set (size_t index, char value);

/**

* Set a new size for the array. If \a new_size is less than the current

* size, then the array is truncated. If \a new_size if greater then the

* current size, then the array is made larger and the new elements are

* not initialized to anything. If \a new_size is the same as the current

* size, then nothing happens.

*

* The array's original contents are preserved regardless of whether the

* array's size is either increased or decreased.

*

* @param[in] new_size New size of the array

*/

void resize (size_t new_size);

/// Shrink the array to reclaim unused space.

void shrink ();

/**

* Locate the specified character in the array. The index of the first

* occurrence of the character is returned. If the character is not

* found in the array, then -1 is returned.

*

* @param[in] ch Character to search for

* @return Index value of the character

* @retval -1 Character not found

*/

int find (char ch) const;

/**

* @overload

*

* This version allows you to specify the start index of the search. If

* the start index is not within the range of the array, then the

* std::out_of_range exception is thrown.

*

* @param[in] ch Character to search for

* @param[in] start Index to begin search

* @return Index value of first occurrence

* @retval -1 Character not found

* @exception std::out_of_range Invalid index

*/

int find (char ch, size_t start) const;

/**

* Test the array for equality.

*

* @param[in] rhs Right hand side of equal to sign

* @retval true Left side is equal to right side

* @retval false Left side is not equal to right side

*/

bool operator == (const Array & rhs) const;

/**

* Test the array for inequality.

*

* @param[in] rhs Right-hand size of not equal to sign

* @retval true Left side is not equal to right side

* @retval false Left size is equal to right side

*/

bool operator != (const Array & rhs) const;

/**

* Fill the contents of the array.

*

* @param[in] ch Fill character

*/

void fill (char ch);

/// Reverse the contents of the array such that the first element is now

/// the last element and the last element is the first element.

void reverse (void);

/**

* The slice() method returns a shallow copy of a portion of an array into

* a new array object selected from begin to end (end not included). The original

* array will not be modified.

*

* @param[in] begin The starting index

* @return A new Array object

*/

Array slice (size_t begin) const;

/**

* @overload

*

* @param[in] begin The starting index

* @param[in] end The ending index

* @return A new Array object

*/

Array slice (size_t begin, size_t end) const;

private:

//////////////////////////////////////////////////////////////////////////////

/// DEFINE ANY HELPER METHODS HERE

//////////////////////////////////////////////////////////////////////////////

/// Pointer to the actual data.

char * data_;

/// Current size of the array.

size_t cur_size_;

/// Maximum size of the array.

size_t max_size_;

};

#include "Array.inl"

#endif // !defined _ARRAY_H_

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

SQL For Data Science Data Cleaning Wrangling And Analytics With Relational Databases

Authors: Antonio Badia

1st Edition

3030575918, 978-3030575915

More Books

Students also viewed these Databases questions