From d053a4c56354bf2b0b37ecb406edfe75871f8ae8 Mon Sep 17 00:00:00 2001 From: "U-DAMIEN\\DamienN" Date: Sun, 7 Mar 2010 19:46:23 +0100 Subject: [PATCH] classe 'Vector3' : ajout d'un constructeur et surcharge de l'operateur 'ostream& operator<< ()'. Constructeur ajoute : initialisation par tableau statique (ajoute pour se conformer a la classe 'Vector') Quelques ajustements mineurs. --- math/inc/vector3.h | 18 ++++++++++++------ math/src/vector3.cc | 17 ++++++++++++++++- testing/math/testVector3.cc | 15 +++++++++++---- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/math/inc/vector3.h b/math/inc/vector3.h index 6cc01ae..f089f8f 100644 --- a/math/inc/vector3.h +++ b/math/inc/vector3.h @@ -15,6 +15,7 @@ class Vector3 Vector3 (); Vector3 (const double cmp1, const double cmp2, const double cmp3); Vector3 (STANDARD_BASIS aVector); //To instantiate a particular vector of R3 + Vector3 (double components[]); Vector3 (const Vector3 &otherVector); virtual ~Vector3 (); @@ -23,7 +24,8 @@ class Vector3 void setVector (const double cmp1 = 0, const double cmp2 = 0, const double cmp3 = 0); - //Operators + /*Operators + All operators can throw logic_error exceptions*/ void operator = (const Vector3 &otherVector); Vector3 operator + (const Vector3 &otherVector) const; @@ -47,14 +49,18 @@ class Vector3 bool operator == (const Vector3 &otherVector) const; bool operator != (const Vector3 &otherVector) const; - //Can throw out_of_range exceptions - // Math like getter/setter + /*Can throw out_of_range exceptions + /!\ index is understood as a mathematical index, not as a general C++ index /!\ + See method 'at()' for the C++ like getter/setter*/ double operator [] (const unsigned int index) const; double& operator [] (const unsigned int index); - // C++ like getter/setter - double at (const unsigned int index) const; - double& at (const unsigned int index); + /*Can throw out_of_range exception + C++ like getter/setter*/ + double at (const unsigned int index) const; + double& at (const unsigned int index); + + friend std::ostream& operator<< (std::ostream& os, const Vector3 &aVector); //Other methods diff --git a/math/src/vector3.cc b/math/src/vector3.cc index 43f59ff..f763e1f 100644 --- a/math/src/vector3.cc +++ b/math/src/vector3.cc @@ -1,4 +1,4 @@ -#include "vector3.h" +#include "../inc/vector3.h" using namespace std; @@ -47,6 +47,14 @@ Vector3::Vector3 (STANDARD_BASIS aVector) } } +Vector3::Vector3 (double components[]) +{ + m_components = new double[3]; + m_components[0] = components[0]; + m_components[1] = components[1]; + m_components[2] = components[2]; +} + //Copy-constructor Vector3::Vector3 (const Vector3 &otherVector) { @@ -228,6 +236,13 @@ double& Vector3::at (const unsigned int index) return m_components[index]; } +//Overload of operator<< to use with every ostream objects (cout, files, etc.) +ostream& operator<< (ostream& os, const Vector3 &aVector) +{ + os << aVector[1] << ' ' << aVector[2] << ' ' << aVector[3]; + return os; +} + //=================================================================== //Inner product calculation method (used by operator * and *=) diff --git a/testing/math/testVector3.cc b/testing/math/testVector3.cc index c59d0d6..5f6d563 100644 --- a/testing/math/testVector3.cc +++ b/testing/math/testVector3.cc @@ -1,16 +1,18 @@ #include -#include "vector3.h" +#include "../../math/inc/vector3.h" using namespace std; int main () { - Vector3 vec1, vec2 (2.0, 2.0, 2.0), e1 (Vector3::e1); + double array[3] = {1, -2.36, 5.78}; + Vector3 vec1, vec2 (2.0, 2.0, 2.0), e1 (Vector3::e1), vec (array); - cout << "Initialisation test :" << endl << "Output should be (0 0 0), (2 2 2), (1 0 0)" << endl; + cout << "Initialisation test :" << endl << "Output should be (0 0 0), (2 2 2), (1 0 0) and (1 -2.36 5.78)" << endl; vec1.show(); vec2.show(); e1.show(); + vec.show(); cout << "Copy constructor test :" << endl << "Output should be (2 2 2)" << endl; Vector3 vec3 (vec2); @@ -25,7 +27,7 @@ int main () cout << "Output should be (1 0 0), (1.5 2.36 0) and (3.1415 -62.36 5)" << endl; vec5.setVector(1); vec5.show(); vec5.setVector(1.5, 2.36); vec5.show(); - vec5.setVector(3.1415, -62.3, 5); vec5.show(); + vec5.setVector(3.1415, -62.36, 5); vec5.show(); cout << "Magnitude test :" << endl << "Output should be 3.4641 and 12" << endl; @@ -34,9 +36,14 @@ int main () cout << "Dot product test :" << endl << "vec2 * e1 = 2 and vec2 * vec3 = 12" << endl; cout << vec2.dotProduct(e1) << '\t' << vec2.dotProduct(vec3) << endl; + cout << endl << "**** End of methods testing ****" << endl << endl; + cout << endl << "**** Beginning of operators testing ****" << endl <