重载左移运算符(用cout输出类)

版权声明:根号v587版权所有 https://blog.csdn.net/hcmdghv587/article/details/81877116
#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;
}

猜你喜欢

转载自blog.csdn.net/hcmdghv587/article/details/81877116
今日推荐