设为首页 加入收藏

TOP

8.9.1 在数值类中重载运算符(1)
2013-10-07 12:36:23 来源: 作者: 【 】 浏览:61
Tags:8.9.1 数值 重载 运算

8.9.1  在数值类中重载运算符(1)

首先定义一个以英尺和英寸表示长度的类,然后以此为基础,示范如何为数值类实现运算符重载。加法运算符似乎是个不错的起点,下面就是包括加法运算符函数的数值类Length:

  1. value class Length  
  2. {  
  3. private:  
  4. int feet; // Feet component  
  5. int inches;             // Inches component  
  6.  
  7. public:  
  8. static initonly int inchesPerFoot = 12;  
  9.  
  10. // Constructor  
  11. Length(int ft, int ins) : feet(ft), inches(ins){ }  
  12.  
  13. // A length as a string  
  14. virtual String^ ToString() override  
  15. { return feet+L" feet " + inches + L" inches"; }  
  16.  
  17. // Addition operator  
  18. Length operator+(Length len)  
  19. {  
  20. int inchTotal = inches+len.inches+inchesPerFoot*(feet+len.feet);  
  21. return Length(inchTotal/inchesPerFoot, inchTotal%inchesPerFoot);  
  22. }  
  23. }; 

常量inchesPerFoot是静态的,因此可以直接被类的静态和非静态函数成员使用。我们将inchesPerFoot声明为initonly意味着该常量不能被修改,因此可以是类的公有成员。我们还为Length类定义了重写的ToString()函数,这样就可以使用Console::WriteLine()函数将Length对象输出到命令行上。operator+()函数的实现非常简单。该函数返回一个通过组合当前对象和形参len的feet和inches成员而得到的新Length对象。计算的过程首先合并两个长度,结果以英寸为单位,然后根据合并长度得到的英寸数,为新对象计算Length类构造函数的实参。

下面的代码段可以练习新的加法运算符函数的使用:

  1. Length len1 = Length(6, 9);  
  2. Length len2 = Length(7, 8);  
  3. Console::WriteLine(L"{0} plus {1} is {2}", len1, len2, len1+len2); 

WriteLine()函数的最后一个实参是两个Length对象的和,因此将调用operator+()函数。结果是新的Length对象,编译器将为该对象调用ToString()函数,因此最后一条语句实际上是下面的语句:

  1. Console::WriteLine(L"{0} plus {1} is {2}", len1, len2,  
  2. len1.operator+(len2).ToString()); 

执行该代码段将产生下面的输出:

  1. 6 feet 9 inches plus 7 feet 8 inches is 14 feet 5 inches 

当然,我们可以像下面这样将operator+()函数定义成Length类的静态成员:

  1. static Length operator+(Length len1, Length len2)  
  2. {  
  3. int inchTotal = len1.inches+len2.inches+inchesPerFoot*  
  4. (len1.feet+len2.feet);  
  5. return Length(inchTotal/inchesPerFoot, inchTotal%inchesPerFoot);  
  6. }  

形参是两个Length对象,它们将加到一起产生新的Length对象。因为operator+()函数现在是类的静态成员,所以完全有权访问两个实参Length对象的私有成员feet和inches。C++(www.cppentry.com)/CLI类不允许友元函数存在,外部函数无权访问类的私有成员,因此我们没有其他实现加法运算符的可能性。

因为我们不打算处理面积,所以Length对象的乘法实际上仅当长度乘以数值时才有意义。乘法运算符可以作为类的静态成员实现,但我们将在类外部定义该函数。Length类现在如下所示:

  1. value class Length  
  2. {  
  3. private:  
  4. int feet;  
  5. int inches;  
  6.  
  7. public:  
  8. static initonly int inchesPerFoot = 12;  
  9.  
  10. // Constructor  
  11. Length(int ft, int ins) : feet(ft), inches(ins){ }  
  12.  
  13. // A length as a string  
  14. virtual String^ ToString() override  
  15. { return feet+L" feet " + inches + L" inches"; }  
  16.  
  17. // Addition operator  
  18. Length operator+(Length len)  
  19. {  
  20. int inchTotal = inches+len.inches+inchesPerFoot*(feet+len.feet);  
  21. return Length(inchTotal/inchesPerFoot, inchTotal%inchesPerFoot);  
  22. }  
  23.  
  24. static Length operator*(double x, Length len);  
  25.                                           // Pre-multiply by a double value  
  26. static Length operator*(Length len, double x);  
  27.                                           // Post-multiply by a double value  
  28. }; 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇8.9.1 在数值类中重载运算符(3) 下一篇8.9.3 在引用类中重载运算符

评论

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