Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. #ifndef VECTOR_H_INCLUDED
  2. #define VECTOR_H_INCLUDED
  3.  
  4. #include <stdarg.h>
  5.  
  6. // brings n variables into array
  7. template <class V>
  8. void vars2array( V &array, int n, ... )
  9. {
  10.     va_list params;
  11.     va_start( params, n );
  12.  
  13.     if( sizeof( array ) / sizeof( V ) > n )
  14.         return;
  15.  
  16.     for( int i = 0; i < n; i++ )
  17.     {
  18.         array[n] = va_arg( params, V );
  19.     }
  20. }
  21.  
  22. // a class to work with vectors
  23. class CVector
  24. {
  25.     public:
  26.         // Variables
  27.         int x, y; // the coordinates
  28.  
  29.         // Constructor
  30.         CVector(){ x = 0; y = 0; }; // used inside of class
  31.         CVector( int, int ); // used for the calcing
  32.  
  33.         // Operator Overloads
  34.         CVector operator + ( CVector );
  35.         CVector operator + ( int[] );
  36.         CVector operator - ( CVector );
  37.         CVector operator - ( int[] );
  38.         CVector operator = ( CVector );
  39.         CVector operator = ( int[] );
  40. };
  41.  
  42. CVector::CVector( int x, int y )
  43. {
  44.     this->x = x;
  45.     this->y = y;
  46. }
  47.  
  48. CVector CVector::operator+ ( CVector param )
  49. {
  50.     CVector temp;
  51.     temp.x = this->x + param.x; // used this for abridgment
  52.     temp.y = this->y + param.y; // s.o.
  53.     return( temp );
  54. }
  55.  
  56. /// @param coord[] - index 0 is x, index 1 is y
  57. CVector CVector::operator+ ( int coord[] )
  58. {
  59.     CVector temp;
  60.     temp.x = this->x + coord[0];
  61.     temp.y = this->y + coord[1];
  62.     return( temp );
  63. }
  64.  
  65. CVector CVector::operator- ( CVector param )
  66. {
  67.     CVector temp;
  68.     temp.x = this->x - param.x; // s.o.
  69.     temp.y = this->y - param.y; // s.o.
  70.     return( temp );
  71. }
  72.  
  73. /// @param coord[] - index 0 is x, index 1 is y
  74. CVector CVector::operator- ( int coord[] )
  75. {
  76.     CVector temp;
  77.     temp.x = this->x - coord[0];
  78.     temp.y = this->y - coord[1];
  79.     return( temp );
  80. }
  81.  
  82. CVector CVector::operator= ( CVector param )
  83. {
  84.     return( param ); // because we want it to be param
  85. }
  86.  
  87. /// @param coord[] - index 0 is x, index 1 is y
  88. CVector CVector::operator= ( int coord[] )
  89. {
  90.     CVector temp( coord[0], coord[1] );
  91.     return( temp );
  92. }
  93.  
  94. #endif // VECTOR_H_INCLUDED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement