8.6.3 实现CBox类(7)
我们可以用自己喜欢的方式编辑或重新整理这里的代码。笔者添加了几个空行,以使代码看起来更清晰一些。
Box.cpp文件的内容最终应该如下所示:
- #include ".\box.h"
-
- CBox::CBox(double lv, double wv, double hv)
- {
- lvlv = lv <= 0.0 1.0 : lv;
- // Ensure positive
- wvwv = wv <= 0.0 1.0 : wv;
- // dimensions for
- hvhv = hv <= 0.0 1.0 : hv;
- // the object
-
- m_Length = lv>wv lv : wv;
- // Ensure that
- m_Width = wv<lv wv : lv;
- // length >= width
- m_Height = hv;
- }
-
- CBox::~CBox(void)
- {
- }
-
- // Overloaded addition operator
- CBox CBox::operator+(const CBox& aBox) const
- {
- // New object has larger length and width of the two,
- // and sum of the two heights
- return CBox(m_Length > aBox.m_Length m_Length : aBox.m_Length,
- m_Width > aBox.m_Width m_Width : aBox.m_Width,
- m_Height + aBox.m_Height);
- }
-
- // Multiply a box by an integer
- CBox CBox::operator*(int n) const
- {
- if(n%2)
- return CBox(m_Length, m_Width, n*m_Height); // n odd
- else
- return CBox(m_Length, 2.0*m_Width, (n/2)*m_Height); // n even
- }
-
- // Divide one box into another
- int CBox::operator/(const CBox& aBox) const
- {
- // Temporary for number in horizontal plane this way
- int tc1 = 0;
- // Temporary for number in a plane that way
- int tc2 = 0;
-
- tc1 = static_cast<int>((m_Length/aBox.m_Length))*
- static_cast<int>((m_Width/aBox.m_Width));
// to fit this way -
- tc2 = static_cast<int>((m_Length/aBox.m_Width))*
- static_cast<int>((m_Width/aBox.m_Length));
// and that way -
- //Return best fit
- return static_cast<int>((m_Height/aBox.m_Height))*
- (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。
现在,我们可以在编辑器窗格中输入下面的代码:
- // BoxOperators.cpp
- // CBox object operations that don't need to access private members
- #include "Box.h"
-
- // Function for testing if a constant is > a CBox object
- bool operator>(const double& value, const CBox& aBox)
- { return value > aBox.Volume(); }
-
- // Function for testing if a constant is < CBox object
- bool operator<(const double& value, const CBox& aBox)
- { return value < aBox.Volume(); }
-
- // Function for testing if CBox object is > a constant
- bool operator>(const CBox& aBox, const double& value)
- { return value < aBox; }
-
- // Function for testing if CBox object is < a constant
- bool operator<( const CBox& aBox, const double& value)
- { return value > aBox; }
-
- // Function for testing if a constant is >= a CBox object
- bool operator>=(const double& value, const CBox& aBox)
- { return value >= aBox.Volume(); }
-
- // Function for testing if a constant is <= CBox object
- bool operator<=(const double& value, const CBox& aBox)
- { return value <= aBox.Volume(); }
-
- // Function for testing if CBox object is >= a constant
- bool operator>=( const CBox& aBox, const double& value)
- { return value <= aBox; }
-
- // Function for testing if CBox object is <= a constant
- bool operator<=( const CBox& aBox, const double& value)
- { return value >= aBox; }
-
- // Function for testing if a constant is == CBox object
- bool operator==(const double& value, const CBox& aBox)
- { return value == aBox.Volume(); }
-
- // Function for testing if CBox object is == a constant
- bool operator==(const CBox& aBox, const double& value)
- { return value == aBox; }
-
- // CBox multiply operator n*aBox
- CBox operator*(int n, const CBox& aBox)
- { return aBox * n; }
-
- // Operator to return the free volume in a packed CBox
- double operator%( const CBox& aBox, const CBox& bBox)
- { 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窗口中添加下面的代码:
- // BoxOperators.h - Declarations for global box operators
- #pragma once
-
- bool operator>(const double& value, const CBox& aBox);
- bool operator<(const double& value, const CBox& aBox);
- bool operator>(const CBox& aBox, const double& value);
- bool operator<(const CBox& aBox, const double& value);
- bool operator>=(const double& value, const CBox& aBox);
- bool operator<=(const double& value, const CBox& aBox);
- bool operator>=(const CBox& aBox, const double& value);
- bool operator<=(const CBox& aBox, const double& value);
- bool operator==(const double& value, const CBox& aBox);
- bool operator==(const CBox& aBox, const double& value);
- CBox operator*(int n, const CBox aBox);
- double operator%(const CBox& aBox, const CBox& bBox);
#pragma once指令将确保本文件的内容在编译过程中只能被嵌入一次。重要的是要将这个指令放在所有头文件中,因为人们很容易不小心多次嵌入头文件。如果最终向源文件中嵌入了多次头文件,那么我们就会在源文件中对同一个事物进行多次定义,从而造成代码无法编译。如果有哪个源文件要使用这些函数,我们只需添加一条嵌入BoxOperators.h的#include指令即可。
现在,我们可以着手将这些函数和CBox类应用到箱子的问题上来。