#ifndef VEC_H #define VEC_H /////////////////////////////////////////////////////////////// // vec.h - declares an n Dimensional vector // // Exam1:Q2 // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 05 // /////////////////////////////////////////////////////////////// #include namespace Exam1 { ///////////////////////////////////////////////////// // This is the required part of the answer template class vec { public: vec(int n, const T& t = T(0)); // vec(const vec& v); // correct compiler generated // vec& operator=(const vec& v); // " " " T& operator[](int n); // v1[1] = t; T operator[](int n) const; // t = v1[1]; vec& operator+=(const vec& v); // v2 += v1; vec& operator-=(const vec& v); // v2 -= v1; vec& operator*=(const T &t); // v2 *= t; T size() const { return data.size(); } private: std::vector data; }; /////////////////////////////////////////////////////// // global operators template vec operator+(const vec& v1, const vec& v2); // v3 = v1 + v2; template vec operator-(const vec& v1, const vec& v2); // v3 = v1 - v2 template vec operator*(const vec& v, const T& t); // v2 = v1*t; template vec operator*(const T& t, const vec& v); // v2 = t*v1; // end of required part of answer /////////////////////////////////////////////////////// // //----< size validation >------------------------------------ template void checkSize(const vec& v1, const vec& v2) { if(v1.size() != v2.size()) throw exception("incompatible vector sizes"); } //---< construct vector with n elements >-------------------- template vec::vec(int n, const T& t) { for(int i=0; i--------------------------------- template vec& vec::operator+=(const vec& v) { checkSize(*this,v); for(int i=0; i--------------------------- template vec operator+(const vec& v1, const vec& v2) { checkSize(v1,v2); vec temp = v1; return (temp += v2); } // //----< subtractive assignment >----------------------------- template vec& vec::operator-=(const vec& v) { checkSize(*this,v); for(int i=0; i------------------------ template vec operator-(const vec& v1, const vec& v2) { checkSize(v1,v2); vec temp = v1; return (temp -= v2); } //----< scalar multiplicative assignment >------------------- template vec& vec::operator*=(const T &t) { for(int i=0; i-------------- template vec operator*(const T &t, const vec& v) { vec temp = v; temp *= t; return temp; } //----< global scalar multiplication operator >-------------- template vec operator*(const vec& v, const T &t) { vec temp = v; temp *= t; return temp; } // //----< index operator >------------------------------------- template T& vec::operator[](int n) { if(n<0 || size()<=n) throw exception("index out of range in operator[]"); return data[n]; } //----< index operator >------------------------------------- template T vec::operator[](int n) const { if(n<0 || size()<=n) throw exception("index out of range in operator[]"); return data[n]; } //----< global display of vector contents >------------------ template void show(const vec& v, const std::string& name="") { cout << "\n "; if(name.size() != 0) std::cout << "\n " << name << "\n "; for(int i=0; i