-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplex.h
More file actions
242 lines (212 loc) · 8.21 KB
/
Copy pathcomplex.h
File metadata and controls
242 lines (212 loc) · 8.21 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*! \file
*
* Definitions for the complex class. \n
* Programmer: Noah Klein \n
* Class: CS5201 \n
* Assignment: Homework 6 \n
*/
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
#include <cmath>
//! complex class.
/*!
* This class uses two type T objects to store and represent a complex number.
*/
template <typename T>
class complex
{
private:
T m_a; /*!< A type T object that stores the real value for the complex
* number.
*/
T m_b; /*!< A type T object that stores the imaginary value for the complex
* number.
*/
public:
// Essential //
/*! \brief Constructor
*
* Description: Default constructor for the complex class that initializes
* the real value as 1 and the imaginary value as 0.
* \post A complex number is initialized with the values specified.
*/
complex(): m_a(1), m_b(0) {}
/*! \brief Constructor
*
* Description: Parameterized constructor for the complex class that
* initializes the different parts with the values passed.
* \param a is the real value for the complex number.
* \param b is the imaginary value for the complex number.
* \post A complex number is initialized with the values passed.
*/
complex(const T& a, const T& b): m_a(a), m_b(b) {}
/*! \brief Copy Constructor
*
* Description: A copy constructor for the complex class that initializes a
* new complex object with the values of the complex object passed.
* \param rhs is the complex object to be copied.
* \pre operator = must be defined for type T.
* \post A complex number is initialized with the same values as the object
* passed.
*/
complex(const complex& rhs);
/*! \brief = operator
*
* Description: = operator for the complex class that sets the value of the
* calling object equal to the values of the object passed.
* \param rhs is the complex number to be copied.
* \return Returns a reference to the calling object.
* \pre = operator must be defined for type T.
* \post The calling object is set equal to the object passed.
*/
complex& operator=(const complex& rhs);
/*! \brief Destructor
*
* Description: Placeholder function for complex destructor.
* \post The calling object is safely desctructed.
*/
~complex() {}
// Accessors //
/*! \brief Real Accessor
*
* Description: Accessor for the real member for the complex class that
* returns a copy of the real part of the calling object.
* \return Returns a copy of m_a from the calling object.
*/
T real() const;
/*! \brief Imaginary Accessor
*
* Description: Accessor for the imaginary member for the complex class that
* returns a copy of the imaginary part of the calling object.
* \return Returns a copy of m_b from the calling object.
*/
T imag() const;
/*! \brief Additive Inverse
*
* Description: Returns the additive inverse of the calling object. -m_a.
* \return Returns a T object that represents the additive inverse of the
* complex number.
* \pre unary - operator must be defined for type T.
*/
complex<T> operator-() const;
/*! \brief Complex Conjugate
*
* Description: Returns the complex conjugate of the calling object. -m_b.
* \return Returns a T object that represents the complex conjugate of the
* complex number.
* \pre unary - operator must be defined for type T.
*/
complex<T> operator!() const;
/*! \brief Magnitude
*
* Description: Returns the magnitude of the complex number.
* \return Returns a type T object that represents the magnitude of the
* complex number.
* \pre binary * operator and binary + operator must be defined for type T.
*/
T operator~() const;
// Mutators //
/*! \brief Addition
*
* Description: Binary + operator for the complex class that calculates the
* addition of two complex objects.
* \param rhs is the complex object to be added to the calling object.
* \return Returns a complex object that represents the addition of the
* two complex objects.
* \pre Binary += operator must be defined for type T.
*/
complex<T> operator+(const complex<T>& rhs) const;
/*! \brief Subtraction
*
* Description: Binary - operator for the complex class that calculates the
* subtraction of two complex objects.
* \param rhs is the complex object to be subtracted from the calling
* object.
* \return Returns a complex object that represents the difference of the
* two complex objects.
* \post -= operator must be defined for type T.
*/
complex<T> operator-(const complex<T>& rhs) const;
/*! \brief Multiplication
*
* Description: Binary * operator for the complex class that calculates the
* product of the two complex objects.
* \param rhs is the complex object to be multiplied to the calling object.
* \return Returns a complex object that represents the product of the
* two complex objects.
* \pre Binary * operator, Binary + operator, and unary - operator must be
* defined for type T.
*/
complex<T> operator*(const complex<T>& rhs) const;
/*! \brief Integer = operator
*
* Description: Allows the complex number to be set "equal as" a passed
* integer.
* \param number is the number to set the complex object "equal as".
* \return Returns a complex object whose non-imaginary component is equal
* to the parameter.
* \pre Binary * operator, Binary + operator, and unary - operator must be
* defined for type T.
* \post The calling object is now set "equal as" the parameter
*/
complex<T>& operator=(const int number);
/*! \brief += operator
*
* Description: += operator overload to increase functionality with the
* nTrix (and other) classes.
* \param rhs is the complex number to add to the calling object.
* \return Returns *this with modified data
* \pre Binary + operator, and = operator must be defined for type T.
* \post The calling object has the parameter added to it.
*/
complex<T>& operator+=(const complex<T>& rhs);
/*! \brief -= operator
*
* Description: -= operator overload to increase functionality with the
* nTrix (and other) classes.
* \param rhs is the complex number to subtract from the calling object.
* \return Returns *this with modified data
* \pre Binary - operator, and = operator must be defined for type T.
* \post The calling object has the parameter subtracted from it.
*/
complex<T>& operator-=(const complex<T>& rhs);
/*! \brief *= operator
*
* Description: *= operator overload to increase functionality with the
* nTrix (and other) classes.
* \param rhs is the complex number to multiply to the calling object.
* \return Returns *this with modified data
* \pre Binary * operator, and = operator must be defined for type T.
* \post The calling object has the parameter multiplied to it.
*/
complex<T>& operator*=(const complex<T>& rhs);
// Friends //
/*! \brief Insertion operator
*
* Description: Insertion operator overload for the complex class that
* prints the object in the proper format.
* \param out is the ostream passed to the function.
* \param rhs is the complex object to be output.
* \return Returns the ostream object.
* \pre << operator must be defined for type T.
* \post The complex object is output to the stream.
*/
template <typename U>
friend std::ostream& operator<<(std::ostream& out, const complex<U>& rhs);
};
// Extras //
/*! \brief Insertion operator
*
* Description: Insertion operator overload for the complex class that
* prints the object in the proper format.
* \param out is the ostream passed to the function.
* \param rhs is the complex object to be output.
* \return Returns the ostream object.
* \pre << operator must be defined for type T.
* \post The complex object is output to the stream.
*/
template <typename T>
std::ostream& operator<<(std::ostream& out, const complex<T>& rhs);
#include "complex.hpp"
#endif