Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first.

/* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first. * */ #include  #include  #include  using namespace std; class Fraction { public: // constructor Fraction(int a, int b); // generate a fraction which is a/b Fraction(int a); // generate a fraction which is a/1 Fraction(); // generate a fraction which is 0/1. i.e 0 // member functions int get_numerator() const; // return the numerator of the fraction int get_denominator() const; // return the denominator of the fraction void reduce(); // reduce this fraction to simplest form. For instance, // 2/4 will be reduced to 1/2 Fraction reciprocal() const; // return the reciprocal of this Fraction // friend functions friend Fraction operator +(const Fraction& f1, const Fraction& f2); // return the sum of f1 and f2, // the result is reduced friend Fraction operator -(const Fraction& f1, const Fraction& f2); // return the difference of f1 and f2, // the result is reduced friend Fraction operator *(const Fraction& f1, const Fraction& f2); // return the product of f1 and f2, // the result is reduced friend Fraction operator /(const Fraction& f1, const Fraction& f2); // return the quotient of f1 and f2, // the result is reduced friend Fraction operator -(const Fraction& f); // return the negation of f friend bool operator  (const Fraction& f1, const Fraction& f2); // return true if f1 is greater than f2. // False otherwise friend bool operator = (const Fraction& f1, const Fraction& f2); // return true if f1 is greater or equal to f2. // False otherwise friend bool operator == (const Fraction& f1, const Fraction& f2); // return true if f1 is equal to f2. // False otherwise friend bool operator != (const Fraction& f1, const Fraction& f2); // return true if f1 is not equal to f2. // False otherwise friend istream& operator >> (istream& in, Fraction& f); // input f in the form of a/b, where b cannot be zero. Also, // if b is negative, the Fraction will change b to be positive. // So, again, 1/-3 will be changed to -1/3 friend ostream& operator , =, > and  0) cout  1.4) cout  0) cout  1.4) cout  0) cout  2.9) cout  if(f1 > f2 || !(f2 > f1) || f1 > f3) { cout  operator was wrong. "; result -= 0.5; } // Test == if(f1 == f2 || !(f1 == f3) || f2 == f3) { cout = if(f1 >= f2 || !(f1 >= f3) || !(f2 >= f3)) { cout  0) cout  1.9) cout > f; if(f.get_denominator() != 4 || f.get_numerator() != -3) { cout > choice; if(choice != 'y' && choice != 'Y') { cout  0) cout  0.9) cout  

image text in transcribed

Define a class for rational numbers. A rational number is a number that can be represented as the quotient of two integers. For example, 1/2, 3/4 64/2, and so forth are all rational numbers. (By 1/2, etc., we mean the everyday meaning of the fraction, not the integer division this expression would produce in a C++ program.) Represent rational numbers as two val- ues of type int, one for the numerator and one for the denominator. Call the class Rational Include a constructor with two arguments that can be used to set the member variables of an object to any legitimate values. Also include a constructor that has only a single parameter of type int; call this single parameter whole_number and define the constructor so that the object will be initialized to the rational number whole_number/1. Also include a default constructor that initializes an object to 0 (that is, to 0/1). Overload the input and output operators >> and , >=, +, -, *, and I. Also write a test program to test your class

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Flash XML Applications Use AS2 And AS3 To Create Photo Galleries Menus And Databases

Authors: Joachim Schnier

1st Edition

0240809173, 978-0240809175

More Books

Students also viewed these Databases questions