// revised_vector.h: based on simple_vector, but with const // based on Stroustrup, A Tour of C++ (2014), p. 37 #ifndef _MEGAVECTOR_H #define _MEGAVECTOR_H #include class AbstractContainer { public: virtual ~AbstractContainer() { } virtual int size() const = 0; virtual double& operator[](int index) = 0; virtual const double& operator[](int index) const = 0; }; class MegaVector : public AbstractContainer { public: static constexpr int SIZE = 1000000; MegaVector() { for(int i = 0; i < SIZE; ++i) elements[i] = 0.0; } int size() const override { return SIZE; } double& operator[](int index) override { if ( index < 0 || index >= SIZE ) throw std::out_of_range("Illegal index into vector."); return elements[index]; } const double& operator[](int index) const override { if ( index < 0 || index >= SIZE ) throw std::out_of_range("Illegal index into vector."); return elements[index]; } private: double elements[SIZE]; }; #endif