当前位置:   article > 正文

算术运算符重载——返回const对象的普通函数重载算术运算符

返回const对象的普通函数重载算术运算符

任务描述

本关与第一关一模一样,只是运算符重载的返回类型使用了 const 作为修饰。至于为什么要使用 const 修饰,留给大家自行查找原因。

原则上,像算术运算符这样的运算符重载,倾向于使用普通函数进行重载。

相关知识

为什么算术运算符重载的返回类型要使用 const 修饰呢?因为如果不使用 const 修饰,如下代码就有可能通过编译。

    1. Int a,b,c;
    2. a + b = c;

如上代码通过编译会造成什么后果呢?什么后果也没有。但是再进一步就有可能了。

  1. if ( a + b = c ){...}

在表示条件判断的表达式中,把“等于号运算符”误写成“赋值运算符”,应该是每个 C/C++ 程序员犯过的错误。更糟糕的是,编译器不会认为这是错误。这是一个极其危险的错误。
当然,要达成这一“编译器不认为是错误”的错误,还需 2 个条件:第一,加号运算符返回的不是 const;第二,赋值运算符的返回类型能够经过一次转型转为 bool 类型。
所以,你现在理解为什么最好加上 const 了吗?

编程要求

根据提示,在右侧编辑器的Begin-End区域内补充代码。

测试说明

本关共3个文件,Int.h、Int.cpp 和 main.cpp。其中 Int.h 和 main.cpp 不得改动,用户只能修改 Int.cpp 中的内容。

Int.h 内容如下:

    1. /**
    2. * 这是一个包装类(wrapper class),包装类在C++中有点小小的用处(基本上没用),在Java中的用处更大一些。
    3. */
    4. #ifndef _INT_H_ //这是define guard
    5. #define _INT_H_ //在C和C++中,头文件都应该有这玩意
    6. class Int{
    7. private://这是访问控制——私有的
    8. int value; //这是数据成员,我们称Int是基本类型int的包装类,就是因为Int里面只有一个int类型的数据成员
    9. public: //这是公有的
    10. Int():value(0){}
    11. Int(Int const&rhs):value(rhs.value){}
    12. Int(int v):value(v){}
    13. int getValue()const{return value;}
    14. void setValue(int v){value=v;}
    15. };//记住这里有一个分号
    16. //算术运算符重载
    17. const Int operator + (Int const&lhs,Int const&rhs);
    18. const Int operator - (Int const&lhs,Int const&rhs);
    19. const Int operator * (Int const&lhs,Int const&rhs);
    20. const Int operator / (Int const&lhs,Int const&rhs);
    21. const Int operator % (Int const&lhs,Int const&rhs);
    22. #endif

main.cpp 内容如下:

    1. #include "Int.h"
    2. #include <iostream>
    3. using namespace std;
    4. int main(){
    5. int x,y;
    6. cin>>x>>y;
    7. Int a(x),b(y);
    8. Int c,d,e,f,g;
    9. c = a + b;
    10. d = a - b;
    11. e = a * b;
    12. f = a / b;
    13. g = a % b;
    14. cout<<c.getValue()<<" "
    15. <<d.getValue()<<" "
    16. <<e.getValue()<<" "
    17. <<f.getValue()<<" "
    18. <<g.getValue()<<endl;
    19. return 0;
    20. }

 

 

  1. /********* Begin **********/
  2. #include"Int.h"
  3. #include<iostream>
  4. using namespace std;
  5. const Int operator+(Int const&lhs,Int const&rhs)
  6. {
  7. Int m;
  8. m.setValue(lhs.getValue()+rhs.getValue());
  9. return m;
  10. }
  11. const Int operator-(Int const&lhs,Int const&rhs)
  12. {
  13. Int m;
  14. m.setValue(lhs.getValue()-rhs.getValue());
  15. return m;
  16. }
  17. const Int operator*(Int const&lhs,Int const&rhs)
  18. {
  19. Int m;
  20. m.setValue(lhs.getValue()*rhs.getValue());
  21. return m;
  22. }
  23. const Int operator/(Int const&lhs,Int const&rhs)
  24. {
  25. Int m;
  26. m.setValue(lhs.getValue()/rhs.getValue());
  27. return m;
  28. }
  29. const Int operator%(Int const&lhs,Int const&rhs)
  30. {
  31. Int m;
  32. m.setValue(lhs.getValue()%rhs.getValue());
  33. return m;
  34. }
  35. /********* End ************/

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/838207
推荐阅读
相关标签
  

闽ICP备14008679号