Maplewing

5/29 C++: class Point2D

May 28th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. /* Point2D.h */
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. class Point2D{
  6.     private:
  7.         // 資料(變數部分)
  8.         int _x; // 加底線 避免名稱上衝突
  9.         int _y;
  10.        
  11.     public:
  12.         // 方法;功能(函式部分)
  13.         friend void OutputPoint2D(Point2D p);
  14.        
  15.         Point2D(){
  16.             _x = 0;
  17.             _y = 0;
  18.         }
  19.        
  20.         Point2D(int x, int y){
  21.             _x = x;
  22.             _y = y;
  23.         }
  24.        
  25.         Point2D operator*(Point2D v2){
  26.             int newX = _x * v2._x;
  27.             int newY = _y * v2._y;
  28.            
  29.             Point2D v3(newX, newY);
  30.             return v3;
  31.         }
  32.        
  33. };
  34.  
  35. void OutputPoint2D(Point2D v){
  36.     cout << "(" << v._x << "," << v._y << ")" << endl;
  37. }
  38.  
  39. /* Point2D.h */
  40.  
  41. int main(){
  42.     int x, y;
  43.  
  44.     cin >> x >> y;
  45.     Point2D p1(x, y);
  46.     cin >> x >> y;
  47.     Point2D p2(x, y);
  48.  
  49.     Point2D p3;
  50.     p3 = p1 * p2;
  51.     OutputPoint2D( p3 );
  52.     return 0;
  53. }
Add Comment
Please, Sign In to add comment