This is a friend operator overload function that used addition. It adds two mix fraction by reducing them to simplest form the adding and converting them back to a mixed number

Mixed operator+(const Mixed& f1, const Mixed& f2)
{
    int newnum, newnum2, gcd, tmpNumerator2, tmpNumerator, tmpInteger, tmpInteger2;
    Mixed r;

        tmpNumerator = f1.numerator;
        tmpNumerator2 = f2.numerator;
        tmpInteger = f1.integer;
        tmpInteger2 = f2.integer;
    
    if(tmpInteger < 0)
        tmpNumerator *= -1;
    if(tmpInteger2 < 0)
        tmpNumerator2 *= -1;

    newnum = (f1.integer * f1.denominator) + tmpNumerator;
    newnum2 = (f2.integer * f2.denominator) + tmpNumerator2;

    r.numerator = (newnum * f2.denominator)+ (newnum2 * f1.denominator);
    r.denominator = f1.denominator * f2.denominator;

    gcd = GCD(r.numerator, r.denominator);
     
    r.numerator /= gcd;
    r.denominator /= gcd;

    r.integer = r.numerator / r.denominator;
    r.numerator = (r.numerator % r.denominator);

    return r;
}

I nee to find a way that will not only make this function add f1+f2 but also f1+10,

I think I need to use it as a member function that not only add f1 to f2 but also add f1 to 10 . I have no idea how to do this. if any one know how to please help. Thank you

Recommended Answers

All 3 Replies

Can we see the constructors of the class Mixed?

Yes but I am the sure how to do this. The only clue that was said was : Note that these last two are not really needed in the Fraction class, since we have a conversion constructor! The normal operator+ function:

friend Fraction operator+(const Fraction& f1, const Fraction& f2);

will take care of calls that involve type conversions from int to Fraction, via the conversion constructor:

Fraction n1, n2, n3;
n3 = n1 + 5;
n3 = 10 + n2;

However, ask yourself... Will both of these work for the friend version and the member function version? Why or why not?

This is my header file

#include <iostream>        // for ostream, istream
using namespace std;

int GCD(int, int);

class Mixed
{
    friend Mixed operator+(const Mixed& f1, const Mixed& f2);
    friend Mixed operator-(const Mixed& f1, const Mixed& f2);
    friend Mixed operator*(const Mixed& f1, const Mixed& f2);
    friend Mixed operator/(const Mixed& f1, const Mixed& f2);

    friend bool operator<(const Mixed& f1, const Mixed& f2);
    friend bool operator>(const Mixed& f1, const Mixed& f2);
    friend bool operator>=(const Mixed& f1, const Mixed& f2);
    friend bool operator<=(const Mixed& f1, const Mixed& f2);
    friend bool operator==(const Mixed& f1, const Mixed& f2);
    friend bool operator!=(const Mixed& f1, const Mixed& f2);

    friend istream& operator>>(istream& out, Mixed& f);
    friend ostream& operator<<(ostream& in, Mixed& f);


public:
    Mixed(int i, int n, int d);    
    Mixed(int i = 0);                        // constructor with parameters
                                            // acts as conversion constructor

    void operator++();                        // ++ prefix
    void operator--();                        // -- prefix

    void operator++(int);                    // postfix ++
    void operator--(int);                    // postfix --

    double Evaluate();
    void ToFraction();
    void Simplify();
private:
    int integer, numerator, denominator;
    double result;
    
};

I want to use conversion constructor to convert the integer operand to a Mixed object in order to add f1 + 10, I haven't learned that yet and I don't know how to do it, this is my only problem left before i can finish. I have working on this one part for a day and a half and I can not figure it out

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.