「分数」を追加して、2クラスのデータを比較する方法。私は+
をオーバーロードすることによって追加演算を実行すると仮定しており、これらのクラスに対して==
演算子のオーバーロードを使用して値の等価性をチェックする必要があります。演算子のオーバーロード:「整数」と私は二つのクラス<strong>整数</strong>と<strong>フラクション</strong>と1つの抽象クラス<strong>数</strong>を持っている
1を実行する
操作は整数+整数=整数
2.追加画分+画分を追加=フラクション
3.整数+画分を追加=フラクション
私は1回目と2回目の操作を実行できましたが、追加できませんでした整数と分数の以下
コードスニペットである:
Number.h
#pragma once #include <iostream> template<class T> class Number { virtual const T operator+ (const T &) = 0; virtual void display(std::ostream &) const = 0; virtual bool operator==(const T& rhs) const = 0; };
Integer.h
#pragma once #include "Number.h" #include "Fraction.h" class Integer : public Number<Integer> { int intValue; public: void display(std::ostream &) const; int getValue() const; void setValue(int); Integer() {} Integer(int num); const Integer operator+ (const Integer &); virtual ~Integer() {} bool operator==(const Integer&) const; };
Integer.cpp
#include "Integer.h" #include "Number.h" #include <iostream> #include <string> // parameterized constructor Integer::Integer(int num) { intValue = num; } // return integer value int Integer::getValue() const { return this->intValue; } void Integer::setValue(int x) { this->intValue = x; } // operator "+" overloading const Integer Integer::operator+(const Integer &secondNumber) { Integer temp = this->intValue + secondNumber.intValue; return temp; } // operator "=" overloading void Integer::display(std::ostream& stream) const { stream << this->intValue; } // comparasion operator overload bool Integer::operator==(const Integer& rhs) const { return this->intValue == rhs.intValue; }
Fraction.h
#pragma once #include "Number.h" #include "Integer.h" class Fraction : public Number<Fraction> { Integer _numerator; Integer _denominator; public: void display(std::ostream &) const; Fraction() = delete; Fraction(const int &, const int &); const Fraction operator+ (const Fraction &); int gcdCalculate(int val1, int val2); int lcmCalculate(const int val1, const int val2); virtual ~Fraction() {} bool operator==(const Fraction& rhs) const; };
Fraction.cpp
#include "Fraction.h" #include <iostream> // parameterised constructor Fraction::Fraction(const int & num, const int & den) { _numerator.setValue(num); _denominator.setValue(den); } // display the fraction value void Fraction::display(std::ostream & stream) const { if (this->_denominator == 0) std::cout << "Undefined: " << this->_numerator.getValue() << "/" << this->_denominator.getValue() << " (Divide By Zero Exception)"; else stream << this->_numerator.getValue() << "/" << this->_denominator.getValue(); } // "+" operator overloading const Fraction Fraction::operator+(const Fraction &numberTwo) { int lcm = lcmCalculate(this->_denominator.getValue(), numberTwo._denominator.getValue()); int multiplier1 = 0; if (this->_denominator.getValue()) multiplier1 = lcm/this->_denominator.getValue(); int multiplier2 = 0; if (numberTwo._denominator.getValue()) multiplier2 = lcm/numberTwo._denominator.getValue(); return Fraction((this->_numerator.getValue() * multiplier1) + (numberTwo._numerator.getValue() * multiplier2), lcm); } // LCM Calculation int Fraction::lcmCalculate(const int val1, const int val2) { int temp = gcdCalculate(val1, val2); return temp ? (val1/temp * val2) : 0; } // GCD Calculation int Fraction::gcdCalculate(int val1, int val2) { for (;;) { if (val1 == 0) return val2; val2 %= val1; if (val2 == 0) return val1; val1 %= val2; } } // comparision operator overload bool Fraction::operator==(const Fraction& rhs) const { Integer numCheck = this->_numerator; Integer denCheck = this->_denominator; if (rhs._numerator.getValue()) numCheck.setValue(numCheck.getValue()/rhs._numerator.getValue()); if (rhs._numerator.getValue()) denCheck.setValue(denCheck.getValue()/rhs._denominator.getValue()); if (numCheck == denCheck) { return true; } return false; }
QUESTION:
- 私は整数+分数クラスを追加する方法として混乱しています。
- Numberクラスから継承する別のクラスを作成する必要がありますか。
oprator+
のオーバーロード方法はNumber Class
です。
私を助けてください、私は
例 ようclass Integer : public Number<Integer>
{
const Fraction operator+(const Fraction &);
}
const Fraction Integer::operator+(const Fraction &numberTwo)
{
^^ I will get error here
// Addition opeartion
}
何かを持っているだろう、私はIntegerクラス自体にInteger + Fraction = Fraction
を追加しようとします。
'Number'クラスはありません - ' Number'はクラステンプレートです。 'Fraction'と' Integer'には共通の基底クラスはありません。 2つの抽象クラス「Number」と「Number 」があり、これら2つは無関係です。 –
molbdnilo
なぜ否定的な投票ですか? – Unbreakable