- 运算符重载
对于基本数据类型,可以直接运算,但是类不能直接参与运算,
没有对运算符进行重载
【1】运算符重载函数名格式
返回值 operator运算符(参数)
{
//函数体
}
【2】运算符重载的目的
让自己定义的类也能直接参与运算
运算符重载的要求:
- 不能创造运算符,必须对已有的运算符重载
- 不能更改运算符本身的功能,+运算重载后实现乘法运算
【3】运算符重载函数的格式
- 成员函数的格式:给哪个类重载运算符,就把重载函数定义为哪个类的成员函数
- 全局函数的格式:需要在类内声明全局函数为友元函数
成员函数版的运算符重载一定比全局函数版的运算符重载少一个参数,成员函数本身提供了一个类对象
【4】算数运算符的重载
算术运算都是双目运算:
格式:L # R ---->需要两个类对象作为参数
结果:右值
参数:运算过程中不需要修改操作数,可以定义为const
【5】赋值运算符的重载
operator= :拷贝赋值函数
+=、-=···运算符的重载
格式:L # R
结果:对左操作数的修改,是一个左值
参数:左操作数运算过程中可以修改,右操作数不能修改
【6】条件运算符的重载
< >·····
格式:L # R
结果:bool类型的真值或者假值
参数:运算过程中不需要修改操作数,可以定义为const
bool operator>(Complex &c1,Complex &c2)
{}
【7】()运算符的重载
- 对()运算符,调用函数的性质重载
- 对()强转的性质重载 float a; int(a);
格式:operator 数据类型(){} ---->因为强转类型已经明确了返回值类型
【8】自增自减运算符的重载
a++、 ++a、
前自增:
成员函数:Complex &operator++(){}
全局函数:Complex &operator++(Complex &c1){}
后自增: ----->需要使用哑元和前自增区分
成员函数:Complex &operator++(int){}
全局函数:Complex &operator++(Complex &c1,int){}
【9】插入/提取运算符的重载***
(输入输出运算符)
int a; cout << a<< endl;
返回值 operator<<(){} 成员函数版的提取运算符
c4.operator<<(cout) 调用格式,不符合日常使用
cout << c4;
cout<<本身有一个可以级联输出的性质,重载后也要求能实现这个性质
cout << a << b; ---> operator<<(cout,a) << b ----->说明函数的返回值是cout
#include <iostream>using namespace std;class Complex
{int real;int vir;
public:Complex(){}Complex(int real,int vir):real(real),vir(vir){}//输出类对象的成员void show(){
cout << real << "+" << vir << "i" << endl;}friend Complex operator-(const Complex &c1,const Complex &c2);//成员函数版的+运算符重载
Complex operator+(const Complex &other){
Complex temp;
temp.real = this->real+other.real;
temp.vir = this->vir+other.vir;return temp;}//成员函数版的混合赋值运算符重载
Complex operator+=(const Complex &c1){this->real = this->real+c1.real;this->vir = this->vir+c1.vir;return *this;}friend bool operator==(const Complex &c1,const Complex &c2);//使用成员函数,实现强转功能的重载 int()//强转功能重载的固定格式,只有成员函数版的重载函数//operator 类型()operator int(){return this->real;}//重载()运算符,调用函数功能的()//实现了一个伪函数void operator()(){
cout << 123 << endl;}void operator()(string name){
cout << name << endl;}friend Complex &operator--(Complex &c1,int);//成员函数版的前自增
Complex &operator++(){++this->real;++this->vir;return *this;}friend ostream &operator<<(ostream &out,Complex &c1);
};//全局函数版实现后自减运算符的重载
Complex &operator--(Complex &c1,int)
{static Complex temp = c1; //使用temp获取自减前的值
c1.real--;
c1.vir--;return temp; //返回自减前的值
}
//全局函数版-运算符重载
Complex operator-(const Complex &c1,const Complex &c2)
{
Complex temp;
temp.real=c1.real-c2.real;
temp.vir=c1.vir-c2.vir;return temp;
}
//全局函数版的==运算符重载
bool operator==(const Complex &c1,const Complex &c2)
{return c1.real==c2.real&&c1.vir==c2.vir;
}//全局函数实现提取运算符的重载
ostream &operator<<(ostream &out,Complex &c1)
{
out << c1.real << "+" << c1.vir << "i" << endl;return out;
}
int main()
{
Complex c1(3,5);
Complex c2(1,4);
c1.show();
c2.show();
Complex c3 = c1+c2;/*c3.show();
c2+=c1; //左调右参,c2调用了混合运算符重载,参数是c1
c2.show();
c2();
c2("zhangsan");
*/
Complex c4 = c3--; cout << c4 << c3 << 3 << endl;//cout << c4 --->operator<<(cout,c4)--->返回值cout//operator<<(cout,c4)<<c3//operator<<(operator<<(cout,c4),c3) << 3//ostream &operator<<(ostream &out,Complex &c1){}int a=9,b =3;//cout << a << b; //先输出a再输出b //operator<<(cout,a)<<b//operator<<(cout,a) ---->会返回一个cout继续输出//cout是ostream类的类对象,自己不能不能定义,但是可以通过引用传入函数再返回//cout应该是ostream类的类对象return 0;
}插入运算符重载
//全局函数实现插入运算符的重载
istream &operator>>(istream &in,Complex &c1)
{
in >> c1.real >> c1.vir;return in;
}
【10】不能重载的运算符
- sizeof()
- 成员访问运算符.
- 指针访问运算符 * ---->对指针访问
- :: 域限定符
- a?a:b