重载乘法运算符
Overloading Multiplication Operator
我正在为我的 c++ class 做作业。我们不得不重载几个运算符,例如 +、-、!=、= 等。好吧,除了乘法,我已经弄清楚了所有这些运算符。我尝试过的所有内容都会溢出或无法编译。不确定我需要什么。
这是包含我的重载的头文件。
#ifndef COMPLEXNUMBER_H
#define COMPLEXNUMBER_H
#include<iostream>
using namespace std;
class ComplexNumber{
public:
double real, imaginary;
ComplexNumber(){
real = 0;
imaginary = 0;
}
ComplexNumber(double a, double b){
real = a;
imaginary = b;
}
ComplexNumber(double a){
real = a;
imaginary = 0;
}
ComplexNumber & operator= (const ComplexNumber & rhs){
if(this == &rhs){
return *this;
}
else{
real = rhs.imaginary;
imaginary = rhs.imaginary;
}
return *this;
}
ComplexNumber & operator+= (const ComplexNumber &rhs){
real += rhs.real;
imaginary += rhs.imaginary;
return *this;
}
ComplexNumber & operator-= (const ComplexNumber &rhs){
real -= rhs.real;
imaginary -= rhs.imaginary;
return *this;
}
const ComplexNumber operator+ (const ComplexNumber &rhs){
ComplexNumber result = *this;
result += rhs;
return result;
}
const ComplexNumber operator- (const ComplexNumber &rhs){
ComplexNumber result = *this;
result -= rhs;
return result;
}
const ComplexNumber operator* (const ComplexNumber &rhs){
return *this * rhs;
}
friend ostream & operator<< (ostream &out, const ComplexNumber &c){
out << "(" << c.real << (c.imaginary<0?" - ":" + ") << abs(c.imaginary) << " i)";
return out;
}
friend istream & operator>> (istream & in, ComplexNumber &c){
in >> c.real >> c.imaginary;
return in;
}
operator double(){
return real;
}
bool operator== (const ComplexNumber & rhs) const {
bool result = (this->real == rhs.real) && (this->imaginary == rhs.imaginary);
return result;
}
bool operator!= (const ComplexNumber &rhs) const{
return !(*this == rhs);
}
};
#endif
我知道乘法运算符还有很长的路要走,但这正是我目前所拥有的。在这里它是独立的。任何想法将不胜感激!!
const ComplexNumber operator* (const ComplexNumber &rhs){
return *this * rhs;
}
由于您对它的调用方式,它给您带来了溢出。通过您的调用,您将一个复数与一个复数相乘,它只是不断调用同一个运算符而不做任何事情。您可以尝试使用一些基本数学并推导出复数乘法的公式。具体来说,假设我们有两个复数 Z1 和 Z2。令 Z1 = a + bi,其中 a 为实部,b 为虚部,Z2 = c + di,其中 c 为实部,d 为虚部。我们有 Z1 * Z2 = (a + bi)(c + di) = ( ac + adi + cbi - bd )。现在,我们将实部和虚部分开,这里的实部是没有 i 的所有内容,所以 ac - bd,虚部是 ad + cb。现在,在您的 class 成员的条款中使用它,您将得到如下内容:
const ComplexNumber operator* (const ComplexNumber &rhs)
{
ComplexNumber result;
result.real = real * rhs.real - imaginary * rhs.imaginary;
result.imaginary = real * rhs.imaginary + imaginary * rhs.real;
return result;
}
我正在为我的 c++ class 做作业。我们不得不重载几个运算符,例如 +、-、!=、= 等。好吧,除了乘法,我已经弄清楚了所有这些运算符。我尝试过的所有内容都会溢出或无法编译。不确定我需要什么。
这是包含我的重载的头文件。
#ifndef COMPLEXNUMBER_H
#define COMPLEXNUMBER_H
#include<iostream>
using namespace std;
class ComplexNumber{
public:
double real, imaginary;
ComplexNumber(){
real = 0;
imaginary = 0;
}
ComplexNumber(double a, double b){
real = a;
imaginary = b;
}
ComplexNumber(double a){
real = a;
imaginary = 0;
}
ComplexNumber & operator= (const ComplexNumber & rhs){
if(this == &rhs){
return *this;
}
else{
real = rhs.imaginary;
imaginary = rhs.imaginary;
}
return *this;
}
ComplexNumber & operator+= (const ComplexNumber &rhs){
real += rhs.real;
imaginary += rhs.imaginary;
return *this;
}
ComplexNumber & operator-= (const ComplexNumber &rhs){
real -= rhs.real;
imaginary -= rhs.imaginary;
return *this;
}
const ComplexNumber operator+ (const ComplexNumber &rhs){
ComplexNumber result = *this;
result += rhs;
return result;
}
const ComplexNumber operator- (const ComplexNumber &rhs){
ComplexNumber result = *this;
result -= rhs;
return result;
}
const ComplexNumber operator* (const ComplexNumber &rhs){
return *this * rhs;
}
friend ostream & operator<< (ostream &out, const ComplexNumber &c){
out << "(" << c.real << (c.imaginary<0?" - ":" + ") << abs(c.imaginary) << " i)";
return out;
}
friend istream & operator>> (istream & in, ComplexNumber &c){
in >> c.real >> c.imaginary;
return in;
}
operator double(){
return real;
}
bool operator== (const ComplexNumber & rhs) const {
bool result = (this->real == rhs.real) && (this->imaginary == rhs.imaginary);
return result;
}
bool operator!= (const ComplexNumber &rhs) const{
return !(*this == rhs);
}
};
#endif
我知道乘法运算符还有很长的路要走,但这正是我目前所拥有的。在这里它是独立的。任何想法将不胜感激!!
const ComplexNumber operator* (const ComplexNumber &rhs){
return *this * rhs;
}
由于您对它的调用方式,它给您带来了溢出。通过您的调用,您将一个复数与一个复数相乘,它只是不断调用同一个运算符而不做任何事情。您可以尝试使用一些基本数学并推导出复数乘法的公式。具体来说,假设我们有两个复数 Z1 和 Z2。令 Z1 = a + bi,其中 a 为实部,b 为虚部,Z2 = c + di,其中 c 为实部,d 为虚部。我们有 Z1 * Z2 = (a + bi)(c + di) = ( ac + adi + cbi - bd )。现在,我们将实部和虚部分开,这里的实部是没有 i 的所有内容,所以 ac - bd,虚部是 ad + cb。现在,在您的 class 成员的条款中使用它,您将得到如下内容:
const ComplexNumber operator* (const ComplexNumber &rhs)
{
ComplexNumber result;
result.real = real * rhs.real - imaginary * rhs.imaginary;
result.imaginary = real * rhs.imaginary + imaginary * rhs.real;
return result;
}