Question

Do the following: 1) Add a constructor with two parameters, one for the numerator, one for...

Do the following:

1) Add a constructor with two parameters, one for the numerator, one for the denominator. If the parameter for the denominator is 0, set the denominator to 1

2) Add a function that overloads the < operator

3) Add a function that overloads the * operator

4) Modify the operator<< function so that if the numerator is equal to the denominator it just prints 1

5) Modify the operator>> function so that after it reads the denominator it sets it to 1 if it is 0

6) Modify the main program so that it tests the 3 functions you created and the 2 functions you modified

7) Add comments to the 3 files

//main.cpp

#include "Fraction.h"

int main ()

{

Fraction f1,f2,f3;

cout<< "Enter a fraction in the form a/b ";

cin >> f1;

cout<< "Enter another fraction in the form a/b ";

cin >> f2;

if ( f1 == f2 )

cout << "\nThe two fractions are equal\n";

else cout <<"\nThe two fractions are different\n";

f3 = f1 + f2;

cout << f1 << " + " << f2 << " = " << f3;

return 0;

}

//Fraction.cpp

#include "Fraction.h"


Fraction :: Fraction()

{

numerator = 0;

denominator = 1;

}

ostream& operator<< ( ostream& ofile, const Fraction &f )

{

ofile<< f.numerator;

ofile<< " / ";

ofile<<f.denominator;

return ofile;

}

istream& operator>> ( istream &ifile, Fraction &f )

{

char ch;

ifile>> f.numerator;

ifile>> ch; // read and discard the symbol /

ifile>> f.denominator;

return ifile;

}

Fraction Fraction :: operator+ ( const Fraction &other)

{

Fraction temp;

temp.numerator = numerator*other.denominator +

denominator*other.numerator;

temp.denominator = denominator * other.denominator;

//C++

return temp;

}

bool Fraction :: operator== ( const Fraction &other )

{

return ( numerator*other.denominator ==

denominator*other.numerator);

}



//Fraction.h

#include <iostream>

using namespace std;

#ifndef FRACTION_H

#define FRACTION_H

class Fraction

{

friend ostream& operator<< (ostream&, const Fraction& );

friend istream& operator>> (istream &, Fraction &);

private:

int numerator;

int denominator;

public:

Fraction();

Fraction operator+ ( const Fraction& );

bool operator== ( const Fraction& );

};

#endif



0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

//Fraction.h

//Fraction.h

#include <iostream>

using namespace std;

#ifndef FRACTION_H

#define FRACTION_H

class Fraction

{

friend ostream& operator<< (ostream&, const Fraction& );

friend istream& operator>> (istream &, Fraction &);

private:

int numerator;

int denominator;

public:

//Add a constructor with two parameters, one for the numerator, one for the denominator. If the parameter for the denominator is 0, set the denominator to 1

Fraction(int n=0,int d=1);

Fraction operator+ ( const Fraction& );

//2) Add a function that overloads the < operator

//3) Add a function that overloads the * operator

bool operator< ( const Fraction& );

Fraction operator* ( const Fraction& );

bool operator== ( const Fraction& );

};

#endif

//Fraction.cpp

//Fraction.cpp

#include "Fraction.h"

Fraction :: Fraction(int n,int d)

{

numerator = n;

if(d==0){

denominator = 1;

}else{

denominator = d;

}

}

ostream& operator<< ( ostream& ofile, const Fraction &f )

{

// if the numerator is equal to the denominator it just prints 1

if(f.numerator==f.denominator){

ofile<< 1;

return ofile;

}

ofile<< f.numerator;

ofile<< " / ";

ofile<<f.denominator;

return ofile;

}

istream& operator>> ( istream &ifile, Fraction &f )

{

char ch;

ifile>> f.numerator;

ifile>> ch; // read and discard the symbol /

ifile>> f.denominator;

// after it reads the denominator it sets it to 1 if it is 0

if (f.denominator==0){

f.denominator=1;

}

return ifile;

}

Fraction Fraction :: operator+ ( const Fraction &other)

{

Fraction temp;

temp.numerator = numerator*other.denominator +

denominator*other.numerator;

temp.denominator = denominator * other.denominator;

return temp;

}

Fraction Fraction :: operator* ( const Fraction &other)

{

Fraction temp;

temp.numerator = numerator*other.numerator;

temp.denominator = denominator * other.denominator;

return temp;

}

bool Fraction :: operator< ( const Fraction &other )

{

if(denominator != other.denominator){

int a=numerator*other.denominator;

int b=other.numerator*denominator;

if (a<b){

return true;

}else{

return false;

}

}

return numerator<other.numerator;

}

bool Fraction :: operator== ( const Fraction &other )

{

return ( numerator*other.denominator == denominator*other.numerator);

}

//main.cpp

#include "Fraction.h"

using namespace std;

int main ()

{

Fraction f1,f2,f3;

cout<< "Enter a fraction in the form a/b ";

cin >> f1;

cout<< "Enter another fraction in the form a/b ";

cin >> f2;

if ( f1 == f2 ){

cout << "\nThe two fractions are equal\n";

}

else {cout <<"\nThe two fractions are different\n";

}

f3 = f1 + f2;

cout << f1 << " + " << f2 << " = " << f3 <<endl;

//Modify the main program so that it tests the 3 functions you created and the 2 functions you modified

//testing the constructor

Fraction f4(5,5);

//testing the modified <<

cout<<"f4 = "<<f4<<endl;

//testing the constructor

Fraction f5;

//testing the modified >>

cout<< "Enter a fraction in the form a/b ";

cin>>f5;

cout<<f5<<endl;

Fraction f6(1,2);

Fraction f7(4,6);

if(f6<f7){

cout<<f6 <<" is less than "<< f7<<endl;

}

else{

cout<<f6 <<" is not less than "<< f7<<endl;

}

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Do the following: 1) Add a constructor with two parameters, one for the numerator, one for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please use C++ and add comments to make it easier to read. Do the following: 1)...

    Please use C++ and add comments to make it easier to read. Do the following: 1) Add a constructor with two parameters, one for the numerator, one for the denominator. If the parameter for the denominator is 0, set the denominator to 1 2) Add a function that overloads the < operator 3) Add a function that overloads the * operator 4) Modify the operator<< function so that if the numerator is equal to the denominator it just prints 1...

  • In this assignment you are required to complete a class for fractions. It stores the numerator...

    In this assignment you are required to complete a class for fractions. It stores the numerator and the denominator, with the lowest terms. It is able to communicate with iostreams by the operator << and >>. For more details, read the header file. //fraction.h #ifndef FRACTION_H #define FRACTION_H #include <iostream> class fraction { private: int _numerator, _denominator; int gcd(const int &, const int &) const; // If you don't need this method, just ignore it. void simp(); // To get...

  • You have been developing a Fraction class for Teacher’s Pet Software that contains several fields and...

    You have been developing a Fraction class for Teacher’s Pet Software that contains several fields and functions. a. Add four arithmetic operators, +, -, *, and /. Remember that to add or subtract two Fractions, you first must convert them to Fractions with a common denominator. You multiply two Fractions by multiplying the numerators and multiplying the denominators. You divide two Fractions by inverting the second Fraction, then multiplying. After any arithmetic operation, be sure the Fraction is in proper...

  • C++ CODE /* This is program project 2 on page 695. * Before you begin the...

    C++ CODE /* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first. * * Author: Your Name * Version: Dates */ #include <iostream> #include <cmath> #include <cassert> 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...

  • C++ CODE /* This is program project 2 on page 695. * Before you begin the...

    C++ CODE /* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first. * * Author: Your Name * Version: Dates */ #include <iostream> #include <cmath> #include <cassert> 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...

  • NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------...

    NEED ASAP PLEASE HELP Task: --------------------------------------------------------------------------------------------------------------------------------- Tasks to complete: ---------------------------------------------------------------------------------------------------------------------------------------- given code: ----------------------------------------------------------------------------------------------------------------------------------------------- main.cpp #include <iostream> #include <iomanip> #include "fractionType.h" using namespace std; int main() { fractionType num1(5, 6); fractionType num2; fractionType num3; cout << fixed; cout << showpoint; cout << setprecision(2); cout << "Num1 = " << num1 << endl; cout << "Num2 = " << num2 << endl; cout << "Enter the fraction in the form a / b: "; cin >> num2; cout << endl; cout <<...

  • Hello, I'm looking to modify my fraction program to include constructors and destructors. Here are all...

    Hello, I'm looking to modify my fraction program to include constructors and destructors. Here are all the instructions: Create constructors default two argument three argument copy constructor Create a destructor. The destructor should set whole and numerator to zero and denominator to one. Add cout statements to the constructors and the destructor so you know when it's getting executed. Add system ("pause") to the destructor so you know when it executes. Only positive values allowed If denominator is 0 set...

  • Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and...

    Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and user_main.cpp. The programming language is C++. I will provide the Sample Test Driver Code as well as the codes given so far in text below. Sample Testing Driver Code: cs52::FlashDrive empty; cs52::FlashDrive drive1(10, 0, false); cs52::FlashDrive drive2(20, 0, false); drive1.plugIn(); drive1.formatDrive(); drive1.writeData(5); drive1.pullOut(); drive2.plugIn(); drive2.formatDrive(); drive2.writeData(2); drive2.pullOut(); cs52::FlashDrive combined = drive1 + drive2; // std::cout << "this drive's filled to " << combined.getUsed( )...

  • In the following code, it gets hung up at    cout << "Number1 * Number2 =...

    In the following code, it gets hung up at    cout << "Number1 * Number2 = " << number1 * number2 << endl; giving an error of "no math for operator<<" what am i doing wrong? Thank you #include <iostream> #include <cctype> #include <cstdlib> using namespace std; class Rational //class for rational numbers (1/2, 5/9, ect..) {    public:        Rational(int numerator, int denominator);        Rational(int numberator);        Rational(); //default        friend istream& operator >>(istream& ins,...

  • If you have already answered this question, please do not repost your old solutions, you will be thumbs downed. I'm...

    If you have already answered this question, please do not repost your old solutions, you will be thumbs downed. I'm looking for NEW solutions only! Frac.h: // a Fraction object holds one Fraction number, one fraction #ifndef FRAC_H #define FRAC_H #include <iostream> using namespace std; //Creaing a Fraction class class Fraction { public: Fraction(int = 0, int = 1); // Function Declarations which performs operations on Fraction class Fraction add(const Fraction &); Fraction subtract(const Fraction& a); Fraction multiply(const Fraction& a);...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT