Created
October 29, 2011 10:42
-
-
Save ramntry/1324326 to your computer and use it in GitHub Desktop.
Yet another small class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SOURCES += \ | |
main.cpp | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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