tutorial4----运算符的重载.doc
实验四 运算符重载一、 实验编号j二、 实验学时2学时三、 实验目的1、熟练掌握运算符重载的方法。2、熟练运用运算符重载解决实际问题。四、实验内容1、定义复数Complex类,包含私有成员double real,imag,重载运算符+和-2、定义Date类,并重载运算符+五、实验原理1、运算符重载(1)成员函数方式运算结果类型 operator 运算符(形式参数) 函数体 (2)友元函数方式friend 结果类型 operator 运算符(对象参数,参数) 函数体 双目(返回类型 类名operator 双目运算符(类名&引用) )2、运算符重载规则(1)不可以重载非C+的运算符;(2)运算符重载可以采用成员函数方式或友元方式;(3)被重载的运算符必须保持预定义的的优先级与结合性;(4)应尽量使重载的运算符语义自然、好理解;(5)注意各运算符之间的联系。六、参考答案1、定义Complex类,包含私有成员double real,imag;并重载运算符+和-1. #include <iostream.h>2. class Complex3. 4. private:5. double real,imag;6. public:7. Complex(double x0=0,double y0=0);8. Complex operator-(const Complex &b);/以成员函数形式重载运算符-9. friend Complex operator+(const Complex &a,const Complex &b);/以友元函数形式重载运算符'+'10. void Display()11. 12. If(real!=0&&imag!=0)13. cout<<real<<'+'<<imag<<'i'<<endl;14. Else if(real!=0&&imag=0)15. Cout<<real<<endl;16. Else if(real=0&&imag!=0)17. Cout<<imag<<"i"<<endl;18. Else19. Cout<<"0"<<endl;20. 21. ;22. Complex :Complex (double x0,double y0)23. 24. real=x0;25. imag=y0;26. 27. Complex Complex :operator-(const Complex &b)28. 29. return Complex (real-b.real,imag-b.imag);30. 31. Complex operator+(const Complex &a,const Complex &b)32. 33. return Complex (a.real+b.real,a.imag+b.imag);34. 35. void main()36. 37. Double x1,y1,x2,y2;38. cout<<"input x1,y1 of v1,x2,y2 of v2:"39. cin>>x1>>y1>>x2>>y2;40. Complex v1(x1,y1),v2(x2,y2),v;41. 42. v=v1+v2;43. cout<<"v1 is:"44. v1.Display();45. cout<<"v2 is:"46. v2.Display();47. cout<<"v1+v2 is:"48. v.Display();49. 50. v=v1-v2;51. cout<<"v1-v2 is:"52. v.Display();53. 2、定义Date类,并重载运算符+1. #include <iostream.h>2. class Date3. private:4. int year;5. int month;6. int day;7. public:8. void SetDate(int x,int y,int z);9. Date operator+();/以成员函数形式重载前置+10. Date operator+(int);/以成员函数形式重载后置+11. void ShowDate(void);12. ;13. void Date:SetDate(int x,int y,int z)14. 15. year=x; month=y; day=z;16. 17. Date Date:operator+()/重载前置+18. 19. day=day+1;20. if(month=1|month=3|month=5|month=7|month=8|month=10|month=12)&&day=32)21. 22. day=1;23. month=month+1;24. if(month=13)25. 26. month=1;27. year=year+1;28. 29. 30. else if(month=4|month=6|month=9|month=11)&&day=31)31. 32. day=1;33. month=month+1;34. 35. else if(month=2)36. 37. if (year%4=0&&year%100!=0)|(year%400=0)/闰年38. 39. if (day=30)40. 41. day=1;42. month=month+1;43. 44. 45. else if (day=29)46. 47. day=1;48. month=month+1;49. 50. 51. return *this;52. 53. Date Date:operator+(int)/重载后置+54. 55. Date temp(*this);56. day=day+1;57. if(month=1|month=3|month=5|month=7|month=8|month=10|month=12)&&day=32)58. 59. day=1;60. month=month+1;61. if(month=13)62. 63. month=1;64. year=year+1;65. 66. 67. else if(month=4|month=6|month=9|month=11)&&day=31)68. 69. day=1;70. month=month+1;71. 72. else if(month=2)73. 74. if (year%4=0&&year%100!=0)|(year%400=0)/闰年75. 76. if (day=30)77. 78. day=1;79. month=month+1;80. 81. 82. else if (day=29)83. 84. day=1;85. month=month+1;86. 87. 88. return temp;89. 90. void Date:ShowDate(void)91. 92. cout<<year<<"/"<<month<<"/"<<day<<endl;93. 94. void main()95. 96. Date day1;97. int x,y,z;98. cout<<"input current date:"99. cin>>x>>y>>z;100. while(1)/判断输入是否有效101. 102. if ( (y=1|y=3|y=5|y=7|y=8|y=10|y=12)&&(z<=31)&&(z>=1) |103. (y=4|y=6|y=9|y=11)&&(z<=30) |104. (x%4=0&&x%100!=0)|(x%400=0)&&(y=2)&&(z<=29)&&(z>=1) |105. (!(x%4=0&&x%100!=0)|(x%400=0)&&(y=2)&&(z<=28)&&(z>=1) 106. )107. break;108. else 109. 110. cout<<"input error!,input current date again:"111. cin>>x>>y>>z;112. 113. 114. day1.SetDate(x,y,z);115. cout<<"the current day is:"116. day1.ShowDate();117. 118. day1+;119. cout<<"after day1+ day1 is:" 120. day1.ShowDate();121. +day1;122. cout<<"after +day1 day1 is:" 123. day1.ShowDate();124. Date day2=day1+;125. cout<<"after day2=day1+ day1 is:" 126. day1.ShowDate();127. cout<<"day2 is:" 128. day2.ShowDate();129. day2=+day1;130. cout<<"after day2=+day1 day1 is:" 131. day1.ShowDate();132. cout<<"day2 is:" 133. day2.ShowDate();134.