设为首页 加入收藏

TOP

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

8.9.3  实现CBox类(8)

可以用自己喜欢的方式编辑或重新整理这里的代码,当然,代写必须编写正确。

如果单击Class View选项卡后再单击CBox类名,那么该类的所有成员都将显示在底部窗格中。

至此CBox类的成员就介绍完了,但是为了将CBox对象的体积与数值进行比较,还需要定义几个实现运算符的全局函数。它们是短小而高效的内联函数。

添加全局函数

您可能以为,应像其他函数定义那样把内联函数的定义放在.cpp文件中,但其实并非如此。在编译好的代码中,内联函数并不是"真正"的函数,因为编译器会在调用内联函数的地方插入内联函数体的代码。在编译包含调用内联函数的文件时,内联函数的代码必须是可用的,否则就会出现连接错误,程序就不会运行。包含在.cpp文件中的.h文件必须包含编译器编译.cpp文件中代码所需的所有内容。如果在.cpp文件中调用了内联函数,内联函数的定义就必须出现在.h文件中,而该.h文件必须包含在.cpp文件中。这也适用于在类定义外部定义的内联成员函数。

支持CBox对象操作的全局函数都是内联函数。可以在Box.h中把支持CBox对象操作的全局函数放在CBox类定义的后面,但为了获得经验,把另一个.h文件添加到项目中以包含它们。单击Solution Explorer选项卡显示解决方案浏览器(当前显示的应该是Class View选项卡),并右击Header Files文件夹。然后从上下文菜单中选择Add | New Item菜单项,以显示对话框。将种类选择成Code,并在对话框右窗格中将模板选择成Header File (.h),然后输入文件名BoxOperators。

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

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

#pragma once指令将确保在编译过程中只能嵌入一次本文件的内容。上面的代码中有一条嵌入Box.h文件的#include指令,因为这些函数要引用CBox类。保存该文件。完成这些代码的输入之后,可以选择Class View选项卡。Class View选项卡现在包括Global Functions and Variables文件夹,其中包含刚才添加的所有函数。前面已经介绍过所有这些函数的定义,因此这里不再讨论。

当在一个非内联函数的项目中定义全局函数时,可以把它们的定义放在一个.cpp文件中。还需要把这些函数的原型放在一个开头包含#pragma once指令的.h文件中。接着把这个.h文件包含到调用任意全局函数的.cpp文件中,这样编译器就知道它们是什么。

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

使用CBox类

假设我们在包装糖果。这些糖果都在破碎机上各占用1.5英寸长、1英寸宽、1英寸高的空间。可以使用4.5英寸长、7英寸宽、2英寸高的标准糖果盒,现在想知道盒中能够容纳多少块糖果,以便制定价格。还有一种标准的纸板箱,长2英尺6英寸、宽18英寸、深18英寸。我们想知道该纸板箱可以容纳多少个糖果盒,装满之后有多大的未用空间。

万一标准糖果盒不是合适的解决方案,我们还想知道定制多大尺寸的糖果盒才合适。如果糖果盒的长度在3~7英寸之间,宽度在3~5英寸之间,高度在1~2.5英寸之间(这些尺寸可以以半英寸为步距变化),就能卖个好价钱。糖果盒中至少需要30块糖果,因为这是绝大多数顾客一次消费的最低数量。另外,糖果盒不应该有剩余空间,那样将使消费者感到上当受骗。理想情况下我们还希望纸板箱塞满,这样糖果盒将不会晃荡。我们也不希望包装得太紧,那样将增加包装的难度,因此如果纸板箱的剩余空间小于一个糖果盒的体积,就可以说没有浪费空间。

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇8.9.3 实现CBox类(7) 下一篇8.9.3 实现CBox类(9)

评论

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

·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)