Solved

# Rational Number w/Operator Overload

Posted on 2005-05-01

>>This code does not compile, please advise. Del

*************************code**************************

#ifndef RAT_H

#define RAT_H

class Rational {

public:

Rational(int t = 0, int n = 1);

friend Rational operator + (const Rational& lVal, const Rational& rVal);

friend Rational operator - (const Rational& lVal, const Rational& rVal);

friend Rational operator * (const Rational& lVal, const Rational& rVal);

friend Rational operator / (const Rational& lVal, const Rational& rVal);

Rational& operator += (const Rational& rVal);

Rational& operator -= (const Rational& rVal);

Rational& operator *= (const Rational& rVal);

Rational& operator /= (const Rational& rVal);

friend int operator < (const Rational& lVal, const Rational& rVal);

friend int operator <= (const Rational& lVal, const Rational& rVal);

friend int operator == (const Rational& lVal, const Rational& rVal);

friend int operator != (const Rational& lVal, const Rational& rVal);

friend int operator >= (const Rational& lVal, const Rational& rVal);

friend int operator > (const Rational& lVal, const Rational& rVal);

friend ostream& operator << (ostream& os, const Rational& rat);

friend istream& operator >> (istream& is, Rational& rat);

private:

int teller, number;

void reduce();

int number(int, int);

};

Rational::Rational(int t, int n): teller(t), number(n) {

reduce();

}

Rational Rational::operator -() const {

return Rational(-teller, number);

}

// operator overloading

Rational operator + (const Rational& lVal, const Rational& rVal) {

Rational result;

result.teller = lVal.teller * rVal.number + lVal.number * rVal.teller;

result.number = lVal.number * rVal.number;

result.reduce();

return result;

}

Rational operator - (const Rational& lVal, const Rational& rVal) {

Rational result;

result = lVal +- rVal;

result.reduce();

return result;

}

Rational operator * (const Rational& lVal, const Rational& rVal) {

Rational result;

result.teller = lVal.teller * rVal.teller;

result.number = lVal.number * rVal.number;

result.reduce();

return result;

}

Rational operator / (const Rational& lVal, const Rational& rVal) {

Rational result;

result.teller = lVal.teller * rVal.number;

result.number = lVal.number * rVal.teller;

result.reduce();

return result;

}

Rational& operator += (const Rational& rVal) {

teller = teller * rVal.number + number * rVal.teller;

number = number * rVal.number;

reduce();

return *this;

}

Rational& operator -= (const Rational& rVal) {

this->operator += (-rVal);

reduce();

return *this;

}

Rational& operator *= (const Rational& rVal) {

teller = teller * rVal.teller;

number = number * rVal.number;

reduce();

return *this;

}

Rational& operator /= (const Rational& rVal) {

teller = teller * rVal.number;

number = number * rVal.teller;

reduce();

return *this;

}

int operator < (const Rational& lVal, const Rational& rVal) {

return(lVal.toDouble() < rVal.toDouble());

}

int operator <= (const Rational& lVal, const Rational& rVal) {

return(lVal < rVal || lVal == rVal);

}

int operator == (const Rational& lVal, const Rational& rVal) {

return lVal.teller * rVal.number == lVal.number * rVal.teller;

}

int operator != (const Rational& lVal, const Rational& rVal) {

return !(lVal == rVal);

}

int operator >= (const Rational& lVal, const Rational& rVal) {

return(lVal > rVal || lVal == rVal);

}

int operator > (const Rational& lVal, const Rational& rVal) {

return(lVal.toDouble() > rVal.toDouble());

}

istream& operator >> (istream& is, Rational& rat) {

int t, n;

char deel;

cin >> is >> t >> deel >> n;

rat = Rational(t,n);

return is;

}

#endif

********************

// main.cpp

#include "rat.h"

#include <iostream>

#include <cstring>

#include <cstdlib>

using namespace std;

int main() {

Rational r;

r.clrscr();

r.Rational b1;

r.Rational b2;

cout << "Input the number in the form (a/b): ";

cin >> b1 >> b2;

// Test for +, -, *, /

cout << b1 << " + " << b2 << " = " << b1 + b2 << endl;

cout << b1 << " - " << b2 << " = " << b1 - b2 << endl;

cout << b1 << " * " << b2 << " = " << b1 * b2 << endl;

cout << b1 << " / " << b2 << " = " << b1 / b2 << endl;

// test program for overloaded operators

cout << b1 << " < " << b2;

(b1 < b2) ? cout << " [X]" << endl : cout << " [ ]" << endl;

cout << b1 << " <= " << b2;

(b1 <= b2) ? cout << " [X]" << endl << endl : cout << " [ ]" << endl << endl;

cout << b1 << " == " << b2;

(b1 == b2) ? cout << " [X]" << endl : cout << " [ ]" << endl;

cout << b1 << " != " << b2;

(b1 != b2) ? cout << " [X]" << endl << endl : cout << " [ ]" << endl << endl;

cout << b1 << " >= " << b2;

(b1 >= b2) ? cout << " [X]" << endl : cout << " [ ]" << endl;

cout << b1 << " > " << b2;

(b1 > b2) ? cout << " [X]" << endl : cout << " [ ]" << endl << endl;

getch();

return 0;

}

*************

output = multiple errors.