Create RationalNumber Class and Overload addition, subtraction, multiplication, division and relational and equality operators - C++ Class

C++ examples for Class:Operator Overload

Description

Create RationalNumber Class and Overload addition, subtraction, multiplication, division and relational and equality operators

Demo Code

                                                                                                                                                               
#include <iostream>
                                                                                                                                                               
class RationalNumber {
    friend std::ostream& operator<<(std::ostream&, const RationalNumber&);
    friend std::istream& operator>>(std::istream&, RationalNumber&);
                                                                                                                                                               
 public://  ww w  . j  a  va2s . c  om
    explicit RationalNumber(int = 1, int = 1);
                                                                                                                                                               
    RationalNumber operator+(const RationalNumber&) const;
    RationalNumber operator-(const RationalNumber&) const;
    RationalNumber operator/(const RationalNumber&) const;
    RationalNumber operator*(const RationalNumber&)const;
                                                                                                                                                               
    bool operator==(const RationalNumber&) const;
    bool operator!=(const RationalNumber&) const;
                                                                                                                                                               
 private:
    int numerator;
    int denominator;
                                                                                                                                                               
    void reduce(int, int);
    int gcd(int, int) const;
};
RationalNumber::RationalNumber(int num, int den) {
    if (den < 1) {
        throw new std::invalid_argument(
            "denominator must be greater than zero");
    }
    reduce(num, den);
}
// reduce fractions
void RationalNumber::reduce(int num, int den) {
    int GCD = gcd(num, den);
                                                                                                                                                               
    numerator = num / GCD;
    denominator = den / GCD;
}
// get GCD
int RationalNumber::gcd(int num, int den) const {
    return den == 0 ? num : gcd(den, num % den);
}
// OVERLOADED OPERATORS
RationalNumber RationalNumber::operator+(const RationalNumber& rn) const {
    return RationalNumber(numerator + rn.numerator,denominator + rn.denominator);
}
RationalNumber RationalNumber::operator-(const RationalNumber& rn) const {
    return RationalNumber(numerator - rn.numerator,denominator - rn.denominator);
}
RationalNumber RationalNumber::operator/(const RationalNumber& rn) const {
    return RationalNumber(numerator * rn.denominator,denominator * rn.numerator);
}
RationalNumber RationalNumber::operator*(const RationalNumber& rn) const {
    return RationalNumber(numerator * rn.numerator,denominator * rn.denominator);
}
bool RationalNumber::operator==(const RationalNumber& rn) const {
    return (numerator == rn.numerator && denominator == rn.denominator);
}
bool RationalNumber::operator!=(const RationalNumber& rn) const {
    return !(*this == rn);
}
// IO OPERATORS
std::ostream& operator<<(std::ostream& out, const RationalNumber& rn) {
    out << '(' << rn.numerator << '/' << rn.denominator << ')';
                                                                                                                                                               
    return out;
}
std::istream& operator>>(std::istream& in, RationalNumber& rn) {
    in >> rn.numerator >> rn.denominator;
                                                                                                                                                               
    return in;
}
                                                                                                                                                               
#include <iostream>
                                                                                                                                                               
int main(int argc, const char *argv[]) {
    RationalNumber r1(3, 5);
    RationalNumber r2(9, 4);
                                                                                                                                                               
    std::cout << "r1: " << r1 << "\nr2: " << r2 << std::endl;
                                                                                                                                                               
    RationalNumber r3 = r1 + r2;
                                                                                                                                                               
    std::cout << r1 << " + " << r2 << " = " << r3;
    r3 = r1 - r2;
    std::cout << "\n" << r1 << " - " << r2 << " = " << r3;
    r3 = r1 / r2;
    std::cout << "\n" << r1 << " / " << r2 << " = " << r3;
    r3 = r1 * r2;
    std::cout << "\n" << r1 << " * " << r2 << " = " << r3 << std::endl;
                                                                                                                                                               
    return 0;
}

Result


Related Tutorials