Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //What the operator[] should do:
- //operator[]
- //The indexing operator should be overloaded to provide accessor methods for the class. Subscript 0 should //provide access to the x component value of the vector; subscript 1 to the y component and subscript 2 to //the z component. Subscript values other than 0, 1, or 2 produce undefined results. For speed, no error //checking needs to be done.
- //The choice of how the data members are implemented in the class could greatly affect the complexity of //overloading this particular operator.
- //Don't forget that this operator needs to be overloaded twice, once for getting a value and once for //setting a value.
- //beginning of Vector3.h file
- #ifndef VECTOR_H
- #define VECTOR_H
- #include <iostream>
- using namespace std;
- class Vector3
- {
- friend ostream & operator<<(ostream &, const Vector3 &);
- friend Vector3 operator*(float, const Vector3 &);
- float coord[3]; //Do I even need this for subscript?
- private:
- float x;
- float y;
- float z;
- public:
- Vector3();
- Vector3(float, float, float);
- Vector3 operator+(const Vector3 &) const;
- Vector3 operator-(const Vector3 &) const;
- float operator*(const Vector3 &) const;
- Vector3 operator*(float rightOp) const;
- float operator[](int index) const; //Read subscript operator
- float & operator [] (int index); //Write subscript operator
- bool operator==(const Vector3 &) const;
- };
- #endif
- //end of Vector3.h File!
- -----------------------------------------------------------------------
- //start of Vector3.cpp file
- #include <iostream>
- #include <iomanip>
- #include "Vector3.h"
- using namespace std;
- Vector3::Vector3()
- {
- x = 0;
- y = 0;
- z = 0;
- }
- Vector3::Vector3( float newX, float newY, float newZ)
- {
- x = newX;
- y = newY;
- z = newZ;
- }
- ostream & operator<<(ostream & osObject, const Vector3 & rightOp)
- {
- osObject << "(" << rightOp.x << ", " << rightOp.y << ", " << rightOp.z << ")";
- return osObject;
- }
- Vector3 Vector3::operator+(const Vector3 & rightOp) const
- {
- Vector3 result = *this;
- result.x += rightOp.x;
- result.y += rightOp.y;
- result.z += rightOp.z;
- return result;
- }
- Vector3 Vector3::operator-(const Vector3 & rightOp) const
- {
- Vector3 result = *this;
- result.x -= rightOp.x;
- result.y -= rightOp.y;
- result.z -= rightOp.z;
- return result;
- }
- float Vector3::operator*(const Vector3 & rightOp) const
- {
- float result = (x * rightOp.x) + (y * rightOp.y) + (z * rightOp.z);
- return result;
- }
- Vector3 Vector3::operator*(float rightOp) const
- {
- Vector3 result = *this;
- result.x *= rightOp;
- result.y *= rightOp;
- result.z *= rightOp;
- return result;
- }
- Vector3 operator*(float leftOp, const Vector3 & rightOp)
- {
- Vector3 result = rightOp;
- result.x *= leftOp;
- result.y *= leftOp;
- result.z *= leftOp;
- return result;
- }
- bool Vector3::operator==(const Vector3 & rightOp) const
- {
- return (x == rightOp.x && y == rightOp.y && z == rightOp.z);
- }
- float Vector3::operator [] (int index) const //This is to read subscript
- {
- return coord[index];
- }
- float & Vector3::operator [] (int index) //this is to write to subscript
- {
- return coord[index];
- }
- //end of Vector3.cpp file!
- ----------------------------------------------------------------------------------
- //File to test the coding
- #include <iostream>
- #include "Vector3.h"
- using std::cout;
- using std::endl;
- int main()
- {
- int test = 1;
- cout << "\nTest " << test++ << ": Constructor and printing\n" << endl;
- const Vector3 v1, v2(1.0, 2.0, 3.0);
- cout << "v1: " << v1 << endl;
- cout << "v2: " << v2 << endl;
- cout << "\nTest " << test++ << ": Addition and subtraction\n" << endl;
- const Vector3 v3(1.0, 2.0, 3.0), v4(-2.0, 3.0, -1.0);
- cout << "v3: " << v3 << endl;
- cout << "v4: " << v4 << endl << endl;
- cout << "v3 + v4 is " << v3 + v4 << endl;
- cout << "v3 - v3 is " << v3 - v4 << endl;
- cout << "\nTest " << test++ << ": Vector multiplication\n" << endl;
- cout << "The scalar product of " << v3 << " and " << v4 << " is ";
- cout << v3 * v4 << endl;
- cout << "\nTest " << test++ << ": Scalar multiplication\n" << endl;
- float k = 2.345;
- cout << v3 << " * " << k << " = " << v3 * k << endl;
- cout << k << " * " << v4 << " = " << k * v4 << endl;
- cout << "\nTest " << test++ << ": Subscripting\n" << endl; //TEST I HAVE PROBLEMS WITH
- const Vector3 v5(3.2, -5.4, 5.6);
- Vector3 v6(1.3, 2.4, -3.1);
- cout << "v5: " << v5 << endl;
- cout << "v6: " << v6 << endl;
- cout << "v5[0] = " << v5[0] << endl;
- cout << "v5[1] = " << v5[1] << endl;
- cout << "v5[2] = " << v5[2] << endl;
- v6[0] = -2.4;
- v6[1] = -1.3;
- v6[2] = 17.5;
- cout << "v6: " << v6 << endl;
- v6 = v5;
- cout << "v6: " << v6 << endl;
- cout << "\nTest " << test++ << ": Equality\n" << endl;
- const Vector3 v7(-1, 2, -1), v8(-1, 2, -2);
- cout << v7 << " and " << v7 << " are ";
- if (v7 == v7)
- cout << "equal" << endl;
- else
- cout << "not equal" << endl;
- cout << v7 << " and " << v8 << " are ";
- if (v7==v8)
- cout << "equal" << endl;
- else
- cout << "not equal" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement