Fraction classes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Myxamatosis
    New Member
    • Feb 2008
    • 30

    Fraction classes

    we're given an implementation file to base our class of Fraction off of.

    Input and output are in the format x/y, with x and y being ints, with the "/" character in the middle.

    so to create an object of class Fraction, it'd need to be like
    Code:
    Fraction frac1 (3/4)
    however I'm not sure on how to create this fraction with the format of the .h file we're given
    Code:
    class Fraction
    {
      public:
    
        // Construct fraction from numerator and denominator
        //
        Fraction( int = 0, int = 1 );
    
        // Construct fraction by copying existing fraction
        //
        Fraction( const Fraction& );
    
        // Assign into fraction by copying existing fraction
        //
        Fraction& operator=( const Fraction& );
    
        // Return true if fraction is valid (non-zero denominator)
        //
        bool IsValid() const;
    
        // Return value of numerator
        //
        int Numerator() const;
    
        // Return value of denominator
        //
        int Denominator() const;
    
        // Input/Output operations
        //
        friend istream& operator>>( istream&, Fraction& );
        friend ostream& operator<<( ostream&, const Fraction& );
    I guess what's tripping me up is the format it's sent in, with the "/" character. if it was
    Code:
     Fraction fract1 ( 3,4);
    i'd have no problem.

    If someone could tell me at least how to declare the fraction to be passed around to the other objects, everything else should fall into place

    Thanks Much,
    -Myxamatosis
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Your Fracton class should contain the numerator (an int) and a denominator (anouther int).

    You pass the fraction around by passing a Fraction object around.

    The 3/4 is the mathematical view of the fraction.

    Please note that the mathematical fraction 3/4 is not 0.75.
    0.75 does not have a numerator or denominator.

    Comment

    • Myxamatosis
      New Member
      • Feb 2008
      • 30

      #3
      so this means I am in fact using
      Fraction frac(3,4)

      right?
      If this is the case, I think I'm all set. I'm fairly certain I know how to remove the character from the numbers, especially since we supply the input in the program...it's not interactive.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by Myaxamatosis
        Fraction frac(3,4)
        That looks right. Then you pass frac.

        An overload of the << operator could display frac as 3/4.

        Now you can write other overloads to add, subtract, divide and multiply Fraction objects.

        Comment

        Working...