运算符重载

运算符重载

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
#include

using namespace std;


//加号运算符重载
//全局函数重载加号

class Person{
public:
int a;
int b;
// Person operator+(Person &p){//成员函数的本质
// Person temp;
// temp.a=p.a+this->a;
// temp.b=p.b+this->b;
// return temp;
// }
};

Person operator+(Person &p1,Person &p2){
Person temp;
temp.a=p1.a+p2.a;
temp.b=p1.b+p2.b;
return temp;
}
int main(){
Person p1;
Person p2;
p2.a=12;
p2.b=23;
p1.a=10;
p1.b=10;
Person p3=p1+p2;
cout<"------------"<endl;
}

左移运算符重载

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
#include


using namespace std;


class Person{
friend ostream & operator<<(ostream &cout,Person &p);
private:
int a;
int b;
public:
Person(int a,int b){
this->a=a;
this->b=b;
}
};

ostream & operator<<(ostream &cout,Person &p){
cout<"-----------"<
}

int main(){
Person p1(12,23);
cout<endl;//直接输出对象

}

递增运算符重载

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
#include


using namespace std;

//自增运算符重载

class MyInteger{
friend ostream & operator<<(ostream &cout,MyInteger Integer);//为什么不传引用
private:
int num;

public:
MyInteger(){
num=0;
}
MyInteger& operator++(){//前置++
num++;
return *this;
}

MyInteger operator++(int){//后置++
MyInteger temp=*this;
num++;
return temp;
}

};
ostream & operator<<(ostream &cout,MyInteger Integer){
cout<
return cout;
}
//重载++运算符
//前置++,后置++;

void test(){
MyInteger myint;
cout<endl;

cout<endl;

}
int main(){
MyInteger num;
cout<endl;
cout<<++num<<endl;
cout<endl;
test();
}

赋值运算符重载

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
#include

using namespace std;

class Person{
public:
int *a;
public:
Person(int age){
a=new int(age);
}
~Person(){
if(a!=NULL){
delete(a);
a=NULL;
}
}
Person & operator=(Person &p){
if(a!=NULL){
delete(a);
}
a=new int(*p.a);//深拷贝
return *this;
}
};

void test(){
Person p1(20);
Person p2(10);
Person p3(10);
p3=p2=p1;
cout<< *p2.a<<endl;
cout<< *p3.a<<endl;
}

int main(){
test();
}

关系运算符重载

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
#include

using namespace std;

class Person{
private:
int age;
string name;
public:
Person(int age,string name){
this->age=age;
this->name=name;
}
bool operator==(Person &p){
if(this->age==p.age&&this->name==p.name){
return true;
}
return false;
}
};

int main(){
Person p1(12,"java");
Person p2(12,"java");
if(p1==p2){
cout<<"p1=p2"<<endl;
}else{
cout<<"p1!=p2"<<endl;
}
}

仿函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include
using namespace std;

class Person{
public:
void operator()(string text){
cout<<text<<endl;
}
};
int main(){
Person p1;
p1("Hello world");
Person()("123");//匿名对象
}

本文标题:运算符重载

文章作者:philxling

发布时间:2020年04月05日 - 23:11

最后更新:2020年10月15日 - 12:26

原始链接:http://philxling.club/2020/04/05/c++/3-%E8%BF%90%E7%AE%97%E7%AC%A6%E9%87%8D%E8%BD%BD/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

philxling wechat
ex. subscribe to my blog by scanning my public wechat account
-------------本文结束感谢您的阅读-------------
谢谢你给我糖吃!!!
0%