08 Lab - ADT GenericArray
08 Lab - ADT GenericArray
Lab 08 Marks 10
Instructions
➢ Work in this lab individually.
➢ You can use your books, notes, handouts etc. but you are not allowed to borrow anything from your peer student.
➢ Make sure to follow the best coding practices.
➢ Include comments to explain the logic where necessary.
➢ You are strictly NOT ALLOWED to include any additional data-members/functions/constructors in your class.
➢ Test your program thoroughly with various inputs to ensure proper functionality and error handling.
➢ Show your work to the instructor before leaving the lab to get some or full credit.
ADT: GenericArray
Write a generic class named GenericArray capable of managing arrays of any valid data type (e.g., int, float, char). The class should
provide functionality for creating, manipulating, and comparing arrays, ensuring robust error handling and memory management.
1. The class should have the following two private data members.
• A generic pointer called data that holds an array of given data type allocated dynamically according to the specified size.
• An integer called size represents the number of elements in the array.
• setElement(int index, T value): Inserts a new element at the specified index in the array. If the index is out of bounds,
display an appropriate error message.
• countElement(T key): Accepts a key and counts the total occurrences of it in the array.
• operator=: Copies the data of the right-hand-side object to the left-hand-side object. If the sizes are different, reallocate
memory for the left-hand-side object based on the size of the right-hand-side object.
• getSubArray(int start_index, int end_index): Returns a new array containing values from start_index to end_index,
inclusive. If the requested sub-collection cannot be created, the function should throw an exception with an appropriate
error message.
• operator+: Appends the contents of the right-hand-side array to the end of the left-hand-side array, returning the result.
• operator==: Determines whether two arrays are equal or not. The operator should return true if both arrays (left-hand-
side and right-hand-side) are equal, and false otherwise.
• Subscript ([]) for non-const objects. If the index is out of bounds, throw an exception with an appropriate error message.
• Subscript ([]) for const objects. If the index is out of bounds, throw an exception with an appropriate error message.
4. Test the functionality of the GenericArray class by creating objects of different data types and performing various operations
on them.