C++运算符重载

C++我们自定义的类型(类)是没有办法操作的。这个时候就需要我们对这么运算符进行重新定义,赋予其新的功能,以满足自身的需求。运算符重载的实质就是函数重载或函数多态。

要重载运算符,需要使用被称为运算符函数的特殊函数形式,运算符函数形式:operatorp(argument-list)//operator 后面的'p'为要重载的运算符符号。

例子:

  1. MyTime.h文件:  
  2. #pragma once  
  3. #ifndef MYTIME_H_  
  4. #define MYTIME_H_  
  5. class CMyTime  
  6. {  
  7. private:  
  8. int m_hours;  
  9. int m_minutes;  
  10. public:  
  11. CMyTime();  
  12. CMyTime(int h, int m = 0);  
  13. void AddHr(int h);  //小时更改  
  14. void AddMin(int m);//分钟更改  
  15. void Reset(int h = 0, int m = 0);  //重新设置时间  
  16. CMyTime operator+(const CMyTime &t) const;  //重载加法  
  17. CMyTime operator-(const CMyTime &t) const;  //重载减法  
  18. CMyTime operator*(double n) const;                //重载乘法  
  19. void Show() const;  
  20. ~CMyTime();  
  21. };  
  22. #endif  


[cpp]  view plain  copy
  1. MyTIme.cpp文件:  
  2. #include "stdafx.h"  
  3. #include "MyTime.h"  
  4. #include <iostream>  
  5.   
  6. CMyTime::CMyTime()  
  7. {  
  8. m_hours = 0;  
  9. m_minutes = 0;  
  10. }  
  11.   
  12.   
  13. CMyTime::CMyTime(int h, int m)  
  14. {  
  15. m_hours = h;  
  16. m_minutes = m;  
  17. }  
  18.   
  19.   
  20. CMyTime::~CMyTime()  
  21. {  
  22. }  
  23.   
  24.   
  25. void CMyTime::AddHr(int h)                                             //小时更改  
  26. {  
  27. m_hours += h;  
  28. }  
  29.   
  30.   
  31. void CMyTime::AddMin(int m)                                             //分钟更改  
  32. {  
  33. m_minutes = m;  
  34. }  
  35.   
  36.   
  37. void CMyTime::Reset(int h, int m)                                           //重新设置时间  
  38. {  
  39. m_hours = h;  
  40. m_minutes = m;  
  41. }  
  42.   
  43.   
  44. CMyTime CMyTime::operator+(const CMyTime &t) const              //重载加法运算符函数  
  45. {  
  46. CMyTime sum;  
  47. sum.m_minutes = t.m_minutes + m_minutes;  
  48. sum.m_hours = t.m_hours + m_hours + sum.m_minutes / 60;  
  49. sum.m_minutes %= 60;  
  50. return sum;  
  51. }  
  52.   
  53.   
  54. CMyTime CMyTime::operator-(const CMyTime &t) const         //重载为减法运算符函数  
  55. {  
  56. CMyTime diff;  
  57. int tot1, tot2;  
  58. tot1 = t.m_minutes + 60 * t.m_hours;  
  59. tot2 = m_minutes + 60 * m_minutes;  
  60. diff.m_minutes = (tot2 - tot1) % 60;  
  61. diff.m_hours = (tot2 - tot1) / 60;  
  62. return diff;  
  63. }  
  64.   
  65.   
  66. CMyTime CMyTime::operator*(double n) const                 //重载为乘法运算符函数。  
  67. {  
  68. CMyTime result;  
  69. long totalMinutes = m_hours * 60 * n+ m_minutes *n;  
  70. result.m_minutes = totalMinutes % 60;  
  71. result.m_hours = totalMinutes / 60;  
  72. return result;  
  73. }  
  74.   
  75.   
  76. void CMyTime::Show() const  
  77. {  
  78. std::cout << m_hours << " hours "  
  79. << m_minutes << " minutes\n";  
  80. }  


[cpp]  view plain  copy
  1. 主函数:  
  2.   
  3. // Study11-02.cpp : 定义控制台应用程序的入口点。  
  4. //  
  5.   
  6.   
  7. #include "stdafx.h"  
  8. #include <iostream>  
  9. #include "MyTime.h"  
  10.   
  11.   
  12. int _tmain(int argc, _TCHAR* argv[])  
  13. {  
  14. using std::cout;  
  15. using std::endl;  
  16. CMyTime weeding(4, 35);  
  17. CMyTime waxing(2, 47);  
  18. CMyTime total;  
  19. CMyTime diff;  
  20. CMyTime adjusted;  
  21.   
  22.   
  23. cout << "weeding Time = ";  
  24. weeding.Show();  
  25. cout << endl;  
  26.   
  27.   
  28. cout << "waxing Time = ";  
  29. waxing.Show();  
  30. cout << endl;  
  31.   
  32.   
  33. cout << "total work Time = ";   //(1)  
  34. total = weeding + waxing;  
  35. total.Show();  
  36. cout << endl;  
  37.   
  38.   
  39. diff = weeding - waxing;  
  40. cout << "weeding Time - waxing Time = "//(2)  
  41. diff.Show();  
  42. cout << endl;  
  43.   
  44.   
  45. adjusted = total *1.5;                     //(3)  
  46. cout << "adjusted work Time = ";  
  47. adjusted.Show();  
  48. cout << endl;  
  49.   
  50.   
  51. return 0;  
  52. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值