设为首页 加入收藏

TOP

8.4.4 重载加法运算符(1)
2013-10-07 16:07:43 来源: 作者: 【 】 浏览:53
Tags:8.4.4 重载 加法 运算

8.4.4  重载加法运算符(1)

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

那么,我们真正希望两个箱子的和是什么?关于这一点有很多合法的可能性,但我们在这里将力求简单。下面这样定义两个CBox对象的和:它是个CBox对象,其体积足够容纳这两个摞在一起的箱子。理想情况下,可通过把两个箱子的最短尺寸合并起来将它们连接起来。为此,可以确保箱子的长度总是大于或等于宽度,且箱子的宽度总是大于或等于高度。接着使新对象的m_Length成员等于两个相加对象中较大的m_Length成员,并以类似的方式求出m_Width成员,然后使m_Height成员等于两个操作数对象的m_Height成员的和,即可使合成的CBox对象足以包含这两个CBox对象。这种实现方法未必是最优的解决方案,因为两个箱子可以绕着高度轴旋转,得到更有效的合并方式,但就我们的目的而言已经足够。通过修改构造函数,还将使CBox对象的长度、宽度和高度按降序排列。

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

因为需要访问CBox对象的私有成员,所以应该将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);  
  8. }  

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

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

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

  1. // Ex8_06.cpp  
  2. // Adding CBox objects  
  3. #include <iostream>                                                                                             // For stream I/O  
  4. #include <utility>                                                                                                  // For operator overload templates  
  5. using std::cout;  
  6. using std::endl;  
  7. using namespace std::rel_ops;  
  8. class CBox                                                                                                                              // Class definition at global scope  
  9. {  
  10. public:  
  11. // Constructor definition  
  12. explicit CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0): m_Height(hv)  
  13. {  
  14. m_Length = std::max(lv, wv);  
  15. m_Width = std::min(lv, wv);  
  16. // Length is now greater than or equal to width  
  17. if(m_Height > m_Length)  
  18. {  
  19. m_Height = m_Length;  
  20. m_Length = hv;  
  21. // m_Height is still greater than m_Width so swap them  
  22. double temp = m_Width;  
  23. m_Width = m_Height;  
  24. m_Height = temp;  
  25. }  
  26. else if( m_Height > m_Width)  
  27. {  
  28. m_Height = m_Width;  
  29. m_Width = hv;  
  30. }  
  31. }  
  32. // Function to calculate the volume of a box  
  33. double Volume() const  
  34. {  
  35. return m_Length*m_Width*m_Height;  
  36. }  
  37. // Operator function for 'less than' that  
  38. // compares volumes of CBox objects.  
  39. bool operator<(const CBox& aBox) const  
  40. {  
  41. return this->Volume() < aBox.Volume();  
  42. }  
  43. // 'Less than' operator function to compare a CBox object volume with a constant  
  44. bool operator<(const double& value) const  
  45. {  
  46. return this->Volume() < value;  
  47. }  
  48. // 'Greater than' function to compare a CBox object volume with a constant  
  49. bool operator>(const double& value) const  
  50. {  
  51. return this->Volume() > value;  
  52. }  
  53. // Overloaded equality operator  
  54. bool operator==(const CBox& aBox) const  
  55. {  
  56. return this->Volume() == aBox.Volume();  
  57. }  
  58. // Function to add two CBox objects  
  59. CBox operator+(const CBox& aBox) const  
  60. {  
  61. // New object has larger length & width, and sum of heights  
  62. return CBox(m_Length > aBox.m_Length   m_Length : aBox.m_Length,  
  63. m_Width > aBox.m_Width   m_Width : aBox.m_Width,  
  64. m_Height + aBox.m_Height);  
  65. }  
  66. // Function to show the dimensions of a box  
  67. void ShowBox() const  
  68. {  
  69. cout << m_Length << " " << m_Width << " " << m_Height << endl;  
  70. }  
  71. private:  
  72. double m_Length;                                                                        // Length of a box in inches  
  73. double m_Width;                                                                         // Width of a box in inches  
  74. double m_Height;                                                                        // Height of a box in inches  
  75. };  
  76. // Function comparing a constant with a CBox object  
  77. inline bool operator>(const double& value, const CBox& aBox)  
  78. {  
  79. return value > aBox.Volume();  
  80. }  
  81. int main()  
  82. {  
  83. CBox smallBox(4.0, 2.0, 1.0);  
  84. CBox mediumBox(10.0, 4.0, 2.0);  
  85. CBox aBox;  
  86. CBox bBox;  
  87. cout << "smallBox dimensions are ";  
  88. smallBox.ShowBox();  
  89. cout << "mediumBox dimensions are ";  
  90. mediumBox.ShowBox();  
  91. aBox = smallBox + mediumBox;  
  92. cout << "aBox = smallBox + mediumBox. Dimensions are "  
  93. aBox.ShowBox();  
  94. bBox = aBox + smallBox + mediumBox;  
  95. cout << "bBox = aBox + smallBox + mediumBox. Dimensions are "  
  96. bBox.ShowBox();  
  97. return 0;  
  98. }  

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇8.4.3 重载赋值运算符(3) 下一篇8.4.4 重载加法运算符(2)

评论

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

·python数据分析岗的 (2025-12-25 10:02:21)
·python做数据分析需 (2025-12-25 10:02:19)
·成为一个优秀的pytho (2025-12-25 10:02:16)
·Java后端面试实习自 (2025-12-25 09:24:21)
·Java LTS版本有哪些 (2025-12-25 09:24:18)