- #36
Jamin2112
- 986
- 12
jbunniii said:6. (C++, easier) Let's write a simple complex number class, with various useful operators.
Ok. This is me going through it very fast. We'll see which simple mistakes I make.
Code:
#include <iostream>
#include <math.h>
class Imaginary {
public:
Imaginary(double, double);
~Imaginary();
Imaginary operator + (Imaginary);
Imaginary operator * (Imaginary);
Imaginary operator ' (); // complex conjugate
void print();
double a, b;
double abs();
}
Imaginary::Imaginary(double newa, double newb) {
a = newb;
b = newb;
}
Imaginary::~Imaginary() {
delete *a, *b;
}
Imaginary Imaginary::operator + (Imaginary nextguy) {
Imaginary sumguy(a + nextguy.a, b + nextguy.b);
return sumguy;
}
Imaginary Imaginary::operator + (Imaginary nextguy) {
Imaginary productguy(a * nextguy.a - b * nextguy.b , a * nextguy.b + b * nextguy.a);
return productguy;
}
// complex conjugate
Imaginary::operator ' () {
b = -b;
}
double abs() {
return sqrt(a*a + b*b);
}
void print() {
cout << a << " + " << b << " * i" << endl;
}