运算符重载定义:对已有运算符重新进行定义,赋予其另一种功能,以适应不同数据类型。
1 加号运算符重载
作用:实现两个自定义数据类型相加运算(类名 operator +())
加号运算符重载:operator +()
(1)成员函数实现加号运算符重载
(2)全局函数实现加号运算符重载
运算符重载也可实现函数重载,重新复用operator+函数,函数名相同,但是参数不同。
#include <iostream>
#include <string>
using namespace std;
//加号运算符重载 函数名为operate +
//1.成员函数重载+号
//2.全局函数重载+号
class Person
{
public:
int m_A ;
int m_B ;
//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;
}
//3.运算符重载也可实现函数重载、
Person operator+ (Person& p1, int a)
{
Person temp;
temp.m_A = p1.m_A +a;
temp.m_B = p1.m_B + a;
return temp;
}
void test01()
{
Person p1;
p1.m_A = 10;
p1.m_B = 10;
Person p2;
p2.m_A = 10;
p2.m_B = 10;
//1.使用运算符重载
Person p3 = p1 + p2;
//对于成员函数实现重载:此句等价于Person p3=p1.operator+(p2)
//对于全局函数实现重载:此句等价于Person p3=operator+(p1,p2)
cout << "p3.m_A=" << p3.m_A << endl;
cout << "p3.m_B=" << p3.m_B << endl;
//3.使用运算符重载的函数重载
Person p4 = p1 + 20;
cout << "p4.m_A=" << p4.m_A << endl;
cout << "p4.m_B=" << p4.m_B << endl;
}
int main()
{
test01();
}
2 左移运算符重载
作用:可以输出自定义(某类对象)数据类型(operator<<)
左移运算符重载:operator<<()
(1)一般不使用成员函数实现左移运算符重载,因为在调用时形式为:对象<<
(2)全局函数实现左移运算符重载
#include <iostream>
#include <string>
using namespace std;
//左移运算符重载 函数名为operate <<
//1.一般不使用成员函数实现左移运算符重载
//2.使用成员函数实现左移运算符重载
class Person
{
public:
int m_A;
int m_B;
//1.成员函数实现重载 左移运算符 p.operator<<(cout)==p<<cout
//通常不会使用成员函数实现左移运算符重载<<
//void operator+(cout)
//{
//
//}
};
//2.全局函数实现重载 左移运算符:cout属于输出流对象,使用引用方式,因为全局只能保证只有一个
ostream & operator<<(ostream &cout, Person &p)
{
cout << "m_A=" << p.m_A << "m_B=" << p.m_B << endl;
return cout;
}
void test01()
{
Person p1;
p1.m_A = 10;
p1.m_B = 10;
cout << "输出自定义数据类型(某类)" << endl; ;
cout << p1 << endl;
}
int main()
{
test01();
}
3 递增运算符重载
作用:通过重载递增运算符,实现自己的整形数据
左移运算符重载:operator<<()
(1)一般不使用成员函数实现左移运算符重载,因为在调用时形式为:对象<<
(2)全局函数实现左移运算符重载
#include <iostream>
#include <string>
using namespace std;
//递增运算符重载 函数名为operator <<
//1.前置递增运算符重载
//2.后置递增运算符重载
class MyInterger
{
friend ostream& operator<<(ostream& cout, MyInterger myint);
public:
MyInterger()
{
m_Num = 0;
}
//1.重载前置++运算符:返回值的引用,是为了对一个数据进行递增操作
MyInterger& operator++()
{
//先递增
m_Num++;
//再返回值
return *this;
}
//2.重载后置++运算符:返回值,是为了避免返回一个局部变量的引用
MyInterger operator++(int)//参数代表占位参数,可以用来区分前置和后置递增
{
//先记录当时结果
MyInterger temp = *this;
//后递增
m_Num++;
//最后将记录做返回
return temp;
}
private:
int m_Num;
};
ostream& operator<<(ostream& cout, MyInterger myint)
{
cout << myint.m_Num;
return cout;
}
void test01()
{
MyInterger myint;
cout << ++myint << endl; ;
}
void test02()
{
MyInterger myint;
cout << myint++ << endl; ;
}
int main()
{
test01();
test02();
}
4 赋值运算符重载
C++编译器至少给一个类添加4个函数:
(1)默认构造函数(无参,函数体为空)
(2)默认析构函数(无参,函数体为空)
(3)默认拷贝构造函数(对属性值进行拷贝)
(4)赋值运算符operator=,对属性进行值拷贝
如果类中有属性指向堆区,做赋值操作时也会出现深拷贝和浅拷贝的问题。
#include <iostream>
#include <string>
using namespace std;
//赋值运算符重载
class Person
{
public:
Person(int age)
{
m_Age = new int(age);//开辟堆区空间:程序员开辟,程序员释放
}
int* m_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;//this为指向自身的指针,*this指自身(对象)
}
};
void test01()
{
Person p1(18);
Person p2(20);
Person p3(33);
p2 = p1=p3;//如果不用重载的=运算符,则会进行浅拷贝,释放堆区数据时会出现多次释放同一数据,程序报错
cout <<"p1的年龄为:" <<*p1.m_Age<< endl;
cout << "p2的年龄为:" << *p2.m_Age << endl;
}
int main()
{
test01();
}
5 关系运算符重载
作用:重载关系运算符,可以让两个自定义类型对象进行对比操作
#include <iostream>
#include <string>
using namespace std;
//关系运算符重载
class Person
{
public:
int m_Age;
string m_Name;
Person(string name,int age)
{
m_Age =age;
m_Name = name;
}
//重载关系运算符
bool operator==(Person& p)
{
if (p.m_Age == m_Age && p.m_Name == m_Name)
{
return true;
}
else
{
return false;
}
}
};
void test01()
{
Person p1("TOM",18);
Person p2("JARRY",20);
if (p1 == p2)
{
cout << "p1和p2是相等的" << endl;
}
else
{
cout << "p1和p2是不相等的" << endl;
}
}
int main()
{
test01();
}
5 函数调用运算符重载
函数调用运算符()也可重载,由于重载后使用方式非常像函数调用,因此称为仿函数。仿函数没有固定的写法,非常灵活。
#include <iostream>
#include <string>
using namespace std;
//函数调用运算符重载
class MyPrint
{
public:
//重载函数调用运算符()
void operator()(string test)
{
cout << test << endl;
}
};
void MyPrint02(string test)
{
cout << test << endl;
}
void test01()
{
MyPrint myPrint;
myPrint("helloword"); //由于使用起来类似于函数调用,因此称为仿函数
MyPrint02("helloword");
}
int main()
{
test01();
}