一、重载加号
#include <iostream>
using namespace std;
class Person{
friend ostream& operator<<(ostream& cout, Person& p);
public:
Person(){
}
Person(int score){
this->m_Score = score;
}
// Person operator+(const Person& p){ //成员函数的方式
// Person temp;
// temp.m_Score = this->m_Score + p.m_Score;
// return temp;
// }
int m_Score;
};
ostream& operator<<(ostream& cout, Person& p){
cout << "score = " << p.m_Score << endl;
return cout;
}
Person operator+(const Person& per1,const Person& per2){ // 全局函数的方式
Person temp;
temp.m_Score = per1.m_Score + per2.m_Score;
return temp;
}
int main(){
Person per1(10);
Person per2(20);
Person per3 = per1 + per2;
cout << per3 << endl;
return 0;
}
二、重载减号
#include <iostream>
using namespace std;
class Person{
friend ostream& operator<<(ostream& cout, Person& p);
public:
Person(){
}
Person(int score){
this->m_Score = score;
}
// Person operator+(const Person& p){ //成员函数方式重载加号
// Person temp;
// temp.m_Score = this->m_Score + p.m_Score;
// return temp;
// }
// Person operator-(const Person& p){ //成员函数方式重载减号
// Person temp;
// temp.m_Score = this->m_Score - p.m_Score;
// return temp;
// }
int m_Score;
};
ostream& operator<<(ostream& cout, Person& p){
cout << "score = " << p.m_Score << endl;
return cout;
}
Person operator+(const Person& per1,const Person& per2){ // 全局函数的方式
Person temp;
temp.m_Score = per1.m_Score + per2.m_Score;
return temp;
}
Person operator-(const Person& per1,const Person& per2){ // 全局函数的方式
Person temp;
temp.m_Score = per1.m_Score - per2.m_Score;
return temp;
}
int main(){
Person per1(10);
Person per2(20);
Person per3 = per1 + per2;
Person per4 = per3 - per2;
cout << "per3 : " << per3 << endl;
cout << "per4 : " << per4 << endl;
return 0;
}
三、重载乘号
#include <iostream>
using namespace std;
class Person{
friend ostream& operator<<(ostream& cout, Person& p);
public:
Person(){
}
Person(int score){
this->m_Score = score;
}
// Person operator+(const Person& p){ //成员函数的方式
// Person temp;
// temp.m_Score = this->m_Score + p.m_Score;
// return temp;
// }
// Person operator-(const Person& p){
// Person temp;
// temp.m_Score = this->m_Score - p.m_Score;
// return temp;
// }
// Person operator*(const Person& p){
// Person temp;
// temp.m_Score = this->m_Score * p.m_Score;
// return temp;
// }
int m_Score;
};
ostream& operator<<(ostream& cout, Person& p){
cout << "score = " << p.m_Score << endl;
return cout;
}
Person operator+(const Person& per1,const Person& per2){ // 全局函数的方式
Person temp;
temp.m_Score = per1.m_Score + per2.m_Score;
return temp;
}
Person operator-(const Person& per1,const Person& per2){ // 全局函数的方式
Person temp;
temp.m_Score = per1.m_Score - per2.m_Score;
return temp;
}
Person operator*(const Person& per1,const Person& per2){ // 全局函数的方式
Person temp;
temp.m_Score = per1.m_Score * per2.m_Score;
return temp;
}
int main(){
Person per1(10);
Person per2(20);
Person per3 = per1 + per2;
Person per4 = per3 - per2;
Person per5 = per1 * per2;
cout << "per3 : " << per3 << endl;
cout << "per4 : " << per4 << endl;
cout << "per5 : " << per5 << endl;
return 0;
}