- 运算符重载概念:对已有的运算符重新进行定义,赋予起另一种功能,以适应不同的数据类型。
加号运算符重载:(+)
**作用:**实现两个自定义数据类型相加的运算
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using namespace std;
//加号运算符重载
class Person
{
public:
//1.成员函数重载 + 号 operator+ 是编译器自己定义的
Person operator+(Person &p)//Person类型的函数,返回值是一个新对象
{
Person temp;
temp.m_A=this->m_A+p.m_A;
temp.m_B=this->m_A+p.m_B;
return temp;
}
int m_A;
int m_B;
};
//2.全局函数重载 + 号 (成员函数和全局函数不写同时声明构造)
Person operator+(Person &p1,Person &p2)//Person类型的函数,返回值是一个新对象
{
Person temp;
temp.m_A=p1.m_A+p2.m_B;
temp.m_B=p1.m_A+p2.m_B;
return temp;
}
//函数重载的版本
Person operator+(Person &p1,int num)//Person类型的函数,返回值是一个新对象
{
Person temp;
temp.m_A=p1.m_A+num;
temp.m_B=p1.m_A+num;
return temp;
}
void test01()
{
Person p1;
p1.m_A=10;
p1.m_B=10;
Person p2;
p2.m_A=10;
p2.m_B=10;
//成员函数重载本质调用
//Person p3=p1.operator+(p2)
Person p3=p1+p2;//简写 相当于 p1.operator+(p2)
cout<<"p3.m_A="<<p3.m_A<<endl;//输出:p3.m_A=20
cout<<"p3.m_B="<<p3.m_B<<endl;//输出:p3.m_B=20
//全局函数重载本质调用
//Person p4=operator+(p1,p2);
Person p4=p1+p2;//简写
cout<<"p4.m_A="<<p4.m_A<<endl;//输出:p4.m_A=20
cout<<"p4.m_B="<<p4.m_B<<endl;//输出:p4.m_B=20
//函数也可以发生 本质
//Person p5=operator+(p1,100);
Person p5=p1+100;//相当于 Person + int
cout<<"p5.m_A="<<p5.m_A<<endl;//输出:p5.m_A=110
cout<<"p5.m_B="<<p5.m_B<<endl;//输出:p5.m_B=110
}
int main()
{
test01();
system("pause");
return 0;
}注意:因为全局函数和成员函数的调用,虽然本质是不一样的,但是简写形式是一样的,所以不能同时写,不然编译器无法识别调用的是哪一个。
总结:
- 对于内置的数据类型的表达式的运算符是不可能改变的(如:int float double)。
- 不要滥用运算符重载。
左移运算符重载:(<<)
**作用:**可以输出自定义数据类型。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using namespace std;
//左移运算符重载
class Person
{
//才有友元 可以访问私有属性
friend ostream &operator<<(ostream &cout,Person &p);
public:
Person(int a,int b)
{
m_A=a;
m_B=b;
}
//利用成员函数重载 左移运算符 p.operator<<(cout) 简化版本 p<<cout
//一般不会利用成员函数重载 << 运算符,因为无法实现 cout在左侧
private:
int m_A;
int m_B;
};
// 利用全局函数重载左移运算符
//cout 是一个ostream输出流对象,用ostream声明表示,ostream也是一个类。
//ostream对象全局只能有一个,所以要采用引用的方式
//在引用的时候cout,这个名是可以改变的,因为引用的本质就是给变量起别名
ostream &operator<<(ostream &cout,Person &p)
{
cout<<"m_A="<<p.m_A<<" m_B="<<p.m_B;
return cout;//ostream类型的函数 返回也是ostream型的,这样在cout<<p 后面可以继续采用链式添加
}
void test01()
{
Person p(10,10);
//本质 operator<<(cout,p) 简化:cout<<p
cout<<p<<endl;//因为返回的是ostream型,才可以加endl 不然只能是cout<<p;
}
int main()
{
test01();
system("pause");
return 0;
}总结:重载左移运算符配合友元可以实现输出自定义数据类型。
递增运算符重载:(++)
**作用:**通过重载递增运算符,实现自己的整型数据。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using namespace std;
class MyInteger
{
friend ostream &operator<<(ostream &cout,MyInteger myint);
public:
MyInteger()
{
m_Num=0;
}
//重载前置++运算符
MyInteger& operator++()//前置递增返回的是引用
{
//先进行++运算
m_Num++;
//再将自身做返回
return *this;
}
//重置后置++运算符
//int代表占位参数,可以用于区分前置和后置递增
MyInteger operator++(int)//后置递增返回的是值
{
//先 记录当时结果
MyInteger temp=*this;
//后 递增
m_Num++;
//最后将记录结果做返回
return temp;
}
private:
int m_Num;
};
//重载 << 运算符 返回引用为了一直对一个数据进行递增操作
ostream &operator<<(ostream &cout,MyInteger myint)
{
cout<<myint.m_Num;
return cout;
}
void test01()
{
MyInteger myint;
cout<<++(++myint)<<endl;//输出的数据是一样的,表示对一个数据进行操作
cout<<myint<<endl;//输出的数据是一样的,表示对一个数据进行操作
}
void test02()
{
MyInteger myint1;
cout<<myint++<<endl;
cout<<myint<<endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}**总结:**前置递增返回引用,后置递增返回值
递减运算符重载:(–)
- 递减运算符重载和递增运算符重载性质一样,只需把++变成–。
赋值运算符重载:(=)
- C++编译器至少给一个类添加4个函数
- 默认构造函数(无参,函数体为空)
- 默认析构函数(无参,函数体为空)
- 默认拷贝构造函数,对属性进行值拷贝
- 赋值运算符operator=,对属性进行值拷贝
- 如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题。
1 |
|
关系运算符重载:(> < = !=)
**作用:**重载关系运算符,可以让两个自定义类型对象进行对比操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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;
}
return false;
}
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 test01()
{
Person p1("Tom",18);
Person p2("Tom",18);
if(p1==p2)//本质p1.operator==(p2)
{
cout<<"p1和p2是相等的!"<<endl;
}
else
{
cout<<"p1和p2是不相等的!"<<endl;
}
if(p1!=p2)//本质p1.operator==(p2)
{
cout<<"p1和p2是不相等的!"<<endl;
}
else
{
cout<<"p1和p2是相等的!"<<endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}总结:> < 也是同样的道理
函数调用运算符重载: ()
函数调用运算符()也可以重载
由于重载后使用的方式非常像函数的调用,因此称为仿函数
仿函数没有固定写法,非常灵活
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using namespace std;
//打印类
class MyPrint
{
public:
//重载函数调用运算符
void operator()(string test)
{
cout<<test<<endl;
}
};
void MyPrint01(string test)
{
cout<<test<<endl;
}
void test01()
{
MyPrint myprint;//把这个hello word 传给test
myPrint("hello word");//由于使用起来非常类似于函数调用,因此称为仿函数
MyPrint01("hello word");//这是函数的调用
}
//仿函数非常的灵活,没有固定的写法
//加法类
class MyAdd
{
public:
int operator()(int num1,int num2)
{
return num1+num2;
}
};
void test02()
{
MyAdd myadd;
int ret=myadd(100,200);//100传给num1,200传给num2
cout<<"ret="<<ret<<endl;
//匿名函数对象 调用
cout<<MyAdd()(100,100)<<endl;//MyAdd()匿名对象 特点:当前行执行完会立即被释放
}
int main()
{
test01();
test02();
system("pause");
return 0;
}