1 Star 0 Fork 1

llongger/Code_C++

forked from GodOuO/Code_C++ 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
34.OperatorL_relo.cpp 1.02 KB
一键复制 编辑 原始数据 按行查看 历史
GodOuO 提交于 2023-03-15 04:07 . Train
#include<iostream>
#include<string>
using namespace std;
//左移运算符重载
class Person
{
friend ostream & operator<<(ostream &cout,Person &p);
private:
int m_A;
int m_B;
public:
//void operator<<(cout); //不会利用成员函数 重载<<左移运算符
int getA();
int getB();
void setAB(int a,int b);
Person(/* args */);
};
Person::Person(/* args */)
{
}
int Person::getA(){
return m_A;
}
int Person::getB(){
return m_B;
}
void Person::setAB(int a,int b){
m_A = a;
m_B = b;
}
ostream & operator<<(ostream &cout,Person &p){ //全局函数重载<<左移运算符
cout<<"P_a: "<<p.getA()<<endl<<"P_b: "<<p.getB(); //重载输出ab
cout<<endl;
cout<<"P_a: "<<p.m_A<<endl<<"P_b: "<<p.m_B; //Friend友元 支持访问private
return cout; //cout 可以起别名 都支持实现
}
void test01(){
Person p;
p.setAB(10,10);
cout<<p<<endl;
//cout<<"P_a: "<<p.getA()<<endl<<"P_b: "<<p.getB()<<endl; //通用输出ab
}
int main(){
test01();
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/llongger/codeCpp.git
git@gitee.com:llongger/codeCpp.git
llongger
codeCpp
Code_C++
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385