1> 思维导图
2> 整理代码
代码:
算术运算符重载:
#include <iostream>
using namespace std;
class Person
{
//全局函数实现运算符重载需要权限
friend const Person operator+(const Person L,const Person R);
private:
int a;
int b;
public:
Person() {}
Person(int a,int b){
this->a=a;
this->b=b;
}
void show(){
cout << "a=" << a <<"b=" << b<<endl;
}
// 成员函数实现运算符重载
// const Person operator+(const Person p)const{
// Person temp;
// temp.a=a+p.a;
// temp.b=b+p.b;
// return temp;
// }
const Person operator-(const Person p)const{
Person temp;
temp.a=a-p.a;
temp.b=b-p.b;
return temp;
}
const Person operator*(const Person p)const{
Person temp;
temp.a=a*p.a;
temp.b=b*p.b;
return temp;
}
const Person operator/(const Person p)const{
Person temp;
temp.a=a/p.a;
temp.b=b/p.b;
return temp;
}
};
//全局函数实现运算符重载
const Person operator+(const Person L,const Person R){
Person temp;
temp.a=L.a+R.a;
temp.b=L.b+R.b;
return temp;
}
int main()
{
Person p1(10,10);
Person p2(10,20);
Person p3=p1+p2;
Person p4=p1-p2;
Person p5=p1*p2;
Person p6=p1/p2;
p1.show();
p2.show();
p3.show();
p4.show();
p5.show();
p6.show();
return 0;
}
关系运算符重载:
#include <iostream>
using namespace std;
class Person
{
//全局函数实现运算符重载需要权限
friend bool operator>(const Person L,const Person R);
private:
int a;
int b;
public:
Person() {}
Person(int a,int b){
this->a=a;
this->b=b;
}
void show(){
cout << "a=" << a <<"b=" << b<<endl;
}
// bool operator>(const Person p)const{
// if(a>p.a && b>p.b){
// return true;
// }
// return false;
// }
bool operator==(const Person p)const{
if(a==p.a && b==p.b){
return true;
}
return false;
}
bool operator<(const Person p)const{
if(a<p.a && b<p.b){
return true;
}
return false;
}
bool operator>=(const Person p)const{
if(a>=p.a && b>=p.b){
return true;
}
return false;
}
bool operator<=(const Person p)const{
if(a<=p.a && b<=p.b){
return true;
}
return false;
}
};
//全局函数实现运算符重载
bool operator>(const Person L,const Person R){
if(L.a>R.a && L.b>R.b)
return true;
return false;
}
int main()
{
Person p1(10,10);
Person p2(10,20);
p1.show();
p2.show();
if(p2>p1){
cout << "p2>p1" <<endl;
}
if(p2==p1){
cout << "p2==p1" <<endl;
}
if(p2<p1){
cout << "p2<p1" <<endl;
}
if(p2>=p1){
cout << "p2>=p1" <<endl;
}
if(p2<=p1){
cout << "p2<=p1" <<endl;
}
return 0;
}
赋值运算符重载
#include <iostream>
using namespace std;
class Person
{
//全局函数实现运算符重载需要权限
friend Person &operator+=(Person &L,const Person &R);
private:
int a;
int b;
public:
Person() {}
Person(int a,int b){
this->a=a;
this->b=b;
}
void show(){
cout << "a=" << a <<"b=" << b<<endl;
}
// Person &operator+=(const Person p){
// a+=p.a;
// b+=p.b;
// return *this;
// }
Person &operator-=(const Person p){
a-=p.a;
b-=p.b;
return *this;
}
Person &operator*=(const Person p){
a*=p.a;
b*=p.b;
return *this;
}
Person &operator/=(const Person p){
a/=p.a;
b/=p.b;
return *this;
}
Person &operator%=(const Person p){
a%=p.a;
b%=p.b;
return *this;
}
};
//全局函数实现运算符重载
Person &operator+=(Person &L,const Person &R){
L.a+=R.a;
L.b+=R.b;
return L;
}
int main()
{
Person p1(10,10);
Person p2(10,20);
p1.show();
p2.show();
// cout << "+=";
// p2+=p1;
// p2.show();
// cout << "-=";
// p2-=p1;
// p2.show();
// cout << "*=";
// p2*=p1;
// p2.show();
// cout << "/=";
// p2/=p1;
// p2.show();
cout << "%=";
p2%=p1;
p2.show();
return 0;
}
今日思维导图