设为首页 加入收藏

TOP

8.9.1 在数值类中重载运算符(2)
2013-10-07 12:35:42 来源: 作者: 【 】 浏览:71
Tags:8.9.1 数值 重载 运算

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

该类中新的函数声明给出两个重载的*运算符函数,两者分别使double类型数值在前面和在后面与Length对象相乘。类外部对应于数值在前乘法的operator*()函数的定义如下所示:

  1. Length Length::operator *(double x, Length len)  
  2. {  
  3. int ins = safe_cast<int>(x*len.inches +x*len.feet*inchesPerFoot);  
  4. return Length(ins/12, ins %12);  

数值在后的乘法运算符版本现在可以根据上面的函数实现:

  1. Length Length::operator *(Length len, double x)  
  2. { return operator*(x, len); } 

该函数仅仅颠倒了一下实参,然后调用数值在前的乘法运算符版本。我们可以用下面的代码段来练习这两个函数的用法:

  1. double factor = 2.5;  
  2. Console::WriteLine(L"{0} times {1} is {2}", 
    factor, len2, factor*len2);  
  3. Console::WriteLine(L"{1} times {0} is {2}", 
    factor, len2, len2*factor); 

该代码段的两行输出应该反映出相同的乘法结果(19英尺2英寸)。实参表达式factor*len2等价于:

  1. Length::operator*(factor, len2).ToString() 

调用静态operator*()函数将得到新的Length对象,而调用该对象的ToString()函数将产生WriteLine()函数的实参。表达式len2*factor与此类似,但调用的是形参颠倒的operator*()函数。虽然operator*()函数是为处理double类型的乘数而编写的,但它们同样可以处理整数。当我们在诸如12*(len1+len2)之类的表达式中使用*运算符时,编译器将自动把整数值升级为double类型。

在可运行的示例中,我们可以对Length类的重载运算符稍微作一些扩充。

试一试:包含重载运算符的数值类

本示例为Length类实现了加法、乘法和除法的运算符重载:

  1. // Ex8_12.cpp : main project file.  
  2. // Overloading operators in the value class, Length  
  3. #include "stdafx.h"  
  4. using namespace System;  
  5.  
  6. value class Length  
  7. {  
  8. private:  
  9. int feet;  
  10. int inches;  
  11.  
  12. public:  
  13. static initonly int inchesPerFoot = 12;  
  14.  
  15. // Constructor  
  16. Length(int ft, int ins) : feet(ft), inches(ins){ }  
  17.  
  18. // A length as a string  
  19. virtual String^ ToString() override  
  20. { return feet+L" feet " + inches + L" inches"; }  
  21.  
  22. // Addition operator  
  23. Length operator+(Length len)  
  24. {  
  25. int inchTotal = inches+len.inches+inchesPerFoot*(feet+len.feet);  
  26. return Length(inchTotal/inchesPerFoot, inchTotal%inchesPerFoot);  
  27. }  
  28.  
  29. // Division operator  
  30. static Length operator/(Length len, double x)  
  31. {  
  32. int ins = safe_cast<int>((len.feet*inchesPerFoot + len.inches)/x);  
  33. return Length(ins/inchesPerFoot, ins%inchesPerFoot);  
  34. }  
  35.  
  36. static Length operator*(double x, Length len);  
  37.                                           // 
    Pre-multiply by a double value  
  38. static Length operator*(Length len, double x);  
  39.                                           // 
    Post-multiply by a double value  
  40. };  
  41.  
  42. Length Length::operator *(double x, Length len)  
  43. {  
  44. int ins = safe_cast<int>(x*len.inches +x*len.feet*inchesPerFoot);  
  45. return Length(ins/inchesPerFoot, ins%inchesPerFoot);  
  46. }  
  47.  
  48. Length Length::operator *(Length len, double x)  
  49. { return operator*(x, len); }  
  50.  
  51. int main(array<System::String ^> ^args)  
  52. {  
  53. Length len1 = Length(6, 9);  
  54. Length len2 = Length(7, 8);  
  55. double factor = 2.5;  
  56.  
  57. Console::WriteLine(L"{0} plus {1} is {2}", len1, len2, len1+len2);  
  58. Console::WriteLine(L"{0} times {1} is {2}", factor, len2, factor*len2);  
  59. Console::WriteLine(L"{1} times {0} is {2}", factor, len2, len2*factor);  
  60. Console::WriteLine(L"The sum of {0} and {1} divided by {2} is {3}",  
  61. len1, len2, factor, (len1+len2)/factor);  
  62.  
  63. return 0;  

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇8.9.2 重载递增和递减运算符 下一篇9.3.1 派生类中构造函数的操作(2..

评论

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