所以我正在爲我的OO類寫一個mixed numerals類。我們需要重載每一個比較和布爾操作數(除其他外),但我在如何考慮'<'和'>''操作數時遇到了麻煩。這裏是我的'=='和'!='操作數函數的一些上下文。混合數字類操作數重載C++
編輯:另外如果任何人有任何提示加/減法的方式,我將不勝感激。
bool operator ==(Mixed& mn1, Mixed& mn2){
mn1.ToFraction();
mn2.ToFraction();
mn1.Simplify();
mn2.Simplify();
if(mn1.numerator == mn2.numerator && mn1.denominator == mn2.denominator)
return true;
else
return false;
}
和
bool operator !=(Mixed& mn1, Mixed& mn2){
mn1.ToFraction();
mn2.ToFraction();
mn1.Simplify();
mn2.Simplify();
if(mn1.numerator == mn2.numerator && mn1.denominator == mn2.denominator)
return false;
else
return true;
}
如果有人可以提供一些指導,我會很感激。哦,我們不能將它們轉換成小數進行比較。
編輯:這是我的標題。
#include <iostream>
using namespace std;
class Mixed
{
public:
Mixed(int integer, int numerator = 0, int denominator = 1);
Mixed(int integer = 0);
double Evaluate();
void ToFraction();
void Simplify();
friend istream& operator >>(istream& in, Mixed& mn);
friend ostream& operator <<(ostream& out, Mixed& mn);
friend bool operator ==(Mixed& mn1, Mixed& mn2);
friend bool operator !=(Mixed& mn1, Mixed& mn2);
friend bool operator >(Mixed& mn1, Mixed& mn2);
friend bool operator <(Mixed& mn1, Mixed& mn2);
friend bool operator <=(Mixed& mn1, Mixed& mn2);
friend bool operator >=(Mixed& mn1, Mixed& mn2);
friend const Mixed operator +(Mixed& mn1, Mixed& mn2);
friend const Mixed operator -(Mixed& mn1, Mixed& mn2);
friend const Mixed operator *(Mixed& mn1, Mixed& mn2);
friend const Mixed operator /(Mixed& mn1, Mixed& mn2);
private:
int GCD(int a, int b);
int integer, numerator, denominator;
};
不要改變你的論點'混合常量&' –
如果我這樣做,我失去的能力調用我的ToFraction()和Simplify()函數。 –
「混合數字」是指「有理數」嗎? –