运算符重载概念
:
对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型
实现运算符重载的两种方式
:
注意
:
作用
:实现两个自定义数据类型相加的运算
案例描述:实现两个类对象的加法运算
1.运算符重载的代码演示
:Person operator+(Person &p1,Person &p2)
2.全局函数的本质调用为
:Person p3 = operator+(p1,p2);
-----------------------简化后为:Person p3 = p1 + p2;
3.局部函数的本质调用为
:Person p3 = p1.operator+(p2);
-----------------------简化后为:Person p3 = p1 + p2;
4.运算符重载也可以发生函数重载
:Person operator+(Person &p1,Person &p2);
--------------------------------------------------Person operator+(Person &p,int num);
通过全局函数实现运算符重载
#include
using namespace std;class Person
{
public:Person(int a, int b){this->m_A = a;this->m_B = b;}int m_A;int m_B;
};// 通过全局函数实现运算符重载
Person operator+(Person &p1, Person &p2)
{Person temp(0,0);temp.m_A = p1.m_A + p2.m_A;temp.m_B = p1.m_B + p2.m_B;return temp;
}// 运算符重载也可以发生函数重载
Person operator+(Person& p, int num)
{Person temp(0,0);temp.m_A = p.m_A + num;temp.m_B = p.m_B + num;return temp;
}void test01()
{Person p1(10,10);Person p2(10,10);//全局函数的本质调用为:Person p3 = operator+(p1,p2);//Person p3 = p1 + p2; //简化后 // 运算符重载也可以发生函数重载Person p4 = operator+(p1, 188);cout << "p3.m_A = " << p3.m_A << endl;cout << "p3.m_B = " << p3.m_B << endl;cout << "p4.m_A = " << p4.m_A << endl;cout << "p4.m_B = " << p4.m_B << endl;
}int main() {test01();system("pause");return 0;
}
p3.m_A = 20
p3.m_B = 20
p4.m_A = 198
p4.m_B = 198
请按任意键继续. . .
通过局部函数实现运算符重载
#include
using namespace std;class Person
{
public:Person(int a, int b){this->m_A = a;this->m_B = b;}// 通过成员函数实现运算符重载Person operator+(Person &p){Person temp(0,0);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 test02()
{Person p1(18,20);Person p2(24,8);//成员函数的本质调用为://Person p3 = p1.operator+(p2);//简化后为:Person p3 = p1 + p2;cout << "p3.m_A = " << p3.m_A << endl;cout << "p3.m_B = " << p3.m_B << endl;
}int main() {test02();system("pause");return 0;
}
p3.m_A = 42
p3.m_B = 28
请按任意键继续. . .
作用
:实现两个自定义数据类型相减的运算
掌握了加号运算符重载,减号运算符重载也就很容易实现了。
#include
using namespace std;class Person
{friend Person operator-(Person& p1, Person& p2);friend void test();
public:Person(int a,int b);private:int m_A;int m_B;
};Person::Person(int a,int b)
{this->m_A = a;this->m_B = b;
}Person operator-(Person& p1,Person &p2)
{Person temp(0,0);temp.m_A = p1.m_A - p2.m_A;temp.m_B = p1.m_B - p2.m_B;return temp;
}void test()
{Person p1(-5, 5);Person p2(5, 0);Person p3 = operator-(p1,p2);//Person p3 = p1 - p2;cout << p3.m_A << '\n' << p3.m_B << endl;
}int main()
{test();system("pause");return 0;
}
-10
5
请按任意键继续. . .
局部函数实现
#include
using namespace std;class Person
{
public:Person(int a,int b);Person operator-(Person& p);int m_A;int m_B;
};Person::Person(int a,int b)
{this->m_A = a;this->m_B = b;
}Person Person::operator-(Person& p)
{Person temp(0,0);temp.m_A = this->m_A - p.m_A;temp.m_B = this->m_B - p.m_B;return temp;
}void test()
{Person p1(-5, 5);Person p2(5, 0);//Person p3 = p1.operator-(p2);Person p3 = p1 - p2;cout << p3.m_A << '\n' << p3.m_B << endl;
}int main()
{test();system("pause");return 0;
}
-10
5
请按任意键继续. . .
作用
:输出自定义的数据类型
我们想要直接输出对象(所有的属性也被输出),而不是对象的某个属性。此时就需要重载左移运算符。
案例描述:实现类对象的直接输出,即输出对象所有的属性
1.左移运算符重载的代码演示
:ostream operator<<(ostream &cout,Person &p)
2.全局函数的本质调用为
:operator<<(cout,p);
-----------------------简化后为:cout<3.不能利用成员函数实现左移运算符重载
:使用成员函数实现左移运算符重载本质为:p.operator<<(cout),简化版本为:p<
#include
using namespace std;class Person
{
public:friend ostream& operator<<(ostream &cout, Person &p);Person(int a, int b){this->m_A = a;this->m_B = b;}//利用成员函数重载 左移运算符 本质为:p.operator<<(cout) 简化版本为:p << cout//我们不会利用成员函数重载<<运算符, 因为无法实现 cout在左侧/*void operator<<(ostream &cout){cout << "p.m_A = " << this->m_A << "\tp.m_B = " << this->m_B;}*/private:int m_A;int m_B;
};//只能利用全局函数重载 左移运算符 本质为:operator<<(ostream &cout,Person &p) 简化版本为:cout << p
ostream& operator<<(ostream &cout, Person &p)
{cout << "p.m_A = " << p.m_A << "\tp.m_B = " << p.m_B;return cout;
}void test()
{Person p(10,10);Person p1(20, 21);cout << p << "\t" << p1 << "\nHelloWorld!" << endl;
}int main() {test();system("pause");return 0;
}
p.m_A = 10 p.m_B = 10 p.m_A = 20 p.m_B = 21
HelloWorld!
请按任意键继续. . .
作用
:通过重载递增运算符,实现自己的整型数据累加的操作。
案例描述:实现自定义数据类型对象的累加操作
1.前置递增(++i)
:i先进行+1的操作,再进行表达式的运算;
2.后置递增(i++)
:i先进行表达式的运算,再进行+1的操作;
3.注意事项一
:访问私有成员属性,需要用到友元的技术;想要直接输出对象(对象中的所有成员属性)则需要用到左移运算符重载;
4.注意事项二
:前置++运算符重载函数,返回的必须是引用(即返回对象本身)。如果返回的是值,则会出现Myint=0,cout << ++(++Myint) << endl;cout << Myint << endl;的输出结果为:2 1,原因是返回的是值,则在++Myint后会得到一个新的变量再进行++的操作。返回引用,是为了一直对一个数据进行递增操作
。
5.注意事项三
:当前置++运算符重载函数与后置++运算符重载函数在同一个类中的时候,需要进行一个函数重载操作,此时利用一个占位操作符就可以解决。比如:前置++函数MyInterger& operator++(){};后置++函数MyInterger operator++(int){}
。
6.注意事项四
:后置++运算符函数,返回的必须是值,不能够是引用。因为,创建了一个局部变量,存放在栈区,在函数运行完以后,局部变量的内存将会被释放,再进行引用的话将是一个非法的操作。正是因为如此,后置++运算符重载也不能够进行链式编程。
7.注意事项五
:C++自带的前置++与后置++运算符也是一样,前置++能够进行链式编程运算,但是后置++不能。
前置++运算符重载,返回的必须是引用,能够进行链式编程
#include
using namespace std;class MyInterger
{friend ostream& operator<<(ostream &cout, MyInterger myint);
public:MyInterger(){this->m_A = 0;}//前置++运算符重载 为什么要返回引用,而不是返回值?【要进行区分】 //返回引用,是为了一直对一个数据进行递增操作MyInterger& operator++(){m_A++; //先进性自++运算return *this; //再将自身返回 }
private:int m_A;
};
//左移运算符重载
ostream& operator<<(ostream &cout, MyInterger myint)
{cout << "myint.m_A = " << myint.m_A;return cout;
}void test01()
{MyInterger myint;cout << ++(++myint) << endl;cout << myint << endl;
}int main() {test01();system("pause");return 0;
}
myint.m_A = 3
myint.m_A = 3
请按任意键继续. . .
后置++运算符重载,返回的必须是值,不能够进行链式编程
#include
using namespace std;class MyInterger
{friend ostream& operator<<(ostream &cout, MyInterger myint);
public:MyInterger(){this->m_A = 0;}//后置++运算符重载MyInterger operator++(int){MyInterger temp = *this; //先记录当时的结果m_A++; //后进行递增操作return temp; //最后将记录结果做一个返回}private:int m_A;
};//左移运算符重载
ostream& operator<<(ostream &cout, MyInterger myint)
{cout << "myint.m_A = " << myint.m_A;return cout;
}void test02()
{MyInterger myint1;cout << myint1++ << endl; //输出:0cout << ((myint1++)++)++ << endl; //输出:1cout << myint1 << endl; //输出:2
}int main() {test02();int i = 0;int j = 0;cout << ++(++(++i)) << endl;cout << i << endl;//cout << ((j++)++)++ << endl;//报错cout << j << endl;system("pause");return 0;
}
myint.m_A = 0
myint.m_A = 1
myint.m_A = 2
3
3
0
请按任意键继续. . .
作用
:通过重载递减运算符,实现自己的整型数据递减的操作。
案例描述:实现自定义数据类型对象的递减操作
前置–运算符重载
#include
using namespace std;class MyInterger
{friend ostream& operator<<(ostream& cout, MyInterger myint);
public:MyInterger(){m_A = 0;}//前置递减运算符重载MyInterger& operator--(){m_A--;return *this;}private:int m_A;
};ostream& operator<<(ostream &cout, MyInterger myint)
{cout << "m_A = " << myint.m_A;return cout;
}void test01()
{MyInterger myint;cout << --(--myint) << endl;cout << myint << endl;
}int main() {test01();system("pause");return 0;
}
m_A = -2
m_A = -2
请按任意键继续. . .
后置–运算符重载
#include
using namespace std;class MyInterger
{friend ostream& operator<<(ostream& cout, MyInterger myint);
public:MyInterger(){m_A = 0;}//后置递减运算符重载MyInterger operator--(int){MyInterger temp = *this;m_A--;return temp;}private:int m_A;
};ostream& operator<<(ostream &cout, MyInterger myint)
{cout << "m_A = " << myint.m_A;return cout;
}void test02()
{MyInterger myint;cout << (myint--)-- << endl; cout << myint << endl;
}int main() {test02();system("pause");return 0;
}
m_A = 0
m_A = -1
请按任意键继续. . .
C++编译器至少会给一个类添加4个函数
如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题
案例描述:对赋值运算符进行重载,有两个类对象,我们想把某个类中的所有属性全部赋值给另外一个类对象。
1.深浅拷贝问题
:若类中有属性指向堆区(即有指针变量时),做赋值操作(或者用到拷贝构造函数)
的时候就会出现深浅拷贝问题;
2.赋值运算符重载
:Person& operator=(Person &p){};简化为:p1=p2;
3.注意事项一
:若类中有指针变量,则此时需要开辟数据到堆区(开辟到堆区的数据需要我们手动释放,又由于编译器给我们提供的拷贝构造函数为值(浅)拷贝),于是就容易出现开辟到堆区的内存重复释放的问题。此时就需要进行深拷贝来解决这个问题。
4.注意事项二
:在进行赋值运算符重载函数编写的时候,第一步应该先判断是否开辟了堆区内存,若有则对现有的堆区内存进行一个清理,然后再进行一个深拷贝操作。为了能够进行链式编程,函数的返回类型应该是类的引用,并且返回值为对象本身。
#include
using namespace std;class Person
{
public:Person(int age){m_Age = new int(age); //在堆区开辟一块内存用来存放年龄数据}Person& operator=(Person &p){//先判断是否已有堆区内存,如果已有,则对堆区内存进行一个判断,再进行一个深拷贝if (m_Age != NULL){delete m_Age;m_Age = NULL;}//编译器提供的代码是浅拷贝//m_Age = p.m_Age;//提供深拷贝 解决浅拷贝的问题m_Age = new int(*p.m_Age);return *this;}~Person(){if (m_Age != NULL){delete m_Age; //开辟在堆区的数据由自己释放【此处如果使用浅拷贝将会出错,因此需要使用深拷贝。需从原理上解释】m_Age = NULL;}}int * m_Age;
};void test()
{Person p1(18);Person p2(24);Person p3(30);p1 = p2; //编译器提供的赋值运算符是浅拷贝p3 = p2 = p1;cout << "p1的年龄 = " << *p1.m_Age << endl;cout << "p2的年龄 = " << *p2.m_Age << endl;cout << "p3的年龄 = " << *p2.m_Age << endl;
}int main()
{test();system("pause");return 0;
}
p1的年龄 = 24
p2的年龄 = 24
p3的年龄 = 24
请按任意键继续. . .
作用
:重载关系运算符,可以让两个自定义类型对象进行对比操作
案例描述:如果有两个类对象,如何判断它们两个是否相等,此时则可以对关系运算符进行重载,来进行判断。
1.相等关系运算符==
:bool operator==(Person &p){};本质:p1==p2;
2.不等关系运算符!=
:bool operator!=(Person &p){};本质:p1!=p2;
相等关系运算符重载
#include
using namespace std;
#include class Person
{
public:Person(string name, int age){this->m_Name = name;this->m_Age = age;}//相等关系运算符重载bool operator==(Person& p){if (this->m_Name == p.m_Name && this->m_Age == p.m_Age){return true;}return false;}string m_Name;int m_Age;
};void test()
{Person p1("张三", 18);Person p2("李四", 18);if (p1 == p2){cout << "p1 和 p2相等!" << endl;}else{cout << "p1 和 p2不相等!" << endl;}
}int main() {test();system("pause");return 0;
}
p1 和 p2不相等!
请按任意键继续. . .
不等关系运算符重载
#include
using namespace std;
#include class Person
{
public:Person(string name, int age){this->m_Name = name;this->m_Age = age;}//不等关系运算符重载bool operator!=(Person& p){if (this->m_Name == p.m_Name && this->m_Age == p.m_Age){return false;}return true;}string m_Name;int m_Age;
};void test()
{Person p1("张三", 18);Person p2("李四", 18);if (p1 != p2){cout << "p1 和 p2不相等!" << endl;}else{cout << "p1 和 p2相等!" << endl;}
}int main() {test();system("pause");return 0;
}
p1 和 p2不相等!
请按任意键继续. . .
函数调用运算符()也可以重载,由于重载后使用的方式非常像函数的调用,因此称为仿函数
,仿函数没有固定写法 ,非常灵活。以后在STL中会使用的比较多。
#include
using namespace std;
#include //打印函数类
class MyPrint
{
public:void operator()(string text){cout << text << endl;}
};void test01()
{//重载的() 操作符 也称为仿函数MyPrint myprint;myprint("Hello World!");
}//加函数类
class MyAdd
{
public:int operator()(int num1, int num2){return num1 + num2;}
};void test02()
{MyAdd myadd;int ret = myadd(18,24);cout << "两个数相加后的结果 ret = " << ret << endl;//匿名对象调用cout << "MyAdd()(6,23) = " << MyAdd()(6, 23) << endl;
}int main() {test01();test02();system("pause");return 0;
}
Hello World!
两个数相加后的结果 ret = 42
MyAdd()(6,23) = 29
请按任意键继续. . .