Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A function that won't return an object

Status
Not open for further replies.

aLittleBoy

Programmer
Sep 17, 2002
6
CA

You could just run this code and try out a fraction like 1&2 + 1&4 and you'll see my problem.


Whenever my protected function (add) returns,
it always returns the numerator and not the whole fraction.


Here is the code:


Code:
//-------Cut Here------//

//THIS WILL ONLY RETURN THE NUMERATOR


#include <iostream>
using namespace std;
//COUT IS USED ONLY FOR DEBUGGING PURPOSES
//AND SHOULD BE REMOVED WHEN I'VE FOUND A WAY TO FIX THIS....

class fraction
{

    private:
        int num;
        int den;
    public:
        fraction():num(0),den(0)
        {}
        fraction(int n, int d):num(n),den(d)
        {}

        void set_num(int n) {num = n;}

        void set_den(int d){den = d;}
        
        int get_num()const{return num;}

        int get_den()const{return den;}
        
        fraction & operator = (fraction &a);
        
        const fraction operator + (const fraction a);
        
    protected:

        fraction & add(int &a,int &b,int &c,int &d);
        
        void LCM(int &a,int &b);
};

    fraction & fraction::operator = (fraction &a) 
        {
            num = a.get_num();
            den = a.get_num();
            return fraction(num,den);
        }

    const fraction fraction::operator + (const fraction a) 
        {
            fraction temp;
            long int num1;
            long int den1;
            long int num2;
            long int den2;
            num1 = num;
            den1 = den;
            num2 = a.get_num();
            den2 = a.get_den();

            cout << &quot;This is in the + operator before the add function\n&quot;;
            cout << &quot;Fraction 1:\n&quot; << num1 << endl <<den1 << endl << &quot;\nFraction 2:\n&quot; << num2 << endl << den2 << endl;
            
            temp = temp.add(num1,den1,num2,den2);
            
            cout << &quot;This is in the + operator after the add function\n&quot;;
            cout << temp.get_num() << endl <<temp.get_den() << &quot;\n\n\n&quot;;
            
            return fraction(temp.get_num(),temp.get_den());
        }


    void fraction::LCM (long int &a,long int &b)
        {
        int lDen;
        bool nope = false;
        int max = a + b;
        do 
            {
                for (lDen = 2 ;!(a % lDen == 0 && b % lDen == 0); lDen++)
                            {
                                if (lDen > max)
                                {
                                    nope = true;
                                    break;
                                };
                            }
                if (a % lDen == 0 && b % lDen == 0)
                    {
                        a = a/lDen;
                        b = b/lDen;
                    }
                else if (nope == true)
                    break;
            } while (nope == false);
        }


    fraction & fraction::add(long int &a,long int &b,long int &c,long int &d)
        {
            fraction temp;
            long int newd;
            long int newn;
            long int newn2;
            long int newer;

            if ((b > d) || (d > b))
            {
                LCM(a,b);
                LCM(c,d);
                newd = b*d;
                newn = a*d;
                newn2 = c*b;
                newer = newn + newn2;

                cout << &quot;\nNow i'm in the add function\n&quot;;
                cout << &quot;----\nthis is the final fraction\n&quot; << newer << endl << newd << &quot;\n&quot;;
                
                LCM(newer,newd);
                
                cout<< &quot;---\nthis is the reduced final fraction\n&quot; << newer << endl 
                    << newd << &quot;\n---\n\n&quot;;
                
                //temp.set_num(newer);
                //temp.set_den(newd);
                //return temp; 
            }
            if (b == d)
            {
                newd = b;
                newer = a+c;
                LCM(newer,newd);
                //return fraction(newer,newd);
            }
            cout<< &quot;---\nthis is the reduced final fraction\n&quot; << newer << endl 
        << newd << &quot;\n---\n\n&quot;;

            cout<<&quot;\n\nThis is whats in the this pointer:\n&quot;<<this->get_num()<<&quot;\n&quot;<<this->get_den()<<&quot;\n\n&quot;;
            this->set_num(newer);
            this->set_den(newd);
        temp.set_num(newer);
        temp.set_den(newd);
            return (fraction(newer,newd));
        }



    int main()
        {
        
        fraction f1(1,2);
        fraction f2(1,4);
        fraction f3;
        f3 = f1 + f2;

        cout << &quot;\nI'm in main\n\n&quot;;
        cout << f3.get_num() << endl << f3.get_den() << endl;

        return 0;
        }

//Stop Here//




///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

















Here is the full version:


Code:
class fraction
{
public:

    ////////////////////////////////////////////////////
    //THIS CODE THIS TO SET VALUES OF THE FRACTION
    ////////////////////////////////////////////////////
    fraction(long int n, long int d):num(n),den(d)
    {}
    void set_num(long int n):num(n)
    {}
    void set_den(long int n):den(n)
    {}
    long int get_num() const 
    {return num;}
    long int get_den() const 
    {return den;}
    ////////////////////////////////////////////////////

    ////////////////////////////////////////////////////
    //THIS PROTOTYPE IS TO OVERLOAD THE OPERATORS
    //TO MAKE IT EASIER FOR THE USER TO PEFORM OPERATIONS
    //WITH THE FRACTIONS
    ////////////////////////////////////////////////////
    fraction operator = (fraction a) const;
    fraction operator + (fraction a) const;
    fraction operator - (fraction a) const;
    fraction operator / (fraction a) const;
    fraction operator * (fraction a) const;
    fraction operator += (fraction a) const;
    fraction operator -= (fraction a) const;
    fraction operator /= (fraction a) const;
    fraction operator *= (fraction a) const;
    ////////////////////////////////////////////////////

    ////////////////////////////////////////////////////
    //THESE PROTOTYPES ARE FOR EXTRA USES OF THIS CLASS
    ////////////////////////////////////////////////////
    fraction reduce (fraction a) const ;
    fraction recip (fraction a) const ;
    ////////////////////////////////////////////////////

    private:
    ////////////////////////////////////////////////////
    //THIS IS THE FRACTION ITSELF
    ////////////////////////////////////////////////////
        long int num;
        long int den;
    ////////////////////////////////////////////////////

    protected:
    ////////////////////////////////////////////////////
    //THIS IS THE BASE OF THE OPERATIONS
    ////////////////////////////////////////////////////

    ////////////////////////////////////////////////////
    //           a = NUMERATOR OF FIRST FRACTION        //
    //           b = DENOMINATOR OF FIRST FRACTION      //
    //           c = NUMERATOR OF SECOND FRACTION       //
    //           d = DENOMINATOR OF SECONFRACTION       //
    ////////////////////////////////////////////////////
        void LCM(long int &a,long int &b);
        void add(long int &a,long int &b,long int &c,long int &d);
        void subtract(long int &a,long int &b,long int &c,long int &d);
        void divide(long int &a,long int &b,long int &c,long int &d);
        void multiply(long int &a,long int &b,long int &c,long int &d);
    ////////////////////////////////////////////////////
};

    ////////////////////////////////////////////////////
    //THIS CODE DIFINES THE OVERLOADED OPERATORS
    ////////////////////////////////////////////////////
    fraction fraction::operator = (fraction a) const
        {
            num = a.get_num;
            den = a.get_num;
        }

    fraction fraction::operator + (fraction a) const
        {
            long int num1;
            long int den1;
            long int num2;
            long int den2;
            num1 = num;
            den1 = den;
            num2 = a.get_num;
            den2 = a.get_den;
            add(num1,den1,num2,den2);
            return fraction(num1,den1);
        }

    fraction fraction::operator - (fraction a) const
        {
            long int num1;
            long int den1;
            long int num2;
            long int den2;
            num1 = num;
            den1 = den;
            num2 = a.get_num;
            den2 = a.get_den;
            subtract(num1,den1,num2,den2);
            return fraction(num1,den1);
        }

    fraction fraction::operator / (fraction a) const
        {
            long int num1;
            long int den1;
            long int num2;
            long int den2;
            num1 = num;
            den1 = den;
            num2 = a.get_num;
            den2 = a.get_den;
            divide(num1,den1,num2,den2);
            return fraction(num1,den1);
        }

    fraction fraction::operator * (fraction a) const
        {
            long int num1;
            long int den1;
            long int num2;
            long int den2;
            num1 = num;
            den1 = den;
            num2 = a.get_num;
            den2 = a.get_den;
            multiply(num1,den1,num2,den2);
            return fraction(num1,den1);
        }

    fraction fraction::operator += (fraction a) const
        {
            fraction temp(num,den);
            temp = temp + a;
            return temp;
        }

    fraction fraction::operator -= (fraction a) const
        {
            fraction temp(num,den);
            temp = temp - a;
            return temp;
        }

    fraction fraction::operator /= (fraction a) const
        {
            fraction temp(num,den);
            temp = temp / a;
            return temp;
        }

    fraction fraction::operator *= (fraction a) const
        {
            fraction temp(num,den);
            temp = temp * a;
            return temp;
        }

    ////////////////////////////////////////////////////

    ////////////////////////////////////////////////////
    //THIS IS THE &quot;EXTRA FUNCTIONS&quot; CODE
    ////////////////////////////////////////////////////
    fraction fraction::reduce (fraction a) const
        {
            long int tempn,tempd;
            tempn = a.get_num;
            tempd = a.get_den;
            LCM(tempn,tempd);
            return fractoin(tempn,tempd);
        }

    fraction fraction::recip (fraction a) const
        {
            long int tempn,tempd;
            tempn = a.get_num;
            tempd = a.get_den;
            return fraction(tempn,tempd);
        }
    ////////////////////////////////////////////////////

    ////////////////////////////////////////////////////
    //THE REST OF THIS CODE DEFINES THE PROTECTED MEMBERS
    ////////////////////////////////////////////////////
    void fraction::LCM (long int &a,long int &b)
        {
        int lDen;
        bool nope = false;
        int temp = 2;
        int max = a + b;


        do 
            {/*&& lDen > max*/); lDen++)
                            {
                                if (lDen > max)
                                {
                                    nope = true;
                                    break;
                                };

                            }


                if (a % lDen == 0 && b % lDen == 0)
                    {
                        a = a/lDen;
                        b = b/lDen;
                    }
                else if (nope == true)
                    break;
            } while (nope == false);

        }


    void fraction::add(long int &a,long int &b,long int &c,long int &d)
        {
            long int newd;
            long int newn;
            long int newn2;
            long int newer;

            if ((b > d) || (d > b))
            {
                LCM(a,b);
                LCM(c,d);
                newd = b*d;
                newn = a*d;
                newn2 = c*b;
                newer = newn + newn2;
                LCM(newer,newd);
            }
            if (b == d)
            {
                newd = b;
                newer = a+c;
                LCM(newer,newd);
            }
            a = newer;
            b = newd;
            c = newer;
            d = newd;

        }

    void fraction::subtract(long int &a,long int &b,long int &c,long int &d)
        {
            long int newd;
            long int newn;
            long int newn2;
            long int newer;

            if ((b > d) || (d > b))
            {
                LCM(a,b);
                LCM(c,d);
                newd = b*d;
                newn = a*d;
                newn2 = c*b;
                newer = newn - newn2;
                LCM(newer,newd);
            }
            if (b == d)
            {
                newd = b;
                newer = a-c;
                LCM(newer,newd);
            }
            a = newer;
            b = newd;
            c = newer;
            d = newd;

        }

    void fraction::divide(long int &a,long int &b,long int &c,long int &d)
        {
            long int temp;
            long int final;
            long int finald;
            temp = c;
            c = d;
            d = temp;
            LCM(a,b);
            LCM(c,d);
            LCM(a,d);
            LCM(c,b);
            final = a*c;
            finald = b * d
            a = final;
            b = finald;
            c = final;
            d = finald;

        }

    void fraction::multiply(long int &a,long int &b,long int &c,long int &d)
        {
            long int final;
            long int finald;
            LCM(a,b);
            LCM(c,d);
            LCM(a,d);
            LCM(c,b);
            final = a*c;
            finald = b * d
            a = final;
            b = finald;
            c = final;
            d = finald;
        }

    ////////////////////////////////////////////////////
This is my signature
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top