复数类实现

来源:互联网 发布:织梦cms和wordpress 编辑:程序博客网 时间:2024/06/06 07:47

复数类实现

实现复数类的++、–、+、-、比较大小等

1.#include <iostream>
2.using namespace std;
3.
4.class Complex
5.{
6.public:
7. Complex(double real = 0.0, double image = 0.0)
8. :_real(real)
9. ,_image(image)
10. {
11. }
12.
13. Complex(const Complex& d)
14. :_real(d._real)
15. ,_image(d._image)
16. {
17. }
18. ~Complex()
19. {
20. }
21.
22. Complex& operator=(const Complex& d)
23. {
24. _real = d._real;
25. _image = d._image;
26. return *this;
27. }
28.
29. void Display()
30.
{
31. cout<<"_real=="<<_real<<"_image=="<<_image<<endl;
32. }
33.
34. bool operator>(const Complex& d)const
35. {
36. if((_real > d._real)&&(_image > d._image))
37. return true;
38. return false;
39. }
40.
41. bool operator<(const Complex& d)const
42. {
43. if((_real < d._real)&&(_image < d._image))
44. return true;
45. return false;
46. }
47.
48. bool operator>=(const Complex& d)const
49. {
50. if(this < &d)
51. return false;
52. return true;
53. }
54.
55. bool operator<=(const Complex& d)const
56. {
57. if(this > &d)
58. return false;
59. return true;
60. }
61.
62. bool operator==(const Complex& d)const
63. {
64. if((_real == d._real)&&(_image == d._image))
65. return true;
66. return false;
67. }
68. bool operator!=(const Complex& d)const
69. {
70. if(this == &d)
71. return false;
72. return true;
73. }
74.
75. Complex& operator++()//(前置++)复数++只给实部+
76. {
77. _real++;
78. return *this;
79. }
80.
81. Complex& operator++(int)//后置++
82. {
83. Complex tmp = *this;
84. tmp._real++;
85. return *this;
86. }
87.
88. Complex& operator+=(const Complex& d)
89. {
90. _real += d._real;
91. return *this;
92. }
93.
94. Complex& operator+(const Complex& d)
95. {
96. _real + d._real;
97. return *this;
98. }
99.
100.private:
101. double _real;
102. double _image;
103.};
104.
105.
106.int main()
107.
{
108. Complex d1(2.3,3.2);
109. Complex d2;
110. d2 = d1;
111. d2.Display();
112. system("pause");
113. return 0;
114.}
115.
116.

原创粉丝点击