1. Engineering
  2. Computer Science
  3. its c program write a class named myvector using the...

Question: its c program write a class named myvector using the...

Question details

ITs C++ program

Write a class named MyVector using the following UML diagram and class attribute descriptions. MyVector will use a partially filled array to store a collection of integers. The array will be created using dynamic memory allocation.

UML Diagram:

xid-53226854_1

Class Attributes:

Variables:

  • list - an integer pointer. Holds the memory address for an integer array.
  • max_size - an integer that holds the capacity of the array.
  • list_size - an integer that stores the number of valid values currently stored in the array. The valid values in the array start at element 0 and go through list_size - 1.

Methods:

  • constructor - dynamically allocates the array of integers, storing it's memory address in pointer list. Initializes max_size to 10 and list_size to 0.
  • destructor - deletes the array.
  • push_back - If the array isn't full, assigns it's argument to list[list_size] and then increments list_size. The array is full if list_size == max_size. If the array is full, a new array twice the size as the original replaces the old array. The contents of the old array are copied to the new array.

    ALGORITHM:
    IF list_size equals max_size:
    SET max_size to 2 times max_size
    DYNAMICALLY allocate a new int array with max_size elements.
    SET temporary pointer temp_list to the address of the new array.
    SET j to 0
    WHILE j is less than list_size:
    SET temp_list[j] to list[j]
    INCREMENT j
    END WHILE
    DELETE(list)
    SET list to temp_list
    SET list[list_size] to the argument
    INCREMENT list_size
    END IF
  • pop_back - if the array isn't empty (list_size == 0), decrements list_size. Throws an exception otherwise. The exception is a c-string: "EMPTY VECTOR".
  • at - uses it's argument as a subscript for the array. Returns that element. It's return type is an integer reference (int&). Throws an exception if the argument is an invalid index. The exception is a c-string: "OUT OF BOUNDS".
  • clear - sets list_size to 0. That's it. This effectively "empties" the array. Note, nothing about the array itself changes.
  • size - returns the list_size variable.
Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution