Thread: New to board problems with overloading

  1. #16
    Lurker
    Guest
    You're close with your '+' function, but you're adding the fraction to itself. I think:

    Code:
    value.numerator = (numerator * rat.denominator) + (rat.numerator * denominator);
    value.denominator = denominator * rat.denominator;
    will work. The left side of the + sign should be able to be represented by the 'private' variables of your class. I did the rational class about two months ago, and the good news is once you get one to work you'll be able to do the rest in no time.

  2. #17
    Registered User
    Join Date
    Dec 2002
    Posts
    8
    Im starting to understand this now somewhat. My question is
    Code:
    value.numerator = (numerator * rat.denominator) + (rat.numerator * denominator);
    value.denominator = denominator * rat.denominator;
    how do you know where to place the rat.numerator or rat.denominator

  3. #18
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Originally posted by Stoned_Coder
    No. operator + should always return an object and not a reference. Remember this rule. It will serve you well.
    Yep, you are right. I was thinking of the = operator.

  4. #19
    Lurker
    Guest
    The rat.numerator and rat.denominator are on the right side of the operator. So, if you are adding:

    solution = 2/3 + 3/4

    rat.numerator is 3, and rat.denomator is 4.

  5. #20
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    or you could try this:
    Code:
    class Rational
    {
       public:
         //do dah
         friend Rational & operator * (const Rational & lhs, const Rational & rhs);
         //more
    };
    
    Rational  operator*(const Rational & lhs, const Rational & rhs)
    {
       Rational result;
       result.numerator = lhs. numerator * rhs.numerator;
       result.denominator = lhs.denominator * rhs.denominator;
       return result;
    }
    you can do a similar syntax (specify the parameters on the left and right hand side) for +. Of course, you need to determine the lowest common denominator just like you do when doing this by hand before you can add the numerators and denominators.
    Last edited by elad; 12-18-2002 at 12:27 PM.

  6. #21
    Registered User
    Join Date
    Dec 2002
    Posts
    8
    Originally posted by Lurker
    The rat.numerator and rat.denominator are on the right side of the operator. So, if you are adding:

    solution = 2/3 + 3/4

    rat.numerator is 3, and rat.denomator is 4.
    Thanx for your help I appreciate it. I now understand. Im going to take my final in an hour everyone wish me luck hahahah

  7. #22
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Most modern compilers support something known as the return value optimisation.
    this
    Code:
    Rational  operator*(const Rational & lhs, const Rational & rhs)
    {
       Rational result;
       result.numerator = lhs. numerator * rhs.numerator;
       result.denominator = lhs.denominator * rhs.denominator;
       return result;
    }
    is better written as:-
    Code:
    Rational operator*(const Rational& lhs,const rational& rhs)
    {
        return Rational ((lhs.numerator*rhs.numerator),(lhs.denominator*rhs.denominator));
    }
    This eliminates the otherwise needed temporary.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# vs ASM board
    By Yarin in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 01-28-2009, 05:18 AM
  2. Network Programming Board
    By Eibro in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 07-07-2003, 12:04 PM
  3. rotating the board
    By axon in forum C++ Programming
    Replies: 5
    Last Post: 02-26-2003, 10:09 AM
  4. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM