过载组成运算符(+=,-=,*=和=)。不计算它应该如何

问题描述 投票:0回答: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();
}

输出文件:enter image description here

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

c++ oop operator-overloading fractions
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 
© www.soinside.com 2019 - 2024. All rights reserved.