运算符重载概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型
加法运算符重载
1.通过成员函数重载“+”
2.通过全局函数重载“+”
#include <iostream>
using namespace std;
class Person
{
public:
int m_a;
int m_b;
Person(int a,int b)
{
this->m_a=a;
this->m_b=b;
}
Person(){}
// 1.通过成员函数重载“+”
Person operator+(Person &p)
{
Person temp;
temp.m_a=this->m_a+p.m_a;
temp.m_b=this->m_b+p.m_b;
return temp;
}
};
// 2.通过全局函数重载“+”
//Person operator+(Person &p1,Person &p2)
//{
// Person temp;
// temp.m_a=p1.m_a+p2.m_a;
// temp.m_b=p1.m_b+p2.m_b;
// return temp;
//}
void func1()
{
Person p1(10,10);
Person p2(10,10);
Person p3=p1+p2;
cout<<p3.m_a<<" "<<p3.m_b<<endl;
}
int main() {
func1();
return 0;
}
- 左移运算符重载
通过全局函数重载“<<”
#include <iostream>
#include <ostream>
using namespace std;
class Person
{
public:
int m_a;
int m_b;
};
ostream& operator<<(ostream &cout,Person p) //返回输出流对象
{
cout<<p.m_a<<" "<<p.m_b;
return cout; //链式编程思想
}
void func()
{
Person p;
p.m_a=10;
p.m_b=10;
cout<<p<<endl;
}
int main()
{
func();
return 0;
}
- 递增运算符重载
1.重置前置++运算符
2.重置后置++运算符
#include <iostream>
#include <ostream>
using namespace std;
class MyInteger
{
friend ostream& operator<<(ostream &cout,MyInteger myInteger);
public:
MyInteger(){m_Num=0;}
MyInteger& operator++() //前置递增重载函数
{
m_Num++;
return *this;
}
MyInteger operator++(int) //int 代表占位参数,用于区分前置和后置
{
MyInteger temp=*this;
m_Num++; //递增后返回原数
return temp; //返回的是局部变量 注意返回值而不是引用!
}
private:
int m_Num;
};
ostream& operator<<(ostream &cout,MyInteger myInteger)
{
cout<<myInteger.m_Num;
return cout;
}
void test01()
{
MyInteger myInteger;
cout<<myInteger<<endl;
cout<<++myInteger<<endl; //前置递增
cout<<myInteger++<<endl; //后置递增
cout<<myInteger<<endl;
}
int main()
{
test01();
return 0;
}
- 赋值运算符重载
#include <iostream>
using namespace std;
class Person
{
public:
int *m_Age;
Person(int age)
{
m_Age=new int(age);
}
~Person()
{
if(m_Age!=NULL)
{
delete m_Age;
m_Age=NULL;
}
}
Person& operator=(Person &p)
{
if(m_Age!=NULL) //判断是否函数堆区内存
{
delete m_Age;
m_Age=NULL;
}
m_Age=new int(*p.m_Age); //深拷贝
return *this; //链式编程
}
};
void test01()
{
Person p1(18);
Person p2(20);
Person p3(22);
p3=p2=p1;
cout<<*p1.m_Age<<endl;
cout<<*p2.m_Age<<endl;
cout<<*p3.m_Age<<endl;
}
int main()
{
test01();
return 0;
}
- 关系运算符重载
重载关系运算符“==”, “>”
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
int m_Age;
string m_Name;
Person(string name,int age)
{
this->m_Age=age;
this->m_Name=name;
}
bool operator==(Person &p)
{
if(this->m_Age==p.m_Age)
return true;
else
return false;
}
bool operator>(Person &p)
{
if(this->m_Age>p.m_Age)
return true;
else
return false;
}
};
void test01()
{
Person p1("A",18);
Person p2("B",18);
Person p3("C",20);
if(p1==p2)
cout<<"same age"<<endl;
else
cout<<"different age"<<endl;
if(p1>p2)
cout<<"bigger age"<<endl;
else
cout<<"small age"<<endl;
}
int main()
{
test01();
return 0;
}
- 函数调用运算符重载
函数调用运算符()也可以重载
由于重载后使用的方式非常像函数的调用,因此称之为仿函数
仿函数没有固定写法,非常灵活
#include <iostream>
#include <string>
using namespace std;
class MyPrint
{
public:
void operator()(string test) //重载函数调用运算符
{
cout<<test<<endl;
}
};
class MyAdd
{
public:
int operator()(int a,int b)
{
return a+b;
}
};
void test01()
{
MyPrint myPrint;
myPrint("USTC_ZS"); //仿函数
}
void test02()
{
MyAdd myAdd;
int result=myAdd(100,200);
cout<<result<<endl;
//匿名函数对象
cout<<MyAdd()(100,100)<<endl;
}
int main()
{
test01();
test02();
return 0;
}