/*
Grant Chen
Due: 10/30/08
CS213
Student ID 908306235
Compiler: Dev-C++
Exercise 6 part 2
We have previously discussed vectors, which are like arrays that can grow in
size. Suppose that vectors were not defined in C++. Define a class called
VectorDouble that is like a class for a vector with base type double. 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.
*/
#ifndef ASGN6P2_H // Eliminating redefinition errors
#define ASGN6P2_H
#include
using namespace std;
class VectorDouble // teacher-written class definition
{
public:
//Constructors
VectorDouble();
VectorDouble(int size);
//Destructor
~VectorDouble();
void push_back(double val);
//POSTCONDITION: Val has been added to the end of the vector
double value_at(int i) const;
//POSTCONDITION: Returns the ith element of the vector, 0-based. Program
//terminates if i < 0 or i > count.
void change_value_at(double newVal, int i);
//POSTCONDITION: Returns the ith element of the vector, 0-based.
int size() const;
//POSTCONDITION: Returns the number of elements in the array
int capacity() const;
//POSTCONDITION: Returns the size of the array
void reserve(int size);
//POSTCONDITION: If size > maxCount the array length is inreased to size,
//otherwise no change is made.
void resize(int size);
//POSTCONDITION: The array is changed to the given size, losing elements
//if necessary. Program terminates if size < 0;
friend ostream& operator <<(ostream& outs, const VectorDouble& v);
private:
void expandCapacity();
//POSTCONDITION: The capacity of the array is doubled.
double *elements;
int maxCount; // the size of the array
int count; // the number of elements stored in the array
};
#endif