Maplewing

4/23 C++: class template (Vector)

Apr 22nd, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. template <typename T>
  7. class Vector{
  8.     private:
  9.         // 資料(變數部分)
  10.         T _x;
  11.         T _y;
  12.          
  13.     public:
  14.         // 操作(函式部分)
  15.        
  16.         Vector(){
  17.             _x = 0;
  18.             _y = 0;
  19.         }
  20.        
  21.         Vector(T x, T y){
  22.             _x = x;
  23.             _y = y;
  24.         }
  25.        
  26.        
  27.         // this指標: 指向自己這個物件
  28.         Vector& setX(T x){
  29.             // (*this)._x = x;
  30.             this->_x = x;
  31.            
  32.             return *this;
  33.         }
  34.        
  35.         Vector& setY(T y){
  36.             _y = y;
  37.             return *this;
  38.         }
  39.        
  40.         T getX() const{
  41.             return _x;
  42.         }
  43.        
  44.         T getY() const{
  45.             return _y;
  46.         }
  47.        
  48.         double length() const{
  49.             return sqrt( _x * _x + _y * _y );
  50.         }
  51.        
  52.         Vector operator+(const Vector &b) const{
  53.             double newX = _x + b._x;
  54.             double newY = _y + b._y;
  55.             Vector c(newX, newY);
  56.             return c;
  57.         }
  58.        
  59.         bool operator<(const Vector &b) const{
  60.             if( length() < b.length() ){
  61.                 return true;
  62.             }
  63.             else if( length() > b.length() ){
  64.                 return false;
  65.             }
  66.             else if( _x < b._x ){
  67.                 return true;
  68.             }
  69.             else if( _x > b._x ){
  70.                 return false;
  71.             }
  72.             else if( _y < b._y ){
  73.                 return true;
  74.             }
  75.             else if( _y > b._y ){
  76.                 return false;
  77.             }
  78.             else{
  79.                 return false;
  80.             }
  81.         }
  82.        
  83.         void print() const{
  84.             cout << "(" << _x << "," << _y << ")" << endl;
  85.         }
  86. };
  87.  
  88. int main(){
  89.    
  90.     /*
  91.     const Vector v(3, 4);
  92.     cout << v.length() << endl;
  93.     */
  94.    
  95.    
  96.     /*
  97.     Vector<double> a;
  98.     Vector<double> b(-1.2, 6.3);
  99.     a.setX(3).setY(4);
  100.    
  101.     Vector<double> answer = a + b;// a.operator+(b);
  102.     answer.print();
  103.     */
  104.    
  105.     Vector<int> array[] = { Vector<int>(3, 4), Vector<int>(4, 5), Vector<int>(1, 2), Vector<int>(3, 5), Vector<int>(1, 3)};
  106.    
  107.     sort(array, array+5);
  108.    
  109.     for( int i = 0 ; i < 5 ; i++ ){
  110.         array[i].print();
  111.     }
  112.    
  113.     return 0;
  114. }
Add Comment
Please, Sign In to add comment