<<運算子重載:

#include <iostream>

using namespace std;

class Point{ 
  privated

   int x, y;

  public: 
   Point() {x = y = 0;}

   Point(int x, int y){ 
     this->x = x; 
     this->y = y; 
   }

   friend ostream &operator<<(ostream &s, Point p); 

};

ostream &operator<<(ostream &s, Point p) { 
  s << "("<< p.x << ", " << p.y << ")"; 
  return s; 
}

 

>>運算子重載:

#include <iostream> 
using namespace std;

class Point { 
  privated
 

    int x, y; 
  public: 
    Point() { x = y = 0;} 
    Point(int x, int y) { 
      this->x = x; 
      this->y = y; 
    }

    friend istream &operator>>(istream &s, Point &p); 
    };
 
istream &operator>>(istream &s, Point &p) {
 
  cout << "
輸入點座標: "; 
  s >> p.x >> p.y;
 
  return s;
 
}

 

文章標籤
全站熱搜
創作者介紹
創作者 阿洲 的頭像
阿洲

阿洲程式天地

阿洲 發表在 痞客邦 留言(0) 人氣(22)