• C++学习:类的使用--运算符重载


    我们知道C++可以对函数进行重载,让同名的函数来完成相同的基本操作。其实运算符也是可以重载的,而且有的运算符已经在使用了,就像*,既可以用于地址,又可以用于乘法。

    运算符重载需要使用关键字operator

    语法如下

    1. Type operator Sign(const Type p1, const Type p2)
    2. {
    3. Type ret;
    4. return ret;
    5. }

     Sign指的是+   -    *   /  等操作符

    复数类开发

    如果我们需要计算两个复数的运算,就需要进行操作符重载了,因为常规的+ -不能直接进行复数运算。

    1. #ifndef COMPLEX_H
    2. #define COMPLEX_H
    3. class Complex
    4. {
    5. private:
    6. int a;
    7. int b;
    8. public:
    9. Complex();
    10. Complex(int a, int b);
    11. int getA();
    12. int getB();
    13. friend Complex operator+ (const Complex& p1, const Complex& p2);
    14. };
    15. Complex operator+ (const Complex& p1, const Complex& p2);
    16. #endif // COMPLEX_H
    1. #include "complex.h"
    2. Complex::Complex(int a, int b)
    3. {
    4. this->a = a;
    5. this->b = b;
    6. }
    7. Complex::Complex()
    8. {
    9. a = 0;
    10. b = 0;
    11. }
    12. int Complex::getA()
    13. {
    14. return a;
    15. }
    16. int Complex::getB()
    17. {
    18. return b;
    19. }
    20. //+操作符重载,两个复数相加,实部与实部相加,虚部与虚部相加
    21. //operator+相当于函数名,
    22. //但是使用的时候直接使用+就可以
    23. Complex operator+(const Complex& p1, const Complex& p2)
    24. {
    25. Complex ret;
    26. ret.a = p1.a + p2.a;
    27. ret.b = p1.b + p2.b;
    28. return ret;
    29. }

     

    1. #include
    2. #include "complex.h"
    3. using namespace std;
    4. int main()
    5. {
    6. Complex c1(1, 2);
    7. Complex c2(3, 4);
    8. //Complex c3 = operator+(c1,c2);
    9. Complex c3 = c1+c2; //与上面的语句等效
    10. cout <<" c3.getA():"<< c3.getA() << endl;
    11. cout <<" c3.getB():"<< c3.getB() << endl;
    12. return 0;
    13. }

     分析:语句Complex c3 = operator+(c1,c2); 和语句Complex c3 = c1+c2;效果是一样的,本来复数是不能直接加减的,但是进行了+ 操作符重载,在进行复数运算是编译器法相不能直接+,就去看你有没有炒作符重载,有的话就直接拿来用。

    上面的代码用的友元,在C++中友元是要慎用的,,可以将操作符重载函数直接定义为类的成员函数也是可以运行的,如下所示:

    1. #ifndef COMPLEX_H
    2. #define COMPLEX_H
    3. class Complex
    4. {
    5. private:
    6. int a;
    7. int b;
    8. public:
    9. Complex();
    10. Complex(int a, int b);
    11. int getA();
    12. int getB();
    13. Complex operator+(const Complex& p);
    14. };
    15. #endif // COMPLEX_H
    1. #include "complex.h"
    2. Complex::Complex(int a, int b)
    3. {
    4. this->a = a;
    5. this->b = b;
    6. }
    7. Complex::Complex()
    8. {
    9. a = 0;
    10. b = 0;
    11. }
    12. int Complex::getA()
    13. {
    14. return a;
    15. }
    16. int Complex::getB()
    17. {
    18. return b;
    19. }
    20. //+操作符重载,两个复数相加,实部与实部相加,虚部与虚部相加
    21. //operator+相当于函数名,
    22. //但是使用的时候直接使用+就可以
    23. Complex Complex::operator+(const Complex& p)
    24. {
    25. Complex ret;
    26. ret.a = this->a + p.a;
    27. //等效于ret.a = a + p.a;
    28. ret.b = this->b + p.b;
    29. //等效于ret.b = b + p.b;
    30. return ret;
    31. }

    友元函数与操作符重载函数对比

    友元函数:friend Complex operator+ (const Complex& p1, const Complex& p2);

    使用友源函数,需要左操作数p1和右操作数p2

    类操作符重载函数:Complex operator+(const Complex& p);

    使用类内部操作符重载函数,参数只有一个p,作为右操作数,左操作数就是本身this。

    时间类开发

    下面一个时间类,也进行时间的相关运算,时与时相加减,分钟与分钟相加减

    1. #ifndef MYTIME_H
    2. #define MYTIME_H
    3. #include
    4. using namespace std;
    5. class Time
    6. {
    7. private:
    8. int hours;
    9. int minutes;
    10. public:
    11. Time();
    12. Time(int h, int m = 0);
    13. void AddMin(int m);
    14. void AddHr(int h);
    15. void Reset(int h = 0, int m = 0);
    16. Time Sum(const Time& t) const;
    17. void Show() const;
    18. };
    19. #endif // MYTIME_H

    1. #include "mytime.h"
    2. Time::Time()
    3. {
    4. hours = minutes = 0;
    5. }
    6. Time::Time(int h, int m)
    7. {
    8. hours = h;
    9. minutes = m;
    10. }
    11. void Time::AddMin(int m)
    12. {
    13. minutes += m;
    14. hours += minutes/60;
    15. minutes %= 60;
    16. }
    17. void Time::AddHr(int h)
    18. {
    19. hours += h;
    20. }
    21. void Time::Reset(int h, int m)
    22. {
    23. hours = h;
    24. minutes = m;
    25. }
    26. Time Time::Sum(const Time &t) const
    27. {
    28. Time sum;
    29. sum.minutes = minutes + t.minutes;
    30. sum.hours = hours + t.hours + sum.minutes/60;
    31. sum.minutes %= 60;
    32. return sum;
    33. }
    34. void Time::Show() const
    35. {
    36. cout << hours << " hours, " << minutes << " minutes.\n";
    37. }

    1. #include
    2. #include "mytime.h"
    3. using namespace std;
    4. int main()
    5. {
    6. Time planning;
    7. Time coding(2, 40);
    8. Time fixing(5, 55);
    9. Time total;
    10. cout << "planning time = ";
    11. planning.Show();
    12. cout << endl;
    13. cout << "coding time = ";
    14. coding.Show();
    15. cout << endl;
    16. cout << "fixing time = ";
    17. fixing.Show();
    18. cout << endl;
    19. total = coding.Sum(fixing);
    20. cout << "coding.Sum(fixing) = ";
    21. total.Show();
    22. cout << endl;
    23. return 0;
    24. }

    输出结果

    1. planning time = 0 hours, 0 minutes.
    2. coding time = 2 hours, 40 minutes.
    3. fixing time = 5 hours, 55 minutes.
    4. coding.Sum(fixing) = 8 hours, 35 minutes.

    添加加法运算符

    Time类转换为重载的加法运算法很容易,只要将Sum()的名称改为operator +()即可。只要把运算符+放在operator的后面

    1. #ifndef STOCK_H
    2. #define STOCK_H
    3. #include
    4. using namespace std;
    5. class stock
    6. {
    7. private:
    8. string company;
    9. long shares;
    10. double share_val;
    11. double total_val;
    12. void set_tot() {total_val = shares * share_val;}
    13. public:
    14. void buy(long num, double price);
    15. void sell(long num, double price);
    16. void update(double price);
    17. void show() const;
    18. Time operator+(const Time& t) const;
    19. const stock& topval(const stock& s) const;
    20. stock(const string& co, long n, double pr);
    21. stock();
    22. ~stock();
    23. };
    24. #endif // STOCK_H

    1. #include "mytime.h"
    2. Time::Time()
    3. {
    4. hours = minutes = 0;
    5. }
    6. Time::Time(int h, int m)
    7. {
    8. hours = h;
    9. minutes = m;
    10. }
    11. void Time::AddMin(int m)
    12. {
    13. minutes += m;
    14. hours += minutes/60;
    15. minutes %= 60;
    16. }
    17. void Time::AddHr(int h)
    18. {
    19. hours += h;
    20. }
    21. void Time::Reset(int h, int m)
    22. {
    23. hours = h;
    24. minutes = m;
    25. }
    26. //运算符+重载
    27. Time Time::operator+(const Time &t) const
    28. {
    29. Time sum;
    30. sum.minutes = minutes + t.minutes;
    31. sum.hours = hours + t.hours + sum.minutes/60;
    32. sum.minutes %= 60;
    33. return sum;
    34. }
    35. void Time::Show() const
    36. {
    37. cout << hours << " hours, " << minutes << " minutes.\n";
    38. }
    operator+(const Time &t) const与Sum(const Time &t) const效果完全一样,可以理解operator+()就是一个函数,也可以被调用。就像下面的语句一样。
    total = coding.operator+(fixing);

    但是可以进一步简化

    total = coding + fixing;

     这两种方法都讲使用operator+()函数,运算符左侧的coding是调用对象,右边的fixing是参数被传递的对象。

    1. #include
    2. #include "mytime.h"
    3. using namespace std;
    4. int main()
    5. {
    6. Time planning;
    7. Time coding(2, 40);
    8. Time fixing(5, 55);
    9. Time total;
    10. cout << "planning time = ";
    11. planning.Show();
    12. cout << endl;
    13. cout << "coding time = ";
    14. coding.Show();
    15. cout << endl;
    16. cout << "fixing time = ";
    17. fixing.Show();
    18. cout << endl;
    19. //total = coding.operator+(fixing);
    20. total = coding + fixing;
    21. cout << "coding.Sum(fixing) = ";
    22. total.Show();
    23. cout << endl;
    24. return 0;
    25. }

    输出结果没有变化

    1. planning time = 0 hours, 0 minutes.
    2. coding time = 2 hours, 40 minutes.
    3. fixing time = 5 hours, 55 minutes.
    4. coding.Sum(fixing) = 8 hours, 35 minutes.

     运算符重载的限制:

    1、重载后的运算符必须至少有一个操作数是用户定义的类型;

    2、使用运算符使不能违反运算符原来的句法规则;

    3、不能创建新的运算符;

    4、有些运算符不能被重载:如sizeof, ?:, ::等

    5、大多数运算符都可以通过成员或者非成员函数进行重载,但=、()、[]、->只能通过成员函数进行重载。

    + 、-、 *重载的应用

    1. #ifndef MYTIME_H
    2. #define MYTIME_H
    3. #include
    4. using namespace std;
    5. class Time
    6. {
    7. private:
    8. int hours;
    9. int minutes;
    10. public:
    11. Time();
    12. Time(int h, int m = 0);
    13. void AddMin(int m);
    14. void AddHr(int h);
    15. void Reset(int h = 0, int m = 0);
    16. Time operator+(const Time& t) const;
    17. Time operator-(const Time& t) const;
    18. Time operator*(double mult) const;
    19. void Show() const;
    20. };
    21. #endif // MYTIME_H
    1. #include "mytime.h"
    2. Time::Time()
    3. {
    4. hours = minutes = 0;
    5. }
    6. Time::Time(int h, int m)
    7. {
    8. hours = h;
    9. minutes = m;
    10. }
    11. void Time::AddMin(int m)
    12. {
    13. minutes += m;
    14. hours += minutes/60;
    15. minutes %= 60;
    16. }
    17. void Time::AddHr(int h)
    18. {
    19. hours += h;
    20. }
    21. void Time::Reset(int h, int m)
    22. {
    23. hours = h;
    24. minutes = m;
    25. }
    26. //运算符+重载
    27. Time Time::operator+(const Time &t) const
    28. {
    29. Time sum;
    30. sum.minutes = minutes + t.minutes;
    31. sum.hours = hours + t.hours + sum.minutes/60;
    32. sum.minutes %= 60;
    33. return sum;
    34. }
    35. Time Time::operator-(const Time& t) const
    36. {
    37. Time diff;
    38. int tot1, tot2;
    39. tot1 = t.minutes + 60 * t.hours;
    40. tot2 = minutes + 60 * hours;
    41. diff.minutes = (tot2 - tot1) % 60;
    42. diff.hours = (tot2 - tot1) / 60;
    43. return diff;
    44. }
    45. Time Time::operator*(double mult) const
    46. {
    47. Time result;
    48. long totalminutes = hours * mult *60 + minutes * mult;
    49. result.hours = totalminutes / 60;
    50. result.minutes = totalminutes % 60;
    51. return result;
    52. }
    53. void Time::Show() const
    54. {
    55. cout << hours << " hours, " << minutes << " minutes.\n";
    56. }
    1. #include
    2. #include "mytime.h"
    3. using namespace std;
    4. int main()
    5. {
    6. Time weeding(4, 35);
    7. Time waxing(2, 47);
    8. Time total;
    9. Time diff;
    10. Time adjusted;
    11. cout << "weeding time = ";
    12. weeding.Show();
    13. cout << endl;
    14. cout << "waxing time = ";
    15. waxing.Show();
    16. cout << endl;
    17. cout << "total working time = ";
    18. total = weeding + waxing;
    19. total.Show();
    20. cout << endl;
    21. diff = weeding - waxing;
    22. cout << "weeding time - waxing time = ";
    23. diff.Show();
    24. cout << endl;
    25. adjusted = total * 1.5;
    26. cout << "adjusted work time = ";
    27. adjusted.Show();
    28. cout << endl;
    29. return 0;
    30. }

    输出结果

    1. weeding time = 4 hours, 35 minutes.
    2. waxing time = 2 hours, 47 minutes.
    3. total working time = 7 hours, 22 minutes.
    4. weeding time - waxing time = 1 hours, 48 minutes.
    5. adjusted work time = 11 hours, 3 minutes.

  • 相关阅读:
    Bug战场:C++篇
    双节履带机械臂小车实现蓝牙遥控功能
    最新,Spring Boot 2.3.5 发布,你跟上了吗?
    javaO2O生鲜果蔬电商设计与实现计算机毕业设计MyBatis+系统+LW文档+源码+调试部署
    golang 装饰器模式详解
    秋日渲染季 | 效果图充值新升级!金秋好礼等你领
    Arduino基础入门
    VMware下的ubuntu虚拟机,实现虚拟机与本地硬盘间的文件互传
    High-variance latent spaces
    Redis --- 安装教程
  • 原文地址:https://blog.csdn.net/m0_49968063/article/details/133985686