14-7.cpp
来自「为C++学习者」· C++ 代码 · 共 41 行
CPP
41 行
#include<iostream.h>
#include<fstream.h>
class three_d
{
int x,y,z;
public:
three_d(int i,int j,int k){x=i;y=j;z=k;}
friend ostream& operator<<(ostream& stream,three_d ob);
friend istream& operator>>(istream& stream,three_d ob);
};
ostream& operator<<(ostream& stream,three_d ob)
{
stream<<ob.x<<' '<<ob.y<<' '<<ob.z<<'\n';
return stream;
}
istream& operator>>(istream& stream,three_d ob)
{
stream>>ob.x>>ob.y>>ob.z;
return stream;
}
main()
{
three_d o1(1,2,3),o2(4,5,6);
ofstream out("test");
if(!out)
{
cout<<"Cannot open output file\n";
return 1;
}
cout<<o1<<o2;
out.close();
ifstream in("test");
if(!in)
{
cout<<"Cannot open input file\n";
return 1;
}
three_d o3(0,0,0),o4(0,0,0);
in>>o3>>o4;
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?