![]() |
#2
czsbc2011-10-12 22:36
|

#include <iostream>
using namespace std;
class Point;
ostream& operator<<(ostream& o, const Point& d);
class Point {
int x , y ;
public :
void set (int a ,int b) { x = a ,y = b ; }
Point operator+(const Point& d ) ; //here ,为什么要用Point operator+(const Point&d)而不是operator+(const Point& d) ?
friend ostream& operator<<(ostream& o, const Point& d) ;
} ;
Point Point::operator+(const Point &d ) //这两个Point 各自什么意思?可以给讲讲吗?为什么用两个Point ?
{
Point s ;
s.set (x+d.x,y+d.y);
return s ;
}
ostream& operator<<(ostream& o, const Point& d) {
return o<<"("<<d.x<<", "<<d.y<<")\n" ;
}
void main()
{
Point s,t ;
s.set(2,5);
t.set(3,1);
cout<<s+t ;
}
using namespace std;
class Point;
ostream& operator<<(ostream& o, const Point& d);
class Point {
int x , y ;
public :
void set (int a ,int b) { x = a ,y = b ; }
Point operator+(const Point& d ) ; //here ,为什么要用Point operator+(const Point&d)而不是operator+(const Point& d) ?
friend ostream& operator<<(ostream& o, const Point& d) ;
} ;
Point Point::operator+(const Point &d ) //这两个Point 各自什么意思?可以给讲讲吗?为什么用两个Point ?
{
Point s ;
s.set (x+d.x,y+d.y);
return s ;
}
ostream& operator<<(ostream& o, const Point& d) {
return o<<"("<<d.x<<", "<<d.y<<")\n" ;
}
void main()
{
Point s,t ;
s.set(2,5);
t.set(3,1);
cout<<s+t ;
}