海贼班 胡船长第一次直播笔记
我个人觉得讲到的点都讲得挺清楚的
运算符重载 工程开发规范
# include <iostream>
//using namespace std; //不符合工程开发规范,因为引入了太多符号 可以这样用:
using std::count;
using std::endl;
using std::ostream;
class Point{
public:
//Point(int x, int y): x(x), y(y){} 构造函数
Point(int x, int y);
friend ostream &operator << (ostream &, const Point &); //设置为友元,下面就可以访问了
// int x,y; //成员属性 成员属性不能直接暴露在外边 设置为私有
private:
int x,y;
};
// in order to obey 工程开发规范: 1.类的声明和定义应该区分开 2. 成员属性不能直接暴露在外边
Point ::Point(int x, int y): x(x), y(y){} // 构造函数 应该放在外边
// correct first error: operator overloading(运算符重载), 重载了左移运算符
ostream &operator << (ostream &out, const Point &p){ // &operator 返回了左值引用 第二个问题:为什么要用const
out << "(" << p.x << "," <<p.y <<")" ; //设置为私有的成员变量之后,这里就不能调用了,所以需要友元—friend