# C++学习(五)_运算符重载

### 重载运算符

#### 重载运算符的规则

{

}

friend 函数类型 operator 运算符（形参表）
{

}

#### 例1：重载+，-

1.使用成员函数实现运算符+，-的重载
#include<iostream>

using namespace std;

class Point {
public:
Point(int x = 0, int y = 0):x(x),y(y) {};
Point operator+(Point p) {
return Point(this->x + p.x, this->y + p.y);
}
Point operator-(Point p) {
return Point(this->x - p.x, this->y - p.y);
}
void display() {
cout << "(" << x << "," << y << ")";
}

private:
int x,y;
};

int main()
{
Point p1(1, 1);
Point p2(2, 2);
//1.将友元函数注释，调用Point的成员函数实现 p1 + p2
Point p3 = p1 + p2;

cout << "p1:";//输出p1的值
p1.display();
cout << " + ";
cout << "p2:";//输出p2的值
p2.display();
cout << " = ";
cout << "p3:";//输出p3的值
p3.display();
cout << endl;

Point p4 = p1 - p2;

cout << "p1:";//输出p1的值
p1.display();
cout << " - ";
cout << "p2:";//输出p2的值
p2.display();
cout << " = ";
cout << "p4:";//输出p4的值
p4.display();
cout << endl;

return 0;
}


2.使用友元函数实现运算符+，-的重载
#include<iostream>

using namespace std;

class Point {
friend Point operator+(Point p1, Point p2); //友元函数
friend Point operator-(Point p1, Point p2);
public:
Point(int x = 0, int y = 0):x(x),y(y) {};
void display() {
cout << "(" << x << "," << y << ")";
}

private:
int x,y;
};

//重载运算符实现
Point operator+(Point p1, Point p2) {
return Point(p1.x + p2.x, p1.y + p2.y);
}

Point operator-(Point p1, Point p2) {
return Point(p1.x - p2.x, p1.y - p2.y);
}

int main()
{
Point p1(1, 1);
Point p2(2, 2);
//2.将成语函数注释，调用Point的友元函数实现 p1 + p2
Point p3 = p1 + p2;

cout << "p1:";//输出p1的值
p1.display();
cout << " + ";
cout << "p2:";//输出p2的值
p2.display();
cout << " = ";
cout << "p3:";//输出p3的值
p3.display();
cout << endl;

Point p4 = p1 - p2;

cout << "p1:";//输出p1的值
p1.display();
cout << " - ";
cout << "p2:";//输出p2的值
p2.display();
cout << " = ";
cout << "p4:";//输出p4的值
p4.display();
cout << endl;

system("pause");

return 0;
}


#### 例2：重载++，--

1.使用成员函数重载++
#include<iostream>

using namespace std;

class Point {

public:
Point(int x = 0, int y = 0):x(x),y(y) {};
Point & operator++() {    //对运算符++（前置）进行重载
this->x++;
this->y++;
return *this;
}
Point operator++(int) {  //对运算符++（后置）进行重载
Point p = *this;
this->x++;
this->y++;
return p;
}
void display() {   //输出点
cout << "(" << x << "," << y << ")" << endl << endl;
}

private:
int x,y;
};

int main()
{
Point p(1, 1);
cout << "p ：";
p.display();

//p++
cout << "------ p++ ------" << endl;

Point p1 = p++;
cout << "p1：";
p1.display();

cout << "p ：";
p.display();

cout << "------ p++ ------" << endl << endl;

cout << "p ：";
p.display();

//++p
cout << "------ ++p ------" << endl;

Point p2 = ++p;
cout << "p2：";
p2.display();

cout << "p ：";
p.display();

cout << "------ ++p ------" << endl << endl;

system("pause");

return 0;
}


2.使用成员函数重载--
#include<iostream>

using namespace std;

class Point {

public:
Point(int x = 0, int y = 0):x(x),y(y) {};
Point& operator--() {     //对运算符--（前置）进行重载
this->x--;
this->y--;
return *this;
}
Point operator--(int) {    //对运算符--（后置）进行重载
Point p = *this;
this->x--;
this->y--;
return p;
}
void display() {
cout << "(" << x << "," << y << ")" << endl << endl;
}

private:
int x,y;
};

int main()
{
Point p(1, 1);
cout << "p ：";
p.display();

//p--
cout << "------ (p--) ------" << endl;

Point p1 = p--;
cout << "p1：";
p1.display();

cout << "p ：";
p.display();

cout << "------ (p--) ------" << endl << endl;

cout << "p ：";
p.display();

//--p
cout << "------ (--p) ------" << endl;

Point p2 = --p;
cout << "p2：";
p2.display();

cout << "p ：";
p.display();

cout << "------ (--p) ------" << endl << endl;

system("pause");

return 0;
}


3.使用友元函数重载++
#include<iostream>

using namespace std;

class Point {
friend Point& operator++(Point& p);     //友元函数
friend Point operator++(Point& p, int);
public:
Point(int x = 0, int y = 0) :x(x), y(y) {};
void display() {
cout << "(" << x << "," << y << ")" << endl << endl;
}

private:
int x, y;
};

Point& operator++(Point &p) {
p.x++;
p.y++;
return p;
}

Point operator++(Point &p, int) {
Point temp = p;
p.x++;
p.y++;
return temp;
}

int main()
{
Point p(1, 1);
cout << "p ：";
p.display();

//p++
cout << "------ p++ ------" << endl;

Point p1 = p++;
cout << "p1：";
p1.display();

cout << "p ：";
p.display();

cout << "------ p++ ------" << endl << endl;

cout << "p ：";
p.display();

//++p
cout << "------ ++p ------" << endl;

Point p2 = ++p;
cout << "p2：";
p2.display();

cout << "p ：";
p.display();

cout << "------ ++p ------" << endl << endl;

system("pause");

return 0;
}


4.使用友元函数重载--
#include<iostream>

using namespace std;

class Point {
friend Point& operator--(Point& p);   //友元函数
friend Point operator--(Point& p, int);
public:
Point(int x = 0, int y = 0) :x(x), y(y) {};
void display() {
cout << "(" << x << "," << y << ")" << endl << endl;
}

private:
int x, y;
};

Point& operator--(Point& p) {
p.x--;
p.y--;
return p;
}

Point operator--(Point& p, int) {
Point temp = p;
p.x--;
p.y--;
return temp;
}

int main()
{
Point p(1, 1);
cout << "p ：";
p.display();

//p--
cout << "------ (p--) ------" << endl;

Point p1 = p--;
cout << "p1：";
p1.display();

cout << "p ：";
p.display();

cout << "------ (p--) ------" << endl << endl;

cout << "p ：";
p.display();

//--p
cout << "------ (--p) ------" << endl;

Point p2 = --p;
cout << "p2：";
p2.display();

cout << "p ：";
p.display();

cout << "------ (--p) ------" << endl << endl;

system("pause");

return 0;
}


#### 例3：重载<<运算符实现对Point对象的输出

#include<iostream>

using namespace std;

class Point {
friend ostream& operator<<(ostream & out, Point& p);    //友元函数
public:
Point(int x = 0, int y = 0) :x(x), y(y) {};
private:
int x, y;
};

ostream& operator<<(ostream & out,Point &p) {
out << "(" << p.x << "," << p.y << ")";
return out;
}

int main()
{
Point p(1, 1);
cout << "p：" << p;    //使用重载后的<<输出p

system("pause");

return 0;
}


*使用成员函数重载<<运算符
#include<iostream>

using namespace std;

class Point {
public:
Point(int x = 0, int y = 0) :x(x), y(y) {};
void operator<<(ostream& out) {        //使用成员函数进行<<运算符重载
out << "(" << this->x << "," << this->y << ")";
}
private:
int x, y;
};

int main()
{
Point p(1, 1);
p << cout;

system("pause");

return 0;
}


p << cout


posted @ 2019-10-27 13:40  美团骑手阿霖  阅读(170)  评论(0编辑  收藏  举报