运算符重载概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型。
1 加号运算符重载
作用:实现两个自定义数据类型相加的运算
1.1 利用成员函数建立“+号”的重载
#include<iostream>
using namespace std;
//加号运算符重载
class person
{
public:
//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;
}
int m_a;
int m_b;
};
void test01()
{
person p1;
p1.m_a = 10;
p1.m_b = 10;
person p2;
p2.m_a = 20;
p2.m_b = 20;
person p3 = p1 + p2;//需要运算符重载才能正确
cout << "p3.m_a=" << p3.m_a << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
成员函数重载本质 person p3 = p1 + p2 相当于person p3 = p1.operator + (p2)
1.2 利用全局函数建立“+号”的重载
#include<iostream>
using namespace std;
//加号运算符重载
class person
{
public:
int m_a;
int m_b;
};
//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 test01()
{
person p1;
p1.m_a = 10;
p1.m_b = 10;
person p2;
p2.m_a = 20;
p2.m_b = 20;
person p3 = p1 + p2;//需要运算符重载才能正确
cout << "p3.m_a=" << p3.m_a << endl;
cout << "p3.m_b=" << p3.m_b << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
全局函数重载本质 person p3 = p1 + p2 相当于person p3 = operator + (p1,p2)
1.3 运算符重载,也可以发生函数重载。
#include<iostream>
using namespace std;
//加号运算符重载
class person
{
public:
int m_a;
int m_b;
};
//函数重载的版本
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 = 20;
p2.m_b = 20;
person p3 = p1 + 10;//person + int
cout << "p3.m_a=" << p3.m_a << endl;
cout << "p3.m_b=" << p3.m_b << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
1.4总结
1.对于内置的数据类型的表达式的运算符是不可能发生改变的。
2.不要滥用运算符重载。
2 左移运算符的重载
#include<iostream>
using namespace std;
//左移运算符的重载(一般只能利用全局函数实现)
class person
{
public:
int m_a;
int m_b;
};
//只能利用全局函数重载左移运算符
ostream & operator<<(ostream &cout,person &p)//本质 operator<<(cout,p) 化简为cout<<p
{
cout << "m_a=" << p.m_a << endl << "m_b=" << p.m_b << endl;
return cout;//利用链式编程思想返回的
}
void test01()
{
person p;
p.m_a = 10;
p.m_b = 20;
cout << p <<endl;
}
int main()
{
test01();
system("pause");
return 0;
}
总结:重载左移运算符配合友元可以实现输出自定义数据类型
3 递增运算符的重载
作用:通过重载递增运算符,实现自己的整型数据
#include<iostream>
using namespace std;
//递增运算符重载
class MYint
{
friend ostream& operator<<(ostream& cout, MYint m);//利用友元的关键字可以访问私有内容
public:
MYint()
{
m_a = 0;
}
//重载前置++运算符 返回引用是为了一直对一个数据进行递增操作
MYint& operator++()
{
//先进行m_a++的运算
m_a++;
//再将自身做一个返回
return *this;
}
//重载后置++运算符
//int 代表占位参数可以用于区分前置和后置递增
MYint operator++(int)
{
//先 记录当时的结果
MYint temp = *this;
//后 递增
m_a++;
//最后将记录的结果返回
return temp;
}
private:
int m_a;
};
//重载“<<”左移运算符
ostream& operator<<(ostream& cout, MYint m)
{
cout << m.m_a;
return cout;
}
void test01()
{
MYint myint;
cout << ++myint << endl;
cout << ++(++myint) << endl;
cout << myint << endl;
}
void test02()
{
MYint myint;
cout <<myint++ << endl;
cout <<myint << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
总结:前置递增返回引用,后置递增返回值。
4 赋值运算符的重载
编译器默认提供浅拷贝操作,如果在堆区创建一些属性,就会出现析构函数在堆区重复释放的问题,所以此时需要自己写一个赋值运算符重载。1.先判断堆区是否存在内存,如果有先释放干净。2.再深拷贝。3.返回对象本身。
#include<iostream>
using namespace std;
//递增运算符重载
class person
{
public:
person(int age)
{
m_age = new int(age);//把这个数据开辟到堆区
}
//在析构函数中释放堆区的内存
~person()
{
if (m_age != NULL)
{
delete m_age;
m_age = NULL;
}
}
person& operator=(person& p)
{
//编译器是提供浅拷贝:m_age=p.m_age
//应该先判断是否有属性在堆区,如果有先释放干净,然后再深拷贝
if (m_age != NULL)
{
delete m_age;
m_age = NULL;
}
//深拷贝
m_age = new int(*p.m_age);
//返回对象本身
return *this;
}
int *m_age;
};
void test01()
{
person p1(18);
person p2(22);
person p3(24);
p3 = p2 = p1;//做赋值运算
cout << "p1的年龄为:" <<*p1.m_age<< endl;
cout << "p2的年龄为:" << *p2.m_age << endl;
cout << "p3的年龄为:" << *p3.m_age << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
5 关系运算符的重载
重载关系运算符,可以让两个自定义类型对象进行对比操作。
#include<iostream>
using namespace std;
//关系运算符重载
class person
{
public:
person(string name, int age)
{
m_name = name;
m_age = age;
}
//重载“==”号
bool operator==(person &p)
{
if ((this->m_name == p.m_name) && (this->m_age == p.m_age))
{
return true;
}
else
{
return false;
}
}
//重载“!=”号
bool operator!=(person& p)
{
if ((this->m_name != p.m_name) || (this->m_age != p.m_age))
{
return true;
}
else
{
return false;
}
}
string m_name;
int m_age;
};
void test01()
{
person p1("Tom", 18);
person p2("Jack", 18);
if (p1 == p2)
{
cout << "p1与p2相等" << endl;
}
else
{
cout << "p1与p2不相等" << endl;
}
if (p1 != p2)
{
cout << "p1与p2不相等" << endl;
}
else
{
cout << "p1与p2相等" << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
6 函数调用运算符重载
1.函数调用运算符()也 可以发生重载。
2.由于重载后使用的方式非常像函数的调用,因此被称为——仿函数。
3.仿函数没有固定写法因此非常灵活。
#include<iostream>
using namespace std;
#include<string>
//函数调用运算符重载
//打印输出类
class Myprint
{
public:
//重载函数调用运算符
void operator()(string test)
{
cout << test << endl;
}
};
void test01()
{
Myprint printf;
printf("hellow cjm");//由于使用起来非常像函数调用,因此被称为仿函数
}
//仿函数非常灵活,没有固定的写法
//例如加法类
class Add
{
public:
int operator()(int a,int b)
{
return a + b;
}
};
void test02()
{
Add add1;
add1(10, 20);
int ret=add1(10, 20);
cout << "ret= " << ret << endl;
//匿名函数对象
cout << Add()(10, 20) << endl;//类型+()会创建出一个匿名对象。匿名对象特点:当前行执行完后立即被释放
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}