1

该代码是一个更复杂的代码的一部分,但我缩小到看到我有问题的部分。因此,在重载 +=、-=、*= 和 /= 运算符后,结果都是一样的,我不知道我做错了什么。 头文件如下所示:

#pragma once

#include<iostream>
using namespace std;

class Fraction
{
protected:
    int a, b;//protected attributes

public:
    Fraction() //explicit constructor w-out parameters
    {
        a = 0;
        b = 1;
    }

    Fraction(int aa, int bb) //explicit constructor with 2 parameter
    {

        if (bb != 0) //check before called
            a = aa;
        b = bb;
    }

    ~Fraction() //destructor
    {
        a = 0;
        b = 0;
    }

    void set_a(int denom = 0) //setters
    {
        a = denom;
    }
    void set_b(int nom = 1)
    {
        b = nom;
    }

    int get_a() //getters
    {
        return a;
    }
    int get_b()
    {
        return b;
    }
};


class Fraction_ext :public Fraction //inherited class
{
public:
    Fraction_ext(int x, int y) :Fraction(x, y) {}

    void simplify() // simplify() func and using the differences based algorithm
    {
        int c = abs(a);
        int d = abs(b);
        while (c != d)
        {
            if (c > d)
                c = c - d;
            else
                d = d - c;
        }
        a /= c;
        b /= c;
        cout << "\n\tFraction was simplified."; //displaying appropriate message
    }

    Fraction_ext operator+=(Fraction_ext F) //overloadng += using member method
    {
        Fraction_ext f(a, b);
        f.a += f.b;
        f.simplify();
        return f;
    }
    Fraction_ext operator-=(Fraction_ext F) //overloading -=
    {
        Fraction_ext f(a, b);
        f.a -= f.b;
        f.simplify();
        return f;
    }
    Fraction_ext operator*=(Fraction_ext F) //overloading *=
    {
        Fraction_ext f(a, b);
        f.a *= f.b;
        f.simplify(); //simplifying result after calc
        return f;
    }
    Fraction_ext operator/=(Fraction_ext F) //overloading /=
    {
        Fraction_ext f(a, b);
        f.a /= f.b;
        f.simplify();
        return f;
    }
};

源文件:

#include"Header.h"

int main()
{   
    int a, b, c, d;
    cout << "Enter attributes for ob1 and ob2:";
    cin >> a >> b; 
    cin >> c >> d;
    Fraction_ext ob1(a, b), ob2(c, d); //instantiate 2 objects for the inherited class

    //creating 4 other objects for the results
    Fraction_ext sum = ob1, dif = ob1, prod = ob1, div = ob1;
    ob1.simplify();
    ob2.simplify();

    cout << "\n\tOb1 simplified is: " << ob1.get_a() << "/" << ob1.get_b();
    cout << "\n\tOb2 simplified is: " << ob2.get_a() << "/" << ob2.get_b() << endl;
    sum += ob2;
    cout << "\nOverloaded += :" << sum.get_a() << "/" << sum.get_b();
    dif -= ob2;
    cout << "\nOverloaded -= :" << dif.get_a() << "/" << dif.get_b();
    prod *= ob2;
    cout << "\nOverloaded *= :" << prod.get_a() << "/" << prod.get_b();
    div /= ob2;
    cout << "\nOverloaded /= :" << div.get_a() << "/" << div.get_b();
}

输出: 在此处输入图像描述

*= 操作后程序停止工作,所以它甚至不显示最后的结果。

4

1 回答 1

1

运算符重载不正确 他们创建一个 Fraction_ext 类型的新对象并更改它,而运算符应更改左侧对象并返回对更改对象的引用。

例如

Fraction_ext & operator +=( const Fraction_ext &F) //overloadng += using member method
{
    this->a += f.b;
    this->simplify();
    return *this;
}

我也怀疑这个说法

this->a += f.b;

添加两个分数。所以检查算法。据我所知,两个分数的总和计算如下

a1   a2   a1 * b2 + a2 * b1 
-- + -- = -----------------
b1   b2        b1 * b2 
于 2020-05-21T12:59:21.843 回答