Purchase Solution

C++ - simple fractions

Not what you're looking for?

Ask Custom Question

Design a C++ program containing a class Fraction. The class data should have two variables numerator and denominator, both of type integer, to represent the fraction numerator/denominator. The fraction representation within the object must allow no simplification. That is, the numerator and denominator have to have no common divisor different from 1. For example, the fraction 4/6 must be represented as 2/3.

Be sure to implement the following features:

1. The class constructor must take two integers numbers (numerator and denominator).
2. For negative fractions, the numerator must be negative and denominator must be positive.
3. Include the method simplify that takes two numbers and returns a fraction object (representing a fraction that cannot be simplified).
4. Include a method add that takes two fraction objects and returns their sum (as a fraction object).
5. Include a method diff that takes two fraction objects and returns their difference (as a fraction object).
6. Implement overloaded operators + and - for addition and subtraction of fractions.
7. Implement overloaded comparison operators == and != for comparing two fraction objects.

Test your class in the main() method by creating several fractions, adding them, subtracting them, and printing the result by using the / sign (e.g., 2/3), not as decimal fractions. Also demonstrate usage of the overloaded operators.

Purchase this Solution

Solution Summary

Simple fractions are solved using C++.

Purchase this Solution


Free BrainMass Quizzes
Basic Computer Terms

We use many basic terms like bit, pixel in our usual conversations about computers. Are we aware of what these mean? This little quiz is an attempt towards discovering that.

Excel Introductory Quiz

This quiz tests your knowledge of basics of MS-Excel.

Java loops

This quiz checks your knowledge of for and while loops in Java. For and while loops are essential building blocks for all Java programs. Having a solid understanding of these constructs is critical for success in programming Java.

Inserting and deleting in a linked list

This quiz tests your understanding of how to insert and delete elements in a linked list. Understanding of the use of linked lists, and the related performance aspects, is an important fundamental skill of computer science data structures.

Word 2010: Tables

Have you never worked with Tables in Word 2010? Maybe it has been a while since you have used a Table in Word and you need to brush up on your skills. Several keywords and popular options are discussed as you go through this quiz.