From 0f2a7c753600089b42bfb9f896208135b95e9b5c Mon Sep 17 00:00:00 2001 From: mickenordin Date: Sat, 18 Jun 2016 19:41:57 +0200 Subject: [PATCH] more operators --- cnumber.cpp | 5 +++-- cnumber.hpp | 6 +++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/cnumber.cpp b/cnumber.cpp index 738e42e..86a78b9 100644 --- a/cnumber.cpp +++ b/cnumber.cpp @@ -3,7 +3,8 @@ int main() { - cnumber z(1, -7); + cnumber a(-1, 4); + cnumber b(4, -7); - cout << z << endl; + cout << "a = " << a << " b = " << b << " a + b = " << a + b << endl; } diff --git a/cnumber.hpp b/cnumber.hpp index 33ceb3b..90b2c54 100644 --- a/cnumber.hpp +++ b/cnumber.hpp @@ -18,9 +18,13 @@ class cnumber { signi = get_sign(i); } + cnumber operator+(const cnumber& that) { + cnumber z(that.r + this->r, that.i + this->i); + return z; + } friend ostream &operator<<( ostream &os, const cnumber &z ) { - os << z.r << (z.signi ? '+' : '-') << 'i' << (z.signi ? z.i : z.i * -1); + os << z.r << (z.signi ? '+' : '\0' ) << z.i << 'i'; return os; }