【C++编程】类与对象 -- 运算符重载:加号、左移运算符、递增运算符、函数调用运算符(仿函数) ...

  • 加号 +Person operator+(Person p);
    #include <iostream>
    using namespace std;
    
    class Person
    {
    public:
    	Person(int a, int b): m_A(a), m_B(b)
    	{
    	}
    	int m_A;
    	int m_B;
    	Person operator+(Person p)  // 通过成员函数重载 +
    	{
    		Person temp(0, 0);
    		temp.m_A = this->m_A + p.m_A;
    		temp.m_B = this->m_B + p.m_B;
    		return temp;
    	};
    };
    
    int main()
    {
    	Person p1(10, 20);
    	Person p2(10, 20);
    	Person p3 = p1 + p2;  // p3.m_A: 20, p3.m_B: 40  
    	cout << p3.m_A << "****" << p3.m_B << endl;       
    }
    
  • 左移运算符 <<ostream & operator<<(ostream &cout, Person p);
    #include <iostream>
    using namespace std;
    
    class Person
    {
    	friend ostream &operator<<(ostream &cout, Person p); // 如果涉及 private 成员
    
    public:
    	Person(int a, int b): m_A(a), m_B(b)
    	{
    	}
    	int m_A;
    	int m_B;
    };
    
    ostream & operator<<(ostream &cout, Person p) // 无法利用成员函数实现
    {
    	cout << " m_A = " << p.m_A << " m_B = " << p.m_B << endl;
    	return cout;
    }
    
    int main()
    {
    	Person p(10, 20);
    	cout << p << endl;  //  m_A = 10 m_B = 20
    }
    
  • 递增运算符 ++Person & operator++(), Person operator++(int)
    #include <iostream>
    using namespace std;
    
    class Person
    {
    	friend ostream &operator<<(ostream &cout, Person p); // 如果涉及 private 成员
    
    public:
    	Person(int a, int b): m_A(a), m_B(b)
    	{
    	}
        Person & operator++()   // 前置递增,返回引用
        {
            this->m_A++;
            return *this;
        }
        Person operator++(int)  // 后置递增,返回值,参数 int 占位
        {
            Person temp = *this;
            this->m_A++;
            return temp;
        }
    
    	int m_A;
    	int m_B;
    };
    
    ostream & operator<<(ostream &cout, Person p)
    {
    	cout << " m_A = " << p.m_A << " m_B = " << p.m_B << endl;
    	return cout;
    }
    
    int main()
    {
    	Person p(10, 20);
    	cout << p << endl;   // m_A = 10 m_B = 20
    	cout << ++p << endl; // m_A = 11 m_B = 20
    	cout << p++ << endl; // m_A = 11 m_B = 20
    }
    
  • 函数调用运算符(): 自选返回类型 operator()(自选形参);
    #include <iostream>
    using namespace std;
    
    class Person
    {
    	friend ostream &operator<<(ostream &cout, Person p); // 如果涉及 private 成员
    
    public:
    	Person(int a, int b): m_A(a), m_B(b)
    	{
    	}
        void operator()()   // 函数调用运算符 -> 仿函数
        {
            cout << "here" << endl;
        }
    	int m_A;
    	int m_B;
    };
    
    
    int main()
    {
    	Person p(10, 20);
    	p();  
    }
    
  • 其他不赘述,仅给出 函数名称和参数返回类型
    • 赋值运算符=Person& operator=(Person p);
    • 关系运算符==bool operator==(Person p);

【黑马程序员匠心之作|C++教程从0到1入门编程,学习编程不再难】

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-14 12:48:04       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-14 12:48:04       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-14 12:48:04       58 阅读
  4. Python语言-面向对象

    2024-07-14 12:48:04       69 阅读

热门阅读

  1. 【LLMs】大语言模型分类

    2024-07-14 12:48:04       25 阅读
  2. 北京工业大学学报

    2024-07-14 12:48:04       25 阅读
  3. FFmpeg学习(五)-- libswresample使用说明及函数介绍

    2024-07-14 12:48:04       23 阅读
  4. 【大学生前端期末作业】顶级茶叶网页

    2024-07-14 12:48:04       16 阅读
  5. Spring @Scheduled学习

    2024-07-14 12:48:04       16 阅读
  6. Xcode自动化测试全景:释放你的应用质量潜能

    2024-07-14 12:48:04       21 阅读
  7. 常用软件的docker compose安装

    2024-07-14 12:48:04       27 阅读
  8. Dubbo 的集群容错机制

    2024-07-14 12:48:04       19 阅读
  9. c++【入门】捡石头

    2024-07-14 12:48:04       20 阅读
  10. 详解 QAxObject

    2024-07-14 12:48:04       12 阅读
  11. windos安装qemu启动树莓派2b连接网卡和openssh-server

    2024-07-14 12:48:04       19 阅读
  12. ARM/Linux嵌入式面经(十五):中科曙光

    2024-07-14 12:48:04       20 阅读
  13. C++ 中的返回值优化

    2024-07-14 12:48:04       20 阅读