c++oopoperator-overloadingfractionscompound-assignment

Overloading composed operators (+=,-=,*= and /=). Doesn't calculate how it should


The code is a part of a more complex one, but i reduced to see the part where i have the problem. So after overloading the +=, -=,*= and /= operators,the results are all the same and i don't know what i'm doing wrong. The header file looks like this:

#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;
    }
};

Source file:

#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();
}

The output: enter image description here

After the *= operation the program stops working, so it doesn't even display the last result.


Solution

  • The operators are overloaded incorrectly They create a new object of the type Fraction_ext and change it while the operators shall change the left hand side objects and return references to the changed objects.

    For example

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

    Also I doubt that this statement

    this->a += f.b;
    

    adds two fractions. So check the algorithms. As far as I know the sum of two fractions are calculated like

    a1   a2   a1 * b2 + a2 * b1 
    -- + -- = -----------------
    b1   b2        b1 * b2