-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11-35.cpp
52 lines (43 loc) · 1.68 KB
/
11-35.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <complex>
#include <iostream>
using namespace std;
template<class T>
struct BasicPolar
{
public:
typedef BasicPolar self;
// constructors
BasicPolar( ) : m( ) { }
BasicPolar(const self& x) : m(x.m) { }
BasicPolar(const T& rho, const T& theta) : m(polar(rho, theta)) { }
// assignment operations
self operator-( ) { return Polar(-m); }
self& operator+=(const self& x) { m += x.m; return *this; }
self& operator-=(const self& x) { m -= x.m; return *this; }
self& operator*=(const self& x) { m *= x.m; return *this; }
self& operator/=(const self& x) { m /= x.m; return *this; }
operator complex<T>( ) const { return m; }
// public member functions
T rho( ) const { return abs(m); }
T theta( ) const { return arg(m); }
// binary operations
friend self operator+(self x, const self& y) { return x += y; }
friend self operator-(self x, const self& y) { return x -= y; }
friend self operator*(self x, const self& y) { return x *= y; }
friend self operator/(self x, const self& y) { return x /= y; }
// comparison operators
friend bool operator==(const self& x, const self& y) { return x.m == y.m; }
friend bool operator!=(const self& x, const self& y) { return x.m != y.m; }
private:
complex<T> m;
};
typedef BasicPolar<double> Polar;
int main( ) {
double rho = 3.0; // magnitude
double theta = 3.141592 / 2; // angle
Polar coord(rho, theta);
cout << "rho = " << coord.rho( ) << ", theta = " << coord.theta( ) << endl;
coord += Polar(4.0, 0.0);
cout << "rho = " << coord.rho( ) << ", theta = " << coord.theta( ) << endl;
system("pause");
}