Exercise16_04.cpp

# Exercise16_04.cpp - #ifndef _H#define _H#include...

This preview shows pages 1–3. Sign up to view the full content.

#ifndef RATIONALWITHOPERATORS_H #define RATIONALWITHOPERATORS_H #include <string> #include <cstdlib> #include <stdexcept> #include <iostream> using namespace std; class Rational { public: Rational(); Rational(int numerator, int denominator); int getNumerator() const; int getDenominator() const; Rational add(const Rational& secondRational) const; Rational subtract(const Rational& secondRational) const; Rational multiply(const Rational& secondRational) const; Rational divide(const Rational& secondRational) const; int compareTo(const Rational& secondRational) const; bool equals(const Rational& secondRational) const; int intValue() const; double doubleValue() const; string toString() const; Rational(int numerator); // Suitable for type conversion // Define function operators for augmented operators Rational& operator+=(const Rational& secondRational); Rational& operator-=(const Rational& secondRational); Rational& operator*=(const Rational& secondRational); Rational& operator/=(const Rational& secondRational); // Define function operator [] int& operator[](int index); // Define function operators for prefix ++ and -- Rational& operator++(); Rational& operator--(); // Define function operators for postfix ++ and -- Rational operator++(int dummy); Rational operator--(int dummy); // Define function operators for unary + and - Rational operator+(); Rational operator-(); // Define the << and >> operators friend ostream& operator<<(ostream& , const Rational&); friend istream& operator>>(istream& , Rational&); private: int numerator; int denominator; static int gcd(int n, int d); }; // Define nonmember function operators for relational operators bool operator<(const Rational& r1, const Rational& r2); bool operator<=(const Rational& r1, const Rational& r2); bool operator>(const Rational& r1, const Rational& r2); bool operator>=(const Rational& r1, const Rational& r2); bool operator==(const Rational& r1, const Rational& r2);

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
bool operator!=(const Rational& r1, const Rational& r2);
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern