Skip to content

Instantly share code, notes, and snippets.

@upsuper
Created June 27, 2011 14:38
Show Gist options
  • Save upsuper/1048985 to your computer and use it in GitHub Desktop.
Save upsuper/1048985 to your computer and use it in GitHub Desktop.
Complex class in C++
#include <iostream>
using namespace std;
template<typename T>
class Complex {
private:
T m_real;
T m_imag;
public:
Complex<T>(T real=0.0, T imag=0.0)
:m_real(real), m_imag(imag) { };
Complex<T>(const Complex<T>& c)
:m_real(c.m_real), m_imag(c.m_imag) { };
Complex<T> operator -() const;
Complex<T>& operator +=(const Complex<T>&);
Complex<T>& operator -=(const Complex<T>&);
Complex<T>& operator *=(const Complex<T>&);
Complex<T>& operator /=(const Complex<T>&);
Complex<T> operator +(const Complex<T>&) const;
Complex<T> operator -(const Complex<T>&) const;
Complex<T> operator *(const Complex<T>&) const;
Complex<T> operator /(const Complex<T>&) const;
template<typename U>
friend ostream& operator <<(ostream&, const Complex<U>&);
};
template<typename T>
Complex<T> Complex<T>::operator -() const {
return Complex(-m_real, -m_imag);
}
template<typename T>
Complex<T>& Complex<T>::operator +=(const Complex<T>& c2) {
m_real += c2.m_real;
m_imag += c2.m_imag;
return *this;
}
template<typename T>
Complex<T>& Complex<T>::operator -=(const Complex<T>& c2) {
return *this += -c2;
}
template<typename T>
Complex<T>& Complex<T>::operator *=(const Complex<T>& c2) {
T real = m_real * c2.m_real - m_imag * c2.m_imag;
T imag = m_real * c2.m_imag + m_imag * c2.m_real;
m_real = real;
m_imag = imag;
return *this;
}
template<typename T>
Complex<T>& Complex<T>::operator /=(const Complex<T>& c2) {
Complex<T> nm = Complex<T>(*this) * Complex<T>(c2.m_real, -c2.m_imag);
T dn = c2.m_real * c2.m_real + c2.m_imag * c2.m_imag;
m_real = nm.m_real / dn;
m_imag = nm.m_imag / dn;
return *this;
}
template<typename T>
Complex<T> Complex<T>::operator +(const Complex<T>& c2) const {
return Complex<T>(*this) += c2;
}
template<typename T>
Complex<T> Complex<T>::operator -(const Complex<T>& c2) const {
return Complex<T>(*this) -= c2;
}
template<typename T>
Complex<T> Complex<T>::operator *(const Complex<T>& c2) const {
return Complex<T>(*this) *= c2;
}
template<typename T>
Complex<T> Complex<T>::operator /(const Complex<T>& c2) const {
return Complex<T>(*this) /= c2;
}
template<typename T>
ostream& operator <<(ostream& os, const Complex<T>& c) {
os << c.m_real;
if (c.m_imag < 0)
os << c.m_imag << "i";
else if (c.m_imag > 0)
os << "+" << c.m_imag << "i";
return os;
}
int main() {
Complex<double> c1(5, 4), c2(2, 10);
cout << "c1=" << c1 << endl;
cout << "c2=" << c2 << endl;
cout << "c1+c2=" << (c1 + c2) << endl;
cout << "c1-c2=" << (c1 - c2) << endl;
cout << "c1*c2=" << (c1 * c2) << endl;
cout << "c1/c2=" << (c1 / c2) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment