{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Vector - #ifndef_VECTOR_H#define_VECTOR_H#include...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
#ifndef __VECTOR_H #define __VECTOR_H #include <stdexcept> using namespace std; // Deal with constants if they are not defined #ifndef INITIAL_CAPACITY #define INITIAL_CAPACITY 10 #endif #ifndef EXPANSION_FACTOR #define EXPANSION_FACTOR 2 #endif #ifndef EXPANSION_CONSTANT #define EXPANSION_CONSTANT 0 #endif template<typename ItemType> class Vector { private: /** The current capacity of the vector */ int currentCapacity; /** The current size of the vector */ int numItems; /** Array that contains the data */ ItemType *data; public: /** * Default constructur */ Vector<ItemType>() : currentCapacity(INITIAL_CAPACITY), numItems(0), data(new ItemType[INITIAL_CAPACITY]) { } /** * Copy constructor */ Vector<ItemType>(const Vector<ItemType>& other) : currentCapacity(other.currentCapacity), data(new ItemType[other.currentCapacity]), numItems(other.numItems) { for (int i=0; i<numItems; i++) data[i] = other.data[i]; } /** * Destructor */ ~Vector<ItemType>() { delete []data; } /** * Access the length */ int size() const { return numItems; }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
/** * Access an element at a particular position (and change it) */ ItemType& operator[](int index) { return data[index]; } /** *
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}