重载<<.>>(friend必须吗?)

 

#include <iostream>
using namespace std;

class position{
public:
    position(
int m,int n);
    friend ostream 
& operator <<(ostream & out,const  position & s);
    friend istream 
& operator >>(istream & in,position & s);
    
int x;
    
int y;
}
;
void main()
{
    position m(
3,4);
    cout
<<m<<endl;

 cout
<<"输入x,y\n";
    cin
>>m;
    cout
<<m<<endl;
}

position ::position(
int m,int n) :x(m),y(n){}
ostream 
& operator<< (ostream & out,const position & s)
{
    
out<<"(" << s.x<<",";
    
out<<s.y << ")\n";
    
return out;
}

istream 
& operator>> (istream & in,position & s)
{
    
in>>s.x;
    
in>>s.y;
    
return in;
}


posted on 2007-10-28 17:56  cutepig  阅读(343)  评论(0编辑  收藏  举报

导航