-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplex.cpp
More file actions
72 lines (57 loc) · 1.44 KB
/
Copy pathComplex.cpp
File metadata and controls
72 lines (57 loc) · 1.44 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
//
// Complex.cpp
// FinalProjectTest
//
// Created by marcus chen i wada on 4/26/24.
//
#include "Complex.hpp"
#include <iostream>
#include <math.h>
// Constructors
Complex::Complex() : real(0.0), imag(0.0) {}
Complex::Complex(double re, double im) : real(re), imag(im) {}
// Getter methods
double Complex::getReal() const {
return real;
}
double Complex::getImaginary() const {
return imag;
}
// Setter methods
void Complex::setReal(double re) {
real = re;
}
void Complex::setImaginary(double im) {
imag = im;
}
// Operator overloading
Complex Complex::operator+(const Complex& other) const {
return Complex(real + other.real, imag + other.imag);
}
Complex Complex::operator-(const Complex& other) const {
return Complex(real - other.real, imag - other.imag);
}
Complex Complex::operator*(const Complex& other) const {
double newReal = real * other.real - imag * other.imag;
double newImag = real * other.imag + imag * other.real;
return Complex(newReal, newImag);
}
// Display method
void Complex::display() const {
std::cout << real;
if (imag >= 0)
std::cout << " + " << imag << "i";
else
std::cout << " - " << -imag << "i";
std::cout << std::endl;
}
double Complex::getModulus() const {
return sqrt(imag*imag+real*real);
}
double Complex::getArgument() const {
if (real == 0.0 && imag == 0.0) {
return 0.0;
} else {
return atan2(imag, real);
}
}