Explore BrainMass
Share

A C++ vector class implementation

This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

Your class VectorDouble will have a private member variable for a dynamic array of doubles. It will also have two member variables of type int; one called maxCount for the size of the dynamic array of doubles, and one called count for the number of array positions currently holding values. (maxCount is the same as the capacity of a vector; count is the same as the size of a vector.)

If you attempt to add an element (a value of type double) to the vector object of the class VectorDouble and there is no more room, then a new dynamic array with twice the capacity of the old dynamic array is created and the values of the old dynamic array are copied to the new dynamic array.

Your class should have all of the following:
? Two constructors: a default constructor that creates a dynamic array for 50 elements and a constructor with one int argument for the number of elements in the initial dynamic array.
? A destructor.
? Member functions push_back, capacity, size, reserve, and resize that behave the same as the member functions of the same names for vectors.
? Two member functions to give your class the same utility as the square brackets: value_at(i), which returns the value of the ith element in the dynamic array; and change_value_at(d,i), which changes the double value at the ith element of the dynamic array to d. Enforce suitable restrictions on the arguments to value_at and change_value_at.

© BrainMass Inc. brainmass.com March 21, 2019, 5:08 pm ad1c9bdddf
https://brainmass.com/computer-science/cpp/a-c-vector-class-implementation-209925

Attachments

Solution Summary

This solution provides a C++ implementation of a vector class.

$2.19