473,387 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
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:
  4.  
  5.     // Construct fraction from numerator and denominator
  6.     //
  7.     Fraction( int = 0, int = 1 );
  8.  
  9.     // Construct fraction by copying existing fraction
  10.     //
  11.     Fraction( const Fraction& );
  12.  
  13.     // Assign into fraction by copying existing fraction
  14.     //
  15.     Fraction& operator=( const Fraction& );
  16.  
  17.     // Return true if fraction is valid (non-zero denominator)
  18.     //
  19.     bool IsValid() const;
  20.  
  21.     // Return value of numerator
  22.     //
  23.     int Numerator() const;
  24.  
  25.     // Return value of denominator
  26.     //
  27.     int Denominator() const;
  28.  
  29.     // Input/Output operations
  30.     //
  31.     friend istream& operator>>( istream&, Fraction& );
  32.     friend ostream& operator<<( ostream&, const Fraction& );
  33.  
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,
-Myxamatosis
Feb 27 '08 #1
3 3083
weaknessforcats
9,208 Expert Mod 8TB
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
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
weaknessforcats
9,208 Expert Mod 8TB
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

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

Similar topics

9
by: John Cho | last post by:
// CHO, JOHN #include<iostream> class fracpri{ int whole; int numer; int denom;
0
by: amarok | last post by:
Hello all. I'm a Software Engineering student, and I'm attempting to write a program in Java that does as follows: UML for the class: Fraction() Fraction(numerator: int) ...
6
evilmonkey
by: evilmonkey | last post by:
I am very new to programming as well as Java and this is my first post so please forgive me if this is not quite posted correctly. My Problem is that I have only been using scanner to get user input...
7
by: d0ugg | last post by:
Hi, Iam writing a code for my class and I cant figure out how to solve it. I will be glad if somebody can teach me how to do something to correct it. The program is divided it 3 parts. ...
1
by: d0ugg | last post by:
Hi, I'm did a fraction program for one of my programming classes and it did compile, however when I'm running the program it crashes for some reason that I do not know. // fraction.cpp ...
2
by: d0ugg | last post by:
Hi, I'm doing a FRACTION program for one of my Programming classes and I'm getting some errors that I can't figure it out. Here is the Assignment: 1. Convert the fraction structure into a...
4
by: d0ugg | last post by:
Hello everyone, I'm creating a program that it is suppose to add, subtract, multiply and also divide fractions and after that, the result has to be reduced to the lowest terms. However, I'm not...
4
by: Semajthewise | last post by:
Hi All For those of you that helped me with the questions on this... Thank you! I believe I have solved all the bugs in the code and wanted to post the final product. This is a set of code to solve...
1
by: jrw133 | last post by:
So i was given this program in class. i am supposed to create a four-function calculator for fractions using a fraction class. Heres what the requirements are:create a member function for each of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.