440,602 Members | 1,507 Online Need help? Post your question and get tips & solutions from a community of 440,602 IT Pros & Developers. It's quick & easy.

# Fraction classes

 P: 30 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 Expand|Select|Wrap|Line Numbers 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 Expand|Select|Wrap|Line Numbers 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 Expand|Select|Wrap|Line Numbers  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 Feb 27 '08 #1
3 Replies

 Expert Mod 5K+ P: 9,197 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. Feb 28 '08 #2

 P: 30 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. Feb 28 '08 #3

 Expert Mod 5K+ P: 9,197 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. Feb 29 '08 #4 