一、重载加号

  1. #include <iostream>
  2. using namespace std;
  3. class Person{
  4. friend ostream& operator<<(ostream& cout, Person& p);
  5. public:
  6. Person(){
  7. }
  8. Person(int score){
  9. this->m_Score = score;
  10. }
  11. // Person operator+(const Person& p){ //成员函数的方式
  12. // Person temp;
  13. // temp.m_Score = this->m_Score + p.m_Score;
  14. // return temp;
  15. // }
  16. int m_Score;
  17. };
  18. ostream& operator<<(ostream& cout, Person& p){
  19. cout << "score = " << p.m_Score << endl;
  20. return cout;
  21. }
  22. Person operator+(const Person& per1,const Person& per2){ // 全局函数的方式
  23. Person temp;
  24. temp.m_Score = per1.m_Score + per2.m_Score;
  25. return temp;
  26. }
  27. int main(){
  28. Person per1(10);
  29. Person per2(20);
  30. Person per3 = per1 + per2;
  31. cout << per3 << endl;
  32. return 0;
  33. }

二、重载减号

  1. #include <iostream>
  2. using namespace std;
  3. class Person{
  4. friend ostream& operator<<(ostream& cout, Person& p);
  5. public:
  6. Person(){
  7. }
  8. Person(int score){
  9. this->m_Score = score;
  10. }
  11. // Person operator+(const Person& p){ //成员函数方式重载加号
  12. // Person temp;
  13. // temp.m_Score = this->m_Score + p.m_Score;
  14. // return temp;
  15. // }
  16. // Person operator-(const Person& p){ //成员函数方式重载减号
  17. // Person temp;
  18. // temp.m_Score = this->m_Score - p.m_Score;
  19. // return temp;
  20. // }
  21. int m_Score;
  22. };
  23. ostream& operator<<(ostream& cout, Person& p){
  24. cout << "score = " << p.m_Score << endl;
  25. return cout;
  26. }
  27. Person operator+(const Person& per1,const Person& per2){ // 全局函数的方式
  28. Person temp;
  29. temp.m_Score = per1.m_Score + per2.m_Score;
  30. return temp;
  31. }
  32. Person operator-(const Person& per1,const Person& per2){ // 全局函数的方式
  33. Person temp;
  34. temp.m_Score = per1.m_Score - per2.m_Score;
  35. return temp;
  36. }
  37. int main(){
  38. Person per1(10);
  39. Person per2(20);
  40. Person per3 = per1 + per2;
  41. Person per4 = per3 - per2;
  42. cout << "per3 : " << per3 << endl;
  43. cout << "per4 : " << per4 << endl;
  44. return 0;
  45. }

三、重载乘号

  1. #include <iostream>
  2. using namespace std;
  3. class Person{
  4. friend ostream& operator<<(ostream& cout, Person& p);
  5. public:
  6. Person(){
  7. }
  8. Person(int score){
  9. this->m_Score = score;
  10. }
  11. // Person operator+(const Person& p){ //成员函数的方式
  12. // Person temp;
  13. // temp.m_Score = this->m_Score + p.m_Score;
  14. // return temp;
  15. // }
  16. // Person operator-(const Person& p){
  17. // Person temp;
  18. // temp.m_Score = this->m_Score - p.m_Score;
  19. // return temp;
  20. // }
  21. // Person operator*(const Person& p){
  22. // Person temp;
  23. // temp.m_Score = this->m_Score * p.m_Score;
  24. // return temp;
  25. // }
  26. int m_Score;
  27. };
  28. ostream& operator<<(ostream& cout, Person& p){
  29. cout << "score = " << p.m_Score << endl;
  30. return cout;
  31. }
  32. Person operator+(const Person& per1,const Person& per2){ // 全局函数的方式
  33. Person temp;
  34. temp.m_Score = per1.m_Score + per2.m_Score;
  35. return temp;
  36. }
  37. Person operator-(const Person& per1,const Person& per2){ // 全局函数的方式
  38. Person temp;
  39. temp.m_Score = per1.m_Score - per2.m_Score;
  40. return temp;
  41. }
  42. Person operator*(const Person& per1,const Person& per2){ // 全局函数的方式
  43. Person temp;
  44. temp.m_Score = per1.m_Score * per2.m_Score;
  45. return temp;
  46. }
  47. int main(){
  48. Person per1(10);
  49. Person per2(20);
  50. Person per3 = per1 + per2;
  51. Person per4 = per3 - per2;
  52. Person per5 = per1 * per2;
  53. cout << "per3 : " << per3 << endl;
  54. cout << "per4 : " << per4 << endl;
  55. cout << "per5 : " << per5 << endl;
  56. return 0;
  57. }