Skip to content

Instantly share code, notes, and snippets.

@ramntry
Created October 29, 2011 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramntry/1324326 to your computer and use it in GitHub Desktop.
Save ramntry/1324326 to your computer and use it in GitHub Desktop.
Yet another small class
SOURCES += \
main.cpp
#include <iostream>
class Complex
{
public:
Complex(double real, double imag = 0.0);
Complex(const Complex &src);
Complex operator +(const Complex &right) const;
private:
double m_real;
double m_imag;
friend std::ostream &operator <<(std::ostream &out, const Complex &self);
};
Complex::Complex(double real, double imag) :
m_real(real),
m_imag(imag)
{}
Complex::Complex(const Complex &src) :
m_real(src.m_real),
m_imag(src.m_imag)
{}
Complex Complex::operator +(const Complex &right) const
{
return Complex(m_real + right.m_real, m_imag + right.m_imag);
}
std::ostream &operator <<(std::ostream &out, const Complex &self)
{
return out << "Complex(" << self.m_real << ", " << self.m_imag << ')';
}
using namespace std;
int main(void)
{
Complex a = 5;
Complex b = a;
Complex c(-5, 1);
cout << (b + c) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment