菜鸟的博客

纵有疾风起,人生不言弃。

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

2023.4.21

复制代码
 1 #include <iostream>
 2 using namespace std;
 3 //通过成员函数重载+号
 4 class Person
 5 {
 6 public:
 7     Person operator+(Person &p)
 8     {
 9         Person temp;
10         temp.m_A = this->m_A + p.m_A;
11         temp.m_B = this->m_B + p.m_B
12         return p;
13     }
14 public:
15     int m_A;
16     int m_B;
17 };
18 int main()
19 {
20     Person p1;
21     Person p2;
22     Person p3;
23     p1.m_A = 10;
24     p1.m_B = 20;
25     p2.m_A = 30;
26     p2.m_B = 40;
27     p3 = p1 + p2;
28     cout<<p3.m_A<<endl;
29     cout<<p3.m_B<<endl;
30     return 0;
31 }
复制代码
复制代码
 1 #include <iostream>
 2 using namespace std;
 3 //通过全局函数重载+号
 4 class Person
 5 {
 6 public:
 7     int m_A;
 8     int m_B;
 9 };
10 Person operator+(Person &p1,Person &p2)
11     {
12         Person temp;
13         temp.m_A = p1.m_A + p2.m_A;
14         temp.m_B = p1.m_B + p2.m_B;
15         return temp;
16     }
17 int main()
18 {
19     Person p1;
20     Person p2;
21     Person p3;
22     p1.m_A = 10;
23     p1.m_B = 20;
24     p2.m_A = 30;
25     p2.m_B = 40;
26     p3 = p1 + p2;
27     cout<<p3.m_A<<endl;
28     cout<<p3.m_B<<endl;
29     return 0;
30 }
复制代码
复制代码
 1 #include <iostream>
 2 using namespace std;
 3 //只能利用全局函数重载左翼运算符
 4 class Person
 5 {
 6 public:
 7     int m_A;
 8     int m_B;
 9 };
10 ostream &operator<<(ostream &cout,Person &p)
11 {
12     cout<<"m_A = "<<p.m_A<<"m_B = "<<p.m_B;
13     return cout;
14 }
15 void test01()
16 {
17     Person p;
18     p.m_A = 10;
19     p.m_B = 20;
20     cout << p << endl;
21 }
22 int main()
23 {
24     test01();
25     return 0;
26 }
复制代码

 

posted on   hhmzd233  阅读(15)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示