////////////////////////////////////////////////////////////////////////////// // // Title : exercise2.cpp // Project : OSCAR tutorial // Created : May 30, 1998 // Author : Mitch Pryor // Platforms : PC // Copyright : Copyright© The University of Texas at Austin, 2002. // All rights reserved. // // This software and documentation constitute an unpublished work // and contain valuable trade secrets and proprietary information // belonging to University. None of the foregoing material may be // copied or duplicated or disclosed without the express, written // permission of University. UNIVERSITY EXPRESSLY DISCLAIMS ANY // AND ALL WARRANTIES CONCERNING THIS SOFTWARE AND DOCUMENTATION, // INCLUDING ANY WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A // PARTICULAR PURPOSE, AND WARRANTIES OF PERFORMANCE, AND ANY WARRANTY // THAT MIGHT OTHERWISE ARISE FROM COURSE OF DEALING OR USAGE OF TRADE. // NO WARRANTY IS EITHER EXPRESS OR IMPLIED WITH RESPECT TO THE USE OF // THE SOFTWARE OR DOCUMENTATION. Under no circumstances shall // University be liable for incidental, special, indirect, direct or // consequential damages or loss of profits, interruption of business, // or related expenses which may arise from use of software or documentation, // including but not limited to those resulting from defects in software // and/or documentation, or loss or inaccuracy of data of any kind. // // Purpose : Solution to Exercise 2 of the OSCAR Guide // (Condensed Solution, not sufficiently robust) // ////////////////////////////////////////////////////////////////////////////// #include #include "Math\Vector.h" class RRVector4 : public RRVector { public: RRVector4(){ size = 4; element = _m; element[0] = 0.0; element[1] = 0.0; element[2] = 0.0; element[3] = 0.0; } RRVector4(double *a){ size = 4; element = _m; element[0] = a[0]; element[1] = a[1]; element[2] = a[2]; element[3] = a[3]; } RRVector4(double x, double y, double z, double w) { size = 4; element = _m; element[0] = x; element[1] = y; element[2] = z; element[3] = w; } RRVector4(double a){ size = 4; element = _m; element[0] = element[1] = element[2] = element[3] = a; } RRVector4(const RRVector4& V){ init(4,V.GetArray()); } RRVector4(const RRVector& V){ if(V.GetSize() != 4) DisplayError("RRVector4::RRVector4(const RRVector& V): V not of size 4\n"); init(4,V.GetArray()); } ~RRVector4(){ element = 0; } double& X() { return element[0]; } double& Y() { return element[1]; } double& Z() { return element[2]; } double& W() { return element[3]; } RRVector4 operator-(void) const { RRVector4 r(-element[0], -element[1], -element[2], -element[3]); return r; } RRVector4 operator/(double a) const { if(a == 0){ DisplayError("error: RRVector4 operator '/', division by zero\n"); abort(); } RRVector4 r(*this); r.at(0) /= a; r.at(1) /= a; r.at(2) /= a; r.at(3) /= a; return r; } RRVector4 operator*(double a) const{ RRVector4 r(*this); r.at(0) *= a; r.at(1) *= a; r.at(2) *= a; r.at(3) *= a; return r; } RRVector4& operator=(const RRVector4& v){ if(this != &v) at(0) = v.at(0); at(1) = v.at(1); at(2) = v.at(2); at(3) = v.at(3); return *this; } protected: double _m[4]; }; ////////////////////////////////////////////////////////////////////////////// // Begin main ////////////////////////////////////////////////////////////////////////////// int main(void) { RRVector4 vA(6.4, -7.2, 4.0, -7.8); cout << "vA: " << vA << "\n" << endl; cout << "Z of vA: " << vA.Z() << "\n" << endl; cout << "Multiply by 5: " << vA*5 << "\n" << endl; cout << "Divide by 5: " << vA/5 << "\n" << endl; return 0; }