Create Rational Class to do arithmetic with fractions. - C++ Class

C++ examples for Class:Operator Overload

Description

Create Rational Class to do arithmetic with fractions.

Demo Code

                                                                                                                                             
#include <iostream>
                                                                                                                                             
class Rational {/*from w  w w.j a  va  2s .c om*/
 public:
    explicit Rational(int = 1, int = 2);
    ~Rational();
                                                                                                                                             
    Rational operator+(const Rational& r) const {
        return Rational(num + r.num, den + r.den);
    }
    Rational operator-(const Rational& r) const {
        return Rational(num - r.num, den - r.den);
    }
    Rational operator*(const Rational& r) const {
        return Rational(num * r.num, den * r.den);
    }
    Rational operator/(const Rational& r) const {
        return Rational(num / r.num, den / r.den);
    }
                                                                                                                                             
    friend std::ostream& operator<<(std::ostream& out, Rational& r) {
        return r.printFloating(out);
    }
                                                                                                                                             
    void reduce();
    int gcd(int, int);
                                                                                                                                             
    void print();
                                                                                                                                             
 private:
    int num;
    int den;
                                                                                                                                             
    std::ostream& printFloating(std::ostream&);
};
Rational::Rational(int n, int d) : num(n), den(d) { reduce(); }
Rational::~Rational() {}
// reduce fractions
void Rational::reduce() {
    int GCD = gcd(num, den);
                                                                                                                                             
    num /= GCD;
    den /= GCD;
}
int Rational::gcd(int a, int b) {
    if (b == 0) {
        return a;
    }
                                                                                                                                             
    return gcd(b, a % b);
}
// print rational number
std::ostream& Rational::printFloating(std::ostream& out) {
    return out << num << "." << den;
}
void Rational::print() { std::cout << num << "/" << den; }
                                                                                                                                             
int main(int argc, const char *argv[]) {
    Rational r1;
    Rational r2(25, 50);
                                                                                                                                             
    std::cout << "r1: " << r1 << "\nr2: " << r2 << std::endl;
                                                                                                                                             
    Rational r3 = (r1 + r2);
                                                                                                                                             
    return 0;
}

Result


Related Tutorials