Reputation: 179
I'd like to ask for a help with a smaller part of my code, implementation of operators +=, -=, *=, /=. I'm really out of ideas how to achieve a situation, that a variable in a method (ComplexNumber operator *= (ComplexNumber &operand) {}
) is for example incremented and new value is stored into same variable. I'd like to keep the style of a syntax used for other binary and unary operators.
// Complex Number project.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
#include "iostream"
#include "ComplexNumber.h"
using namespace std;
template <class T> class ComplexNumber {
T re, im;
public:
ComplexNumber(T re, T im) {
this->re = re;
this->im = im;
}
ComplexNumber operator + (ComplexNumber &operand) {
ComplexNumber sum(this->re + operand.re, this->im + operand.im);
return sum;
}
ComplexNumber operator - (ComplexNumber &operand) {
ComplexNumber diff(this->re - operand.re, this->im - operand.im);
return diff;
}
ComplexNumber operator * (ComplexNumber &operand) {
ComplexNumber product((this->re * operand.re) - (this->im * operand.im), (this->re * operand.im) + (this->im * operand.re));
return product;
}
ComplexNumber operator / (ComplexNumber &operand) {
ComplexNumber quot(((this->re * operand.re) + (this->im * operand.im) / (operand.re * operand.re) + (operand.im * operand.im)), (this->re * operand.im) + (this->im * operand.re));
return quot;
}
ComplexNumber operator += (ComplexNumber &operand) {
ComplexNumber add(this->re - operand.re, this->im - operand.im);
return add;
}
ComplexNumber operator -= (ComplexNumber &operand) {
ComplexNumber sub(this->re - operand.re, this->im - operand.im);
return sub;
}
ComplexNumber operator *= (ComplexNumber &operand) {
return multipl;
}
ComplexNumber operator /= (ComplexNumber &operand) {
return div;
}
ComplexNumber operator - () {
return ComplexNumber(-re, -im);
}
ComplexNumber operator ~ () {
return ComplexNumber(re, -im);
}
void print(void) {
cout << noshowpos << re << showpos << im << "i" << noshowpos;
}
};
int main(int argc, _TCHAR* argv[])
{
ComplexNumber<double> a(2.1, 3.6);
ComplexNumber<double> b(10, 20);
ComplexNumber<double> c(0, 0);
c = ~a;
cout << "Complex number: ";
a.print();
cout << endl << "Complex conjugate number: ";
c.print();
cout << endl << endl;
c = -a;
cout << "Complex number: ";
a.print();
cout << endl << "Negative number: ";
c.print();
cout << endl << endl;
c = a + b;
cout << "Sum: ";
a.print();
cout << " + ";
b.print();
cout << " = ";
c.print();
cout << endl << endl;
c = a - b;
cout << "Difference: ";
a.print();
cout << " - ";
b.print();
cout << " = ";
c.print();
cout << endl << endl;
c = a * b;
cout << "Product: ";
a.print();
cout << " * ";
b.print();
cout << " = ";
c.print();
cout << endl << endl;
c = a / b;
cout << "Quotient: ";
a.print();
cout << " / ";
b.print();
cout << " = ";
c.print();
cout << endl << endl;
c += a; // c = c + a
cout << "Addition: ";
a.print();
cout << " / ";
b.print();
cout << " = ";
c.print();
cout << endl << endl;
c -= a; // c = c - a
cout << "Subtraction: ";
a.print();
cout << " / ";
b.print();
cout << " = ";
c.print();
cout << endl << endl;
c *= a; // c = c * a
cout << "Multiplication: ";
a.print();
cout << " / ";
b.print();
cout << " = ";
c.print();
cout << endl << endl;
c /= a; // c = c / a
cout << "Division: ";
a.print();
cout << " / ";
b.print();
cout << " = ";
c.print();
cout << endl << endl;
//cout << "Complex number: " << noshowpos << a.re << showpos << a.im << "i" << noshowpos;
//a.print();
//b.print();
system("pause");
return 0;
}
Thank you very much for any help.
Upvotes: 0
Views: 177
Reputation: 66459
Since you already have the binary arithmetic operators, you can use those.
The assigning operators are supposed to return a reference to *this
, and the operand should be either const
or not a reference:
ComplexNumber& operator += (const ComplexNumber &operand) {
*this = *this + operand;
return *this;
}
It's more common to start with the assigning operators and use those to implement the binary operators, often as non-members (this is more useful if your class has converting constructors):
ComplexNumber& operator += (const ComplexNumber &operand) {
re += operand.re;
im += operand.im;
return *this;
}
// ...
template <class T>
ComplexNumber<T> operator + (ComplexNumber<T> lhs, const ComplexNumber<T>& rhs) {
return lhs += rhs;
}
Upvotes: 2