[ad_1]
The function add for addition of fractions. The function’s parameter is an object of type Fraction, whose value will be added to the object’s current value. [Note: a/b + c/d = ( a*d + b*c)/b*d] [5 marks] e) The function multiply for multiplication of fractions. The function’s parameter is an object of type Fraction, whose value will be multiplied by its current value. [Note: a/b * c/d = a*c/b*d]
私が試したこと:
void Fraction::add(Fraction f) { }
解決策 1
まず、次のような分数の定義が必要です。
C++
class fract { public: fract(): _counter(0), _divisor(1){} fract(int c, int d): _counter(c), _divisor(d){}; fract& operator=(const fract& f) { /* TODO */ return *this;} // Assignment Operator fract& operator+=(const fract& f) // compound assignment { /* TODO: addition of f to *this takes place here */ return *this; // return the result by reference } friend fract operator+(fract lhs, const fract& rhs) { // Help for chained function a+b+c fractions lhs += rhs; // use compound assignment return lhs; // return result } private: int _counter; int _divisor; };
次に、分数を追加できます。
C++
int main() { fract a(2, 3), b(1, 4), c; c = a + b; return 0; }
[ad_2]
コメント