设为首页 加入收藏

TOP

8.6.3 实现CBox类(7)
2013-10-07 12:29:59 来源: 作者: 【 】 浏览:70
Tags:8.6.3 实现 CBox

8.6.3  实现CBox类(7)

我们可以用自己喜欢的方式编辑或重新整理这里的代码。笔者添加了几个空行,以使代码看起来更清晰一些。

Box.cpp文件的内容最终应该如下所示:

  1. #include ".\box.h"  
  2.  
  3. CBox::CBox(double lv, double wv, double hv)  
  4. {  
  5. lvlv = lv <= 0.0   1.0 : lv;  
  6.     // Ensure positive  
  7. wvwv = wv <= 0.0   1.0 : wv;  
  8.     // dimensions for  
  9. hvhv = hv <= 0.0   1.0 : hv;  
  10.     // the object  
  11.  
  12. m_Length = lv>wv   lv : wv;  
  13. // Ensure that  
  14. m_Width = wv<lv   wv : lv;  
  15.     // length >= width  
  16. m_Height = hv;  
  17. }  
  18.  
  19. CBox::~CBox(void)  
  20. {  
  21. }  
  22.  
  23. // Overloaded addition operator  
  24. CBox CBox::operator+(const CBox& aBox) const  
  25. {  
  26. // New object has larger length and width of the two,  
  27. // and sum of the two heights  
  28. return CBox(m_Length > aBox.m_Length   m_Length : aBox.m_Length,  
  29. m_Width > aBox.m_Width   m_Width : aBox.m_Width,  
  30. m_Height + aBox.m_Height);  
  31. }  
  32.  
  33. // Multiply a box by an integer  
  34. CBox CBox::operator*(int n) const  
  35. {  
  36. if(n%2)  
  37. return CBox(m_Length, m_Width, n*m_Height); // n odd  
  38. else  
  39. return CBox(m_Length, 2.0*m_Width, (n/2)*m_Height); // n even  
  40. }  
  41.  
  42. // Divide one box into another  
  43. int CBox::operator/(const CBox& aBox) const  
  44. {  
  45. // Temporary for number in horizontal plane this way  
  46. int tc1 = 0;  
  47. // Temporary for number in a plane that way  
  48. int tc2 = 0;  
  49.  
  50. tc1 = static_cast<int>((m_Length/aBox.m_Length))*  
  51.    static_cast<int>((m_Width/aBox.m_Width));        
    // to fit this way  
  52.  
  53. tc2 = static_cast<int>((m_Length/aBox.m_Width))*  
  54.  static_cast<int>((m_Width/aBox.m_Length));      
    // and that way  
  55.  
  56. //Return best fit  
  57. return static_cast<int>((m_Height/aBox.m_Height))*  
  58.    (tc1>tc2   tc1 : tc2);  

带阴影的代码是应该修改或手工添加的代码。

几个非常短的函数,具体地说,是几个仅仅返回数据成员数值的函数,其定义在类定义内部,因此属于内联函数。如果单击Class View选项卡,然后再单击CBox类名旁边的+号,我们将看到该类的所有成员都显示在底部窗格中。

至此CBox类的定义就结束了,但是为了将CBox对象的体积与数值进行比较,我们还需要定义几个实现运算符的全局函数。

添加全局函数

我们还需要创建一个.cpp文件,来包含那些处理CBox对象的全局函数的定义,该文件也需要成为本项目的组成部分。单击Solution Explorer选项卡显示解决方案浏览器(当前显示的应该是Class View选项卡),并右击Source Files文件夹。然后从上下文菜单中选择Add | New Item…,之后将显示出添加新文件对话框。将种类选择成Code,并在对话框右窗格中将模板选择成C++(www.cppentry.com) File (.cpp),然后输入文件名BoxOperators。

现在,我们可以在编辑器窗格中输入下面的代码:

  1. // BoxOperators.cpp  
  2. // CBox object operations that don't need to access private members  
  3. #include "Box.h"  
  4.  
  5. // Function for testing if a constant is > a CBox object  
  6. bool operator>(const double& value, const CBox& aBox)  
  7. { return value > aBox.Volume(); }  
  8.  
  9. // Function for testing if a constant is < CBox object  
  10. bool operator<(const double& value, const CBox& aBox)  
  11. { return value < aBox.Volume(); }  
  12.  
  13. // Function for testing if CBox object is > a constant  
  14. bool operator>(const CBox& aBox, const double& value)  
  15. { return value < aBox; }  
  16.  
  17. // Function for testing if CBox object is < a constant  
  18. bool operator<( const CBox& aBox, const double& value)  
  19. { return value > aBox; }  
  20.  
  21. // Function for testing if a constant is >= a CBox object  
  22. bool operator>=(const double& value, const CBox& aBox)  
  23. { return value >= aBox.Volume(); }  
  24.  
  25. // Function for testing if a constant is <= CBox object  
  26. bool operator<=(const double& value, const CBox& aBox)  
  27. { return value <= aBox.Volume(); }  
  28.  
  29. // Function for testing if CBox object is >= a constant  
  30. bool operator>=( const CBox& aBox, const double& value)  
  31. { return value <= aBox; }  
  32.  
  33. // Function for testing if CBox object is <= a constant  
  34. bool operator<=( const CBox& aBox, const double& value)  
  35. { return value >= aBox; }  
  36.  
  37. // Function for testing if a constant is == CBox object  
  38. bool operator==(const double& value, const CBox& aBox)  
  39. { return value == aBox.Volume(); }  
  40.  
  41. // Function for testing if CBox object is == a constant  
  42. bool operator==(const CBox& aBox, const double& value)  
  43. { return value == aBox; }  
  44.  
  45. // CBox multiply operator n*aBox  
  46. CBox operator*(int n, const CBox& aBox)  
  47. { return aBox * n; }  
  48.  
  49. // Operator to return the free volume in a packed CBox  
  50. double operator%( const CBox& aBox, const CBox& bBox)  
  51. { return aBox.Volume() - (aBox / bBox) * bBox.Volume(); } 

上面的代码中有一条嵌入Box.h文件的#include指令,因为这些函数要引用CBox类。保存该文件。完成这些代码的输入之后,我们可以选择Class View选项卡。Class View选项卡现在包括Global Functions和Variables文件夹,其中包含刚才添加的所有函数。

我们在前面已经看过所有这些函数的定义,因此这里不再讨论。如果希望在另一个.cpp文件中使用这些函数,则必须以编译器能够识别的方式声明所有要使用的函数。为此,我们可以将一组声明语句放入某个头文件中。再次切换到Solution Explorer窗格,右击Header Files文件夹,从上下文菜单中选择Add | New Item…,显示出添加新文件对话框。这次将种类选择成Code,将模板选择成Header File(.h),然后输入文件名BoxOperators。在单击Add按钮之后,一个空的头文件将被添加到本项目中,随后我们可以在Editor窗口中添加下面的代码:

  1. // BoxOperators.h - Declarations for global box operators  
  2. #pragma once  
  3.  
  4. bool operator>(const double& value, const CBox& aBox);  
  5. bool operator<(const double& value, const CBox& aBox);  
  6. bool operator>(const CBox& aBox, const double& value);  
  7. bool operator<(const CBox& aBox, const double& value);  
  8. bool operator>=(const double& value, const CBox& aBox);  
  9. bool operator<=(const double& value, const CBox& aBox);  
  10. bool operator>=(const CBox& aBox, const double& value);  
  11. bool operator<=(const CBox& aBox, const double& value);  
  12. bool operator==(const double& value, const CBox& aBox);  
  13. bool operator==(const CBox& aBox, const double& value);  
  14. CBox operator*(int n, const CBox aBox);  
  15. double operator%(const CBox& aBox, const CBox& bBox); 

#pragma once指令将确保本文件的内容在编译过程中只能被嵌入一次。重要的是要将这个指令放在所有头文件中,因为人们很容易不小心多次嵌入头文件。如果最终向源文件中嵌入了多次头文件,那么我们就会在源文件中对同一个事物进行多次定义,从而造成代码无法编译。如果有哪个源文件要使用这些函数,我们只需添加一条嵌入BoxOperators.h的#include指令即可。

现在,我们可以着手将这些函数和CBox类应用到箱子的问题上来。

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇8.4.3 重载赋值运算符 下一篇4.4.1 引用的概念

评论

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