By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,602 Members | 1,507 Online
Bytes IT Community
+ Ask a Question
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
  1. 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
  1. class Fraction
  2. {
  3.   public:
  5.     // Construct fraction from numerator and denominator
  6.     //
  7.     Fraction( int = 0, int = 1 );
  9.     // Construct fraction by copying existing fraction
  10.     //
  11.     Fraction( const Fraction& );
  13.     // Assign into fraction by copying existing fraction
  14.     //
  15.     Fraction& operator=( const Fraction& );
  17.     // Return true if fraction is valid (non-zero denominator)
  18.     //
  19.     bool IsValid() const;
  21.     // Return value of numerator
  22.     //
  23.     int Numerator() const;
  25.     // Return value of denominator
  26.     //
  27.     int Denominator() const;
  29.     // Input/Output operations
  30.     //
  31.     friend istream& operator>>( istream&, Fraction& );
  32.     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
  1.  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,
Feb 27 '08 #1
Share this Question
Share on Google+
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)

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'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

Post your reply

Sign in to post your reply or Sign up for a free account.