As a starting point, use the files fraction.h, fraction.cpp, testfraction.cpp below:     // simple class for fractions #ifndef FRACTION_H_ #define FRACTION_H_ class Fraction {

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

As a starting point, use the files fraction.h, fraction.cpp, testfraction.cpp below:

 

 

// simple class for fractions

#ifndef FRACTION_H_
#define FRACTION_H_

class Fraction {

private:                        // internal implementation is hidden
    int num;                    // numerator
    int den;                    // denominator

    int gcd(int a, int b);        // calculates the gcd of a and b
    int lcm(int a, int b);

public:
    Fraction();                    // empty constructor
    Fraction(int, int = 1);     // constructor taking values for fractions and
                                // integers. Denominator by default is 1
    void print();                // prints it to the screen
};


#endif /* FRACTION_H_ */

 

#include <iostream>
#include "fraction.h"

Fraction::Fraction()
{
    num = 1;
    den = 1;
}

Fraction::Fraction(int n, int d)
{
    int tmp_gcd = gcd(n, d);

    num = n / tmp_gcd;
    den = d / tmp_gcd;
}

int Fraction::gcd(int a, int b)
{
    int tmp_gcd = 1;

    // Implement GCD of two numbers;

    return tmp_gcd;
}

int Fraction::lcm(int a, int b)
{
    return a * b / gcd(a, b);

}

void Fraction::print()
{
    std::cout << num << "/" << den << std::endl;
}

 

#include <iostream>
#include "fraction.h"

using namespace std;

int main(void)
{
    Fraction a(4, 2);
    Fraction b(17, 11);
    Fraction c(5);

    a.print(); 
    b.print(); 
    c.print(); 
}

 

 

 

Replace the method print () by an overloaded operator << such that you can use cout <<
for printing a fraction on the screen.
• Overload the operator >> such that you can enter from the keyboard a fraction using
cin >>. Check the validity of the input (you can assume that the numerator and denomi-
nator will be numbers).
• Overload the operators * and / for computing the multiplication and division of two frac-
tions.
• In your testing program you should then be able to enter two fractional numbers (using
cin >>), then the product and quotient are printed on the screen (one per line using the
overloaded operator and cout <<).
Transcribed Image Text:Replace the method print () by an overloaded operator << such that you can use cout << for printing a fraction on the screen. • Overload the operator >> such that you can enter from the keyboard a fraction using cin >>. Check the validity of the input (you can assume that the numerator and denomi- nator will be numbers). • Overload the operators * and / for computing the multiplication and division of two frac- tions. • In your testing program you should then be able to enter two fractional numbers (using cin >>), then the product and quotient are printed on the screen (one per line using the overloaded operator and cout <<).
Expert Solution
steps

Step by step

Solved in 5 steps with 4 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY