SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Operator Overloading Friends And References
Exercise:
Programming Projects
Question:2 | ISBN:9780132846813 | Edition: 5

Question

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 and so on we mean the everyday fraction, not the integer division this expression would produce in a C++ program.) Represent rational numbers as two values 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 wholeNumber and define the constructor so that the object will be initialized to the rational number wholeNumber /1. Include a default constructor that initializes an object to 0 (that is, to 0/1). Overload the input and output operators >> and <<. Numbers are to be input and output in the form 1/2, 15/32, 300/401, and so forth. Note that the numerator, the denominator, or both may contain a minus sign, so -1/2 , 15/-32 , and -300/-401 are also possible inputs. Overload all the following operators so that they correctly apply to the type Rational : ==, <, <=, >, >=, +, -, *, and /. Write a test program to test your lass. Hints: Two rational numbers a/b and c/d are equal if a*d equals c*b. If b and d are positive rational numbers, a/b is less than c/d provided a*d is less than c*b . You should include a function to normalize the values stored so that, after normalization, the denominator is positive and the numerator and denominator are as small as possible. For example, after normalization 4/-8 would be represented the same as -1/2 .


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

C++ program code:

//header section
#include <iostream>
#include <cmath>
using namespace std;
//Create a class name, Rational
class Rational 
{
private:
      //Declare variables
    int numerator;
    int denominator;

public:
    //Construtor with arguments
    Rational(int num = 0, int den = 1) 
    {
        numerator = num;
        denominator = den;
        normalize();
    }
    //The normalize function is defined to normalize the rational 
    //number by ensuring the denominator is positive and reducing 
    //the fraction to its simplest form.
    void normalize()
    {
        if (denominator < 0) 
        {
            numerator *= -1;
            denominator *= -1;
        }

        int gcd =__gcd(abs(numerator), denominator);
        numerator /= gcd;
        denominator /= gcd;
    }
    //The operator<< function overloads the insertion operator
    //to output the rational number in the form "numerator/denominator".
    friend ostream& operator<<(ostream& os, const Rational& rational) 
    {
        os << rational.numerator << "/" << rational.denominator;
        return os;
    }
    //The operator>> function overloads the extraction operator
    //to input the rational number in the form "numerator/denominator" and normalize it.
    friend istream& operator>>(istream& is, Rational& rational) 
    {
        char slash;
        is >> rational.numerator >> slash >> rational.denominator;
        rational.normalize();
        return is;
    }
    //The comparison operators (==, <, <=, >, >=) are overloaded 
    //to compare two rational numbers based on their values.
    bool operator==(const Rational& other) const 
    {
        return (numerator == other.numerator) && (denominator == other.denominator);
    }

    bool operator<(const Rational& other) const {
        return (numerator * other.denominator) < (denominator * other.numerator);
    }

    bool operator<=(const Rational& other) const {
        return (*this < other) || (*this == other);
    }

    bool operator>(const Rational& other) const {
        return !(*this <= other);
    }

    bool operator>=(const Rational& other) const {
        return !(*this < other);
    }
    //The arithmetic operators (+, -, *, /) are overloaded to perform addition,
    //subtraction, multiplication, and division on rational numbers.
    Rational operator+(const Rational& other) const {
        int num = (numerator * other.denominator) + (other.numerator * denominator);
        int den = denominator * other.denominator;
        return Rational(num, den);
    }

    Rational operator-(const Rational& other) const {
        int num = (numerator * other.denominator) - (other.numerator * denominator);
        int den = denominator * other.denominator;
        return Rational(num, den);
    }

    Rational operator*(const Rational& other) const {
        int num = numerator * other.numerator;
        int den = denominator * other.denominator;
        return Rational(num, den);
    }

    Rational operator/(const Rational& other) const {
        int num = numerator * other.denominator;
        int den = denominator * other.numerator;
        return Rational(num, den);
    }
};

int main() 
{
    //Create two Rational objects, r1 and r2, are created with initial values.
    Rational r1(1, 2);
    Rational r2(3, 4);

    cout << "r1: " << r1 << endl;
    cout << "r2: " << r2 << endl;
    //Call the methods and output
    cout << "r1 == r2: " << boolalpha << (r1 == r2) << endl;
    cout << "r1 < r2: " << boolalpha << (r1 < r2) << endl;
    cout << "r1 <= r2: " << boolalpha << (r1 <= r2) << endl;
    cout << "r1 > r2: " << boolalpha << (r1 > r2) << endl;
    cout << "r1 >= r2: " << boolalpha << (r1 >= r2) << endl;

    Rational sum = r1 + r2;
    Rational difference = r1 - r2;
    Rational product = r1 * r2;
    Rational quotient = r1 / r2;

    cout << "Sum: " << sum << endl;
    cout << "Difference: " << difference << endl;
    cout << "Product: " << product << endl;
    cout << "Quotient: " << quotient << endl;

    return 0;
}

Output of the program code:

r1: 1/2
r2: 3/4
r1 == r2: false
r1 < r2: true
r1 <= r2: true
r1 > r2: false
r1 >= r2: false
Sum: 5/4
Difference: -1/4
Product: 3/8
Quotient: 2/3

 

0 0

Discussions

Post the discussion to improve the above solution.