设为首页 加入收藏

TOP

8.9.3 实现CBox类(1)
2013-10-07 16:08:42 来源: 作者: 【 】 浏览:63
Tags:8.9.3 实现 CBox

8.9.3  实现CBox类(1)

我们实际上需要考虑希望嵌入CBox类内部的错误防护程度。为说明类的各个方面而定义的基本的CBox类可以作为一个始点,但还应该更深入地考虑其他几点。构造函数不够完善,因为不能确保CBox对象的尺寸有效,因此可能首先应该确保始终获得有效的对象。为此,可以重新定义基本的CBox类:

  1. class CBox                                                                                                  // Class definition at global scope  
  2. {  
  3. public:  
  4. // Constructor definition  
  5. explicit CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0): m_Height(hv)  
  6. {  
  7. if(lv < 0.0 || wv < 0.0 || hv < 0.0)  
  8. throw "Negative dimension specified for CBox object.";  
  9. m_Length = std::max(lv, wv);  
  10. m_Width = std::min(lv, wv);  
  11. // Length is now greater than or equal to width  
  12. if(m_Height > m_Length)  
  13. {  
  14. m_Height = m_Length;  
  15. m_Length = hv;  
  16. // m_Height is still greater than m_Width so swap them  
  17. double temp = m_Width;  
  18. m_Width = m_Height;  
  19. m_Height = temp;  
  20. }  
  21. else if( m_Height > m_Width)  
  22. {  
  23. m_Height = m_Width;  
  24. m_Width = hv;  
  25. }  
  26. }  
  27. // Function to calculate the volume of a box  
  28. double Volume() const  
  29. {  
  30. return m_Length*m_Width*m_Height;  
  31. }  
  32. // Function providing the length of a box  
  33. double GetLength() const { return m_Length; }  
  34. // Function providing the width of a box  
  35. double GetWidth() const { return m_Width; }  
  36. // Function providing the height of a box  
  37. double GetHeight() const { return m_Height; }  
  38. private:  
  39. double m_Length;                                                                    // Length of a box in inches  
  40. double m_Width;                                                                     // Width of a box in inches  
  41. double m_Height;                                                                    // Height of a box in inches  
  42. };  

构造函数现在是可靠的,因为在构造函数中将任何被用户设置成小于0的尺寸都会抛出一个异常。

该类的默认复制构造函数和默认赋值运算符是令人满意的,因为我们不需要给数据成员动态分配内存。在该情况下,默认析构函数同样工作得很好,因此不需要定义它。现在,应该考虑要支持类对象的比较功能都需要做些什么。

1. 比较CBox对象

我们应该包括对>、>=、==、<和<=运算符的支持,使它们能够处理两个操作数都是CBox对象的情况,还能处理一个操作数是CBox对象、另一个操作数是double类型数值的情况。这样总共有18个运算符函数。一旦定义了<和==运算符函数,就可以从utility头文件的模板中得到4个带双操作数的运算符函数,这些都在Ex8_06中完成了:
 

  1. // Operator function for < comparing CBox objects  
  2. bool operator<(const CBox& aBox) const  
  3. {       return this->Volume() < aBox.Volume(); }  
  4. // Operator function for == comparing CBox objects  
  5. bool operator==(const CBox& aBox) const  
  6. {       return this->Volume() == aBox.Volume(); }  

比较左操作数是CBox对象、右操作数是一个常量的所有4个运算符函数可以放在类中:
 

  1. // Function for testing if a CBox object is > a value  
  2. bool operator>(const double& value) const  
  3. {       return Volume() > value; }  
  4. // Function for testing if a CBox object is < a value  
  5. bool operator<(const double& value) const  
  6. {       return Volume() < value; }  
  7. // Function for testing if a CBox object is >= a value  
  8. bool operator>=(const double& value) const  
  9. {       return Volume() >= value; }  
  10. // Function for testing if a CBox object is <= a value  
  11. bool operator<=(const double& value) const  
  12. {       return Volume() <= value; }  
  13. // Function for testing if a CBox object is == a value  
  14. bool operator==(const double& value) const  
  15. {       return Volume() == value; }  
  16. // Function for testing if a CBox object is != a value  
  17. bool operator!=(const double& value) const  
  18. {       return Volume() != value; }  

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇8.9.2 定义问题 下一篇8.9.3 实现CBox类(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)