9_7.cpp
来自「c++书籍的源代码」· C++ 代码 · 共 37 行
CPP
37 行
#include<iostream.h>
#include<iomanip.h>
ostream& pxy(ostream& out);
class point //point 类的定义
{
public: //外部接口
point(int a=0, int b=0);//构造函数
friend ostream& operator <<(ostream& out,const point& p);//声明友元函数
private: //私有数据
int x;
int y;
};
//成员函数的实现
point::point(int a,int b)
{
x=a;
y=b;
}
ostream& operator <<(ostream& out,const point& p)//重载<<运算符
{
out<<"x="<<pxy<<p.x<<"y="<<pxy<<p.y<<endl;//使用操纵符pxy设置格式
return(out);
}
ostream& pxy(ostream& out)//自定义操纵符
{
out.flags(ios::left);
out<<setw(3);
return(out);
}
//主程序
int main()
{
point A(1,2),B(3,4);
cout<<A<<B;
return(0);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?