设为首页 加入收藏

TOP

8.4.1 实现重载的运算符(2)
2013-10-07 16:07:59 来源: 作者: 【 】 浏览:51
Tags:8.4.1 实现 重载 运算

8.4.1  实现重载的运算符(2)

试一试:运算符重载

可以通过如下示例练习如何使用CBox对象的operator<()函数:

  1. // Ex8_03.cpp  
  2. // Exercising the overloaded 'less than' operator and equality operators  
  3. #include <iostream>                                     // For stream I/O  
  4. using std::cout;  
  5. using std::endl;  
  6. class CBox                                            // Class definition at global scope  
  7. {  
  8. public:  
  9. // Constructor definition  
  10. explicit CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0):  
  11. m_Length(lv), m_Width(wv), m_Height(hv)  
  12. {  
  13. cout << endl << "Constructor called.";  
  14. }  
  15. // Function to calculate the volume of a box  
  16. double Volume() const  
  17. {  
  18. return m_Length*m_Width*m_Height;  
  19. }  
  20. bool operator < (const CBox& aBox) const;       // Overloaded 'less than'  
  21. // Overloaded equality operator  
  22. bool operator==(const CBox& aBox) const  
  23. {  
  24. return this->Volume() == aBox.Volume();  
  25. }  
  26. // Destructor definition  
  27. ~CBox()  
  28. {  
  29. cout << "Destructor called." << endl;  
  30. }  
  31. private:  
  32. double m_Length;                                  // Length of a box in inches  
  33. double m_Width;                                   // Width of a box in inches  
  34. double m_Height;                                  // Height of a box in inches  
  35. };  
  36. // Operator function for 'less than' that  
  37. // compares volumes of CBox objects.  
  38. inline bool CBox::operator < (const CBox& aBox) const  
  39. {  
  40. return this->Volume() < aBox.Volume();  
  41. }  
  42. int main()  
  43. {  
  44. CBox smallBox(4.0, 2.0, 1.0);  
  45. CBox mediumBox(10.0, 4.0, 2.0);   
  46. CBox bigBox(30.0, 20.0, 40.0);  
  47. CBox thatBox(4.0, 2.0, 10.0);  
  48. if(mediumBox < smallBox)  
  49. cout << endl << "mediumBox is smaller than smallBox";  
  50. if(mediumBox < bigBox)  
  51. cout << endl << "mediumBox is smaller than bigBox";  
  52. else cout << endl << "mediumBox is not bigger than bigBox";  
  53. if(thatBox == mediumBox)  
  54. cout << endl << “thatBox is equal to mediumBox”;  
  55. else  
  56. cout << endl << “thatBox is not equal to mediumBox”;  
  57. cout << endl;  
  58. return 0;  
  59. }  

示例说明

operator<()运算符函数的原型出现在类的public部分。由于函数定义在类定义外部,因此该函数默认不是内联函数,所以显式指定它。通常情况下最好把简单的演算法函数设置为内联的,这样代码运行速度会更快。把函数定义放在类定义外部的唯一原因是演示这种可能性。完全可以将函数定义放在类定义中,就像operator==()函数那样。这种情况下将不需要指定inline或者在函数名前面用CBox加以限定。我们记得,当函数成员在类定义外部定义时,为了告诉编译器该函数是CBox类的成员,必须用类名进行限定。

main()函数中有两条对类成员使用<运算符的if语句,它们将自动调用重载的运算符函数。如果想确认这一点,那么可以给运算符函数添加一条输出语句。此外还有一条使用==运算符的语句。该示例的输出如下:
 

  1. Constructor called.  
  2. Constructor called.  
  3. Constructor called.  
  4. Constructor called.  
  5. mediumBox is smaller than bigBox  
  6. thatBox is equal to mediumBox  
  7. Destructor called.  
  8. Destructor called.  
  9. Destructor called.  
  10. Destructor called.  

输出证实,使用运算符函数的if语句工作正常,重载的运算符可用于const和非const对象。因此直接用CBox对象表示CBox问题的解决方案开始成为很现实的命题。
 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇8.4.1 实现重载的运算符(1) 下一篇8.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)