<<符号重载
.h文件
#include <iostream>
using namespace std;
class Point {
private :
int x = 0;
int y = 0;
public:
Point();
Point(int, int);
int getX() const;
void setX(int x);
int getY() const;
void setY(int y);
friend ostream & operator<<(ostream &_START,const Point &);
};
.cpp文件
#include "Point.h"
Point::Point() {
}
Point::Point(int x, int y) {
this->x = x;
this->y = y;
}
int Point::getX() const {
return x;
}
void Point::setX(int x) {
Point::x = x;
}
int Point::getY() const {
return y;
}
void Point::setY(int y) {
Point::y = y;
}
ostream & operator<<(ostream &_START, const Point &point) {
_START << "我输出了" << point.getX() << " " << point.getY() << endl;
//此处的<<是系统的符号
return _START;
}
main文件
#include <iostream>
#include "Point.h"
int main() {
cout << point;
//此处<<是重载过得符号
}