C++回顾(十二)—— 运算符重载
迪丽瓦拉
2024-05-30 16:45:47
0

12.1 概念

12.1.1 什么是运算符重载

就是赋予运算符新的意义,比如 << 既可以当作左移运算符,又可以当初输出运算符。

12.1.2 运算符重载入门基础推演

  • 1为什么会用运算符重载机制
    用复数类举例:Complex c3 = c1 + c2;
    原因 Complex是用户自定义类型,编译器根本不知道如何进行加减,编译器给提供了一种机制,让用户自己去完成,自定义类型的加减操作,这个机制就是运算符重载机制
  • 2 运算符重载的本质是一个函数

注意:重载可以发生在类的外边,也可以作为类的的成员函数。

  • (1)在类外边重载
    在这里插入图片描述
    此时记得将该函数变成友元函数,否则由于成员变量权限设置,可以无法在类外访问。
    在这里插入图片描述
    该运算符重载后的应用:
    在这里插入图片描述
  • 在类内作为成员函数重载
    在这里插入图片描述
    运用:
    在这里插入图片描述
    完整示例代码:
#include using namespace std;class Complex
{//friend Complex operator+(const Complex &c1, const Complex &c2);
private:int a;    //实部int b;    //虚部
public:Complex(int _a, int _b){this->a = _a;this->b = _b;}void print(){cout << a << " + " << b << "i" << endl;}Complex operator+(const Complex &c){Complex t(0, 0);t.a = this->a + c.a;t.b = this->b + c.b;return t;}
};//运算符重载本质就是函数的重载
/*Complex operator+(const Complex &c1, const Complex &c2)
{Complex t(0, 0);t.a = c1.a + c2.a;t.b = c1.b + c2.b;return t;
}*/int main()
{Complex c1(1, 2);Complex c2(2, 3);c1.print();//c1 + c2;Complex t(0, 0);//t = operator+(c1, c2);t = c1 + c2;    //编译器会转换成  t = c1.operator+(c2)t.print();return 0;
}

运行结果:
在这里插入图片描述

12.2 运算符重载的限制

在这里插入图片描述
在这里插入图片描述

12.3 运算符重载的基础

12.3.1 运算符重载的两种方法(成员函数 或 友元函数)

(1)二元运算符

在这里插入图片描述

(2)一元运算符

在这里插入图片描述

(3)前置和后置运算符总结

要明白前置++ 和 后置++ 的区别
在这里插入图片描述

++ 运算符的重载:
在这里插入图片描述
在这里插入图片描述
示例代码:

#include using namespace std;class Complex
{friend ostream &operator<<(ostream &out, const Complex &c);
private:int a;    //实部int b;    //虚部
public:Complex(int _a, int _b){this->a = _a;this->b = _b;}//后置++Complex operator++(int)   //通过占位参数来构成函数重载{Complex t = *this;this->a++;this->b++;return t;}//前置++Complex &operator++(){this->a++;this->b++;return *this;}
};ostream &operator<<(ostream &out, const Complex &c)
{out << c.a << " + " << c.b << "i";return out;
}int main()
{Complex c1(1, 2);cout << c1++ << endl;cout << ++c1 << endl;return 0;
}

运行结果:
在这里插入图片描述

12.3.2 定义运算符重载函数名步骤

全局函数、类成员函数方法实现运算符重载步骤
1)要承认操作符重载是一个函数,写出函数名称operator+ ()
2)根据操作数,写出函数参数
3)根据业务,完善函数返回值(看函数是返回引用还是指针 元素),及实现函数业务

12.3.3 友元函数实现操作符重载应用场景

(1)友元函数和成员函数选择方法

  • 当无法修改左操作数的类时,使用全局函数进行重载
  • =, [], ()和->操作符只能通过成员函数进行重载

(2)用友元函数重载<< >>运算符

  • istream 和 ostream 是 C++ 的预定义流类
  • cin 是 istream 的对象,cout 是 ostream 的对象
  • 运算符 << 由ostream 重载为插入操作,用于输出基本类型数据
  • 运算符 >> 由 istream 重载为提取操作,用于输入基本类型数据
  • 用友员函数重载 << 和 >> ,输出和输入用户自定义的数据类型

示例代码:

#include using namespace std;class Complex
{friend ostream &operator<<(ostream &out, const Complex &c);
private:int a;    //实部int b;    //虚部
public:Complex(int _a, int _b){this->a = _a;this->b = _b;}void print(){cout << a << " + " << b << "i" << endl;}/*ostream &operator<<(ostream &out)     //如果左操作数不能修改,则不能重载成成员函数{out << this->a << " + " << b << "i";return out;}*/
};ostream &operator<<(ostream &out, const Complex &c)
{out << c.a << " + " << c.b << "i";return out;
}int main()
{Complex c1(1, 2);c1.print();cout << c1 << endl;    //operator<<(operator<<(cout, c1), endl);   等价于 cout.operator<<(c1)return 0;
}

运行结果:
在这里插入图片描述

相关内容