设为首页 加入收藏

TOP

8.4.4 重载加法运算符
2013-10-07 12:30:20 来源: 作者: 【 】 浏览:72
Tags:8.4.4 重载 加法 运算

8.4.4  重载加法运算符

让我们看看如何为CBox类重载加法运算符。这是个有趣的问题,因为涉及创建和返回新的对象。新对象是两个操作数-- 即两个CBox对象的和(无论将和的意义定义成什么)。

那么,我们真正希望两个箱子的和是什么?关于这一点有很多合法的可能性,但我们在这里将力求简单。下面这样定义两个CBox对象的和:它是个CBox对象,其体积足够容纳另外两个码在一起的箱子。通过使新对象的m_Length成员等于两个相加对象中较大的m_Length成员,并以类似的方式求出m_Width,然后使m_Height成员等于两个操作数对象m_Height成员的和,我们即可使合成的CBox对象足以包含另两个CBox对象。这种实现方法未必是最优的解决方案,但就我们的目的而言已经足够。通过修改构造函数,我们还将使CBox对象的m_Length成员总是大于或等于m_Width成员。

CBox类的加法运算符版本更容易通过图形来解释,因此我们进一步在图8-4中阐明了相加的过程。

 
(点击查看大图)图  8-4
因为我们需要直接访问对象的成员,所以应该将operator+()实现为成员函数。该函数成员在类定义内部的声明如下所示:
  1. CBox operator+(const CBox& aBox) const;      
    // Function adding two CBox objects 

我们将形参定义成引用,以避免该函数被调用时不必要地复制右边的实参。我们还将形参声明为const引用,因为该函数不修改实参。如果不把形参声明为const引用,则编译器将不允许const对象被传递给这个函数,因此+运算符的右操作数不可以是const CBox对象。我们将该运算符函数也声明为const,因为它不修改调用它的那个对象。如果不这样做,则+运算符的左操作数也不可以是const CBox对象。

operator+()函数的定义现在如下所示:

  1. // Function to add two CBox objects  
  2. CBox CBox::operator+(const CBox& aBox) const  
  3. {  
  4. // New object has larger length and width, and sum of heights  
  5. return CBox(m_Length > aBox.m_Length   m_Length:aBox.m_Length,  
  6. m_Width > aBox.m_Width   m_Width:aBox.m_Width,  
  7. m_Height + aBox.m_Height);  

根据当前对象(*this)和传递的实参对象aBox,我们构造出一个局部的CBox对象。记住,返回过程将创建局部对象的临时副本,并将此副本(而非从函数返回时将被抛弃的局部对象)返回给调用程序。

试一试:练习使用我们重载的加法运算符

在这个示例中,我们将能够看到CBox类中重载加法运算符的工作过程。

  1. // Ex8_06.cpp  
  2. // Adding CBox objects  
  3. #include <iostream>                              
    // For stream I/O  
  4. using std::cout;  
  5. using std::endl;  
  6.  
  7. class CBox                                      

    // Class definition at global scope  
  8. {  
  9. public:  
  10. // Constructor definition  
  11. CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0): m_Height(hv)  
  12. {  
  13. m_Length = lv > wv  lv: wv;                             // Ensure that  
  14. m_Width = wv < lv  wv: lv;                              // length >= width  
  15. }  
  16.  
  17. // Function to calculate the volume of a box  
  18. double Volume() const  
  19. {  
  20. return m_Length*m_Width*m_Height;  
  21. }  
  22.  
  23. // Operator function for 'greater than' which  
  24. // compares volumes of CBox objects.  
  25. int CBox::operator>(const CBox& aBox) const  
  26. {  
  27. return this->Volume() > aBox.Volume();  
  28. }  
  29.  
  30. // Function to compare a CBox object with a constant  
  31. int operator>(const double& value) const  
  32. {  
  33. return Volume() > value;  
  34. }  
  35.  
  36. // Function to add two CBox objects  
  37. CBox operator+(const CBox& aBox) const  
  38. {  
  39. // New object has larger length & width, and sum of heights  
  40. return CBox(m_Length > aBox.m_Length  m_Length:aBox.m_Length,  
  41.   m_Width > aBox.m_Width  m_Width:aBox.m_Width,  
  42.                   m_Height + aBox.m_Height);  
  43. }  
  44.  
  45. // Function to show the dimensions of a box  
  46. void ShowBox() const  
  47. {  
  48. cout << m_Length << " "  
  49. << m_Width << " "  
  50. << m_Height << endl;  
  51. }  
  52.  
  53. private:  
  54. double m_Length;                               
    // Length of a box in inches  
  55. double m_Width;                                    
    // Width of a box in inches  
  56. double m_Height;                                
    // Height of a box in inches  
  57. };  
  58.  
  59. int operator>(const double& value, const CBox& 
    aBox); // Function prototype  
  60.  
  61. int main()  
  62. {  
  63. CBox smallBox(4.0, 2.0, 1.0);  
  64. CBox mediumBox(10.0, 4.0, 2.0);  
  65. CBox aBox;  
  66. CBox bBox;  
  67.  
  68. aBox = smallBox + mediumBox;  
  69. cout << "aBox dimensions are ";  
  70. aBox.ShowBox();  
  71.  
  72. bBox = aBox + smallBox + mediumBox;  
  73. cout << "bBox dimensions are ";  
  74. bBox.ShowBox();  
  75.  
  76. return 0;  
  77. }  
  78.  
  79. // Function comparing a constant with a CBox object  
  80. int operator>(const double& value, const CBox& aBox)  
  81. {  
  82. return value > aBox.Volume();  

在本章后面,我们将再次用到CBox类的定义。因此现在请在书上作好记号,稍后可能需要返回到这里。

示例说明

在该示例中,我们稍微对CBox类的成员作了一些修改。析构函数已经被删除,因为对CBox类而言它不是必需的。构造函数已经被修改成可以保证m_Length成员不小于m_Width成员。知道箱子的长度无论如何也不会小于宽度,将使加法操作更容易些。我们还添加了一个输出CBox对象尺寸的ShowBox()函数。使用该函数,我们将能够验证重载的加法操作是否像预期的那样工作。

该程序的输出如下:

  1. aBox dimensions are 10 4 3  
  2. bBox dimensions are 10 4 6 

该输出看来与前面定义的使CBox对象相加的概念一致。另外可以看出,该函数还可以处理表达式中的多次加法操作。为了计算bBox,重载的加法运算符被调用了两次。

我们也能够以友元函数的形式实现CBox类的加法操作,其原型如下:

  1. friend CBox operator+(const CBox& aBox, const CBox& bBox); 

除需要使用直接成员选择运算符获得函数两个实参的成员以外,得出结果的过程将完全相同。该版本的运算符函数将像第一个版本一样工作。

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇8.5.2 根据类模板创建对象 下一篇8.4.3 重载赋值运算符

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: