#include <iostream>
using namespace std;
class Vector
{
public:
Vector(int x, int y)
{
this -> x = x;
this -> y = y;
}
private:
int x, y;
friend ostream & operator<<(ostream &, Vector &);
};
ostream & operator<<(ostream & os, Vector & rhs)
{
os << "(" << rhs.x << "," << rhs.y << ")";
return os;
}
int main()
{
Vector v(2, 3);
cout << "v=" << v << endl;
return 0;
}
本文介绍了一个简单的C++程序,定义了一个名为Vector的类,并实现了友元输出运算符来打印该类的对象。通过这个例子,读者可以了解到如何在C++中使用类成员以及如何自定义输出类实例的方式。
4554

被折叠的 条评论
为什么被折叠?



