Advertisement
SeZomg

Help with subscripting operator overload?

Mar 1st, 2012
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.40 KB | None | 0 0
  1. //What the operator[] should do:
  2. //operator[]
  3.  
  4. //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.
  5.  
  6. //The choice of how the data members are implemented in the class could greatly affect the complexity of //overloading this particular operator.
  7.  
  8. //Don't forget that this operator needs to be overloaded twice, once for getting a value and once for //setting a value.
  9.  
  10.  
  11. //beginning of Vector3.h file
  12. #ifndef VECTOR_H
  13. #define VECTOR_H
  14.  
  15. #include <iostream>
  16.  
  17. using namespace std;
  18.  
  19. class Vector3
  20.         {
  21.         friend ostream & operator<<(ostream &, const Vector3 &);
  22.         friend Vector3 operator*(float, const Vector3 &);
  23.  
  24.         float coord[3];                         //Do I even need this for subscript?
  25.  
  26.         private:
  27.  
  28.                 float x;                                        
  29.                 float y;                                        
  30.                 float z;                                        
  31.  
  32.         public:
  33.  
  34.                 Vector3();
  35.                 Vector3(float, float, float);
  36.                 Vector3 operator+(const Vector3 &) const;
  37.                 Vector3 operator-(const Vector3 &) const;
  38.                 float operator*(const Vector3 &) const;
  39.                 Vector3 operator*(float rightOp) const;
  40.                 float operator[](int index) const;                       //Read subscript operator
  41.                 float & operator [] (int index);            //Write subscript operator
  42.                 bool operator==(const Vector3 &) const;
  43.         };
  44.  
  45. #endif
  46. //end of Vector3.h File!
  47.  
  48. -----------------------------------------------------------------------
  49.  
  50. //start of Vector3.cpp file
  51.  
  52.  
  53. #include <iostream>
  54. #include <iomanip>
  55. #include "Vector3.h"
  56.  
  57. using namespace std;
  58.  
  59. Vector3::Vector3()
  60.         {
  61.                 x = 0;
  62.                 y = 0;
  63.                 z = 0;
  64.         }
  65.  
  66. Vector3::Vector3( float newX, float newY, float newZ)
  67.         {
  68.                 x = newX;
  69.                 y = newY;
  70.                 z = newZ;
  71.         }
  72.  
  73. ostream & operator<<(ostream & osObject, const Vector3 & rightOp)
  74.         {
  75.         osObject << "(" << rightOp.x << ", " << rightOp.y << ", " << rightOp.z << ")";
  76.  
  77.         return osObject;
  78.         }
  79.  
  80. Vector3 Vector3::operator+(const Vector3 & rightOp) const
  81.         {
  82.                 Vector3 result = *this;
  83.  
  84.                 result.x += rightOp.x;
  85.                 result.y += rightOp.y;
  86.                 result.z += rightOp.z;
  87.  
  88.         return result;
  89.         }
  90.  
  91. Vector3 Vector3::operator-(const Vector3 & rightOp) const
  92.         {
  93.                 Vector3 result = *this;
  94.  
  95.                 result.x -= rightOp.x;
  96.                 result.y -= rightOp.y;
  97.                 result.z -= rightOp.z;
  98.         return result;
  99.         }
  100.  
  101. float Vector3::operator*(const Vector3 & rightOp) const
  102.         {
  103.                 float result = (x * rightOp.x) + (y * rightOp.y) + (z * rightOp.z);
  104.  
  105.                 return result;
  106.         }
  107.  
  108. Vector3 Vector3::operator*(float rightOp) const
  109.         {
  110.                 Vector3 result = *this;
  111.  
  112.                 result.x *= rightOp;
  113.                 result.y *= rightOp;
  114.                 result.z *= rightOp;
  115.  
  116.         return result;
  117.         }
  118.  
  119. Vector3 operator*(float leftOp, const Vector3 & rightOp)
  120.         {
  121.                 Vector3 result = rightOp;
  122.  
  123.                 result.x *= leftOp;
  124.                 result.y *= leftOp;
  125.                 result.z *= leftOp;
  126.  
  127.         return result;
  128.         }
  129.  
  130. bool Vector3::operator==(const Vector3 & rightOp) const
  131.         {
  132.         return (x == rightOp.x && y == rightOp.y && z == rightOp.z);
  133.         }
  134.  
  135.  
  136. float Vector3::operator [] (int index) const                         //This is to read subscript
  137.         {
  138.         return coord[index];
  139.         }
  140.  
  141.  
  142. float & Vector3::operator [] (int index)            //this is to write to subscript
  143.         {
  144.         return coord[index];
  145.         }
  146.  
  147. //end of Vector3.cpp file!
  148.  
  149. ----------------------------------------------------------------------------------
  150. //File to test the coding
  151.  
  152. #include <iostream>
  153. #include "Vector3.h"
  154.  
  155. using std::cout;
  156. using std::endl;
  157.  
  158. int main()
  159.         {
  160.         int test = 1;
  161.  
  162.         cout << "\nTest " << test++ << ": Constructor and printing\n" << endl;
  163.  
  164.         const Vector3 v1, v2(1.0, 2.0, 3.0);
  165.  
  166.         cout << "v1: " << v1 << endl;
  167.         cout << "v2: " << v2 << endl;
  168.  
  169.         cout << "\nTest " << test++ << ": Addition and subtraction\n" << endl;
  170.  
  171.         const Vector3 v3(1.0, 2.0, 3.0), v4(-2.0, 3.0, -1.0);
  172.  
  173.         cout << "v3: " << v3 << endl;
  174.         cout << "v4: " << v4 << endl << endl;
  175.  
  176.         cout << "v3 + v4 is " << v3 + v4 << endl;
  177.         cout << "v3 - v3 is " << v3 - v4 << endl;
  178.  
  179.         cout << "\nTest " << test++ << ": Vector multiplication\n" << endl;
  180.  
  181.         cout << "The scalar product of " << v3 << " and " << v4 << " is ";
  182.         cout << v3 * v4 << endl;
  183.  
  184.         cout << "\nTest " << test++ << ": Scalar multiplication\n" << endl;
  185.  
  186.         float k = 2.345;
  187.  
  188.         cout << v3 << " * " << k << " = " << v3 * k << endl;
  189.         cout << k << " * " << v4 << " = " << k * v4 << endl;
  190.  
  191.         cout << "\nTest " << test++ << ": Subscripting\n" << endl;              //TEST I HAVE PROBLEMS WITH
  192.  
  193.         const Vector3 v5(3.2, -5.4, 5.6);
  194.         Vector3 v6(1.3, 2.4, -3.1);
  195.  
  196.         cout << "v5: " << v5 << endl;
  197.         cout << "v6: " << v6 << endl;
  198.  
  199.         cout << "v5[0] = " << v5[0] << endl;
  200.         cout << "v5[1] = " << v5[1] << endl;
  201.         cout << "v5[2] = " << v5[2] << endl;
  202.  
  203.         v6[0] = -2.4;
  204.         v6[1] = -1.3;
  205.         v6[2] = 17.5;
  206.  
  207.         cout << "v6: " << v6 << endl;
  208.         v6 = v5;
  209.         cout << "v6: " << v6 << endl;
  210.  
  211.         cout << "\nTest " << test++ << ": Equality\n" << endl;
  212.  
  213.         const Vector3 v7(-1, 2, -1), v8(-1, 2, -2);
  214.  
  215.         cout << v7 << " and " << v7 << " are ";
  216.  
  217.         if (v7 == v7)
  218.                 cout << "equal" << endl;
  219.         else
  220.                 cout << "not equal" << endl;
  221.  
  222.         cout << v7 << " and " << v8 << " are ";
  223.  
  224.         if (v7==v8)
  225.                 cout << "equal" << endl;
  226.         else
  227.                 cout << "not equal" << endl;
  228.  
  229.         return 0;
  230.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement