Maplewing

10/24 C++: Point2D

Oct 23rd, 2015
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Point2D{
  5.     private:
  6.         // 資料 (變數部分)
  7.         double x;
  8.         double y;
  9.        
  10.     public:
  11.        
  12.         friend void OutputPoint2D(Point2D v);
  13.        
  14.         // 操作 (函式部分)
  15.         Point2D(){
  16.             x = 0;
  17.             y = 0;
  18.         }
  19.        
  20.         Point2D(double a, double b){
  21.             x = a;
  22.             y = b;
  23.         }
  24.        
  25.         double getX(){
  26.             return x;
  27.         }
  28.        
  29.         double getY(){
  30.             return y;
  31.         }
  32.  
  33.         Point2D operator*(Point2D b){
  34.             double i = x * b.x;
  35.             double j = y * b.y;
  36.             Point2D c( i, j );
  37.             return c;
  38.         }
  39.        
  40. };
  41.  
  42. void OutputPoint2D(Point2D v){
  43.     cout << "(" << v.x << "," << v.y << ")" << endl;
  44. }
  45.  
  46.  
  47.  
  48. int main(){
  49.     int x, y;
  50.    
  51.     cin >> x >> y;
  52.     Point2D p1(x, y);
  53.     cin >> x >> y;
  54.     Point2D p2(x, y);
  55.  
  56.     Point2D p3;
  57.     p3 = p1 * p2;
  58.     OutputPoint2D( p3 );
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment