Monday, November 2, 2009

An implementation of a rational class (good use of operator overloading)

Title: Rational class definition
Author: Daniel Tanner
Date: August 2nd, 2005
Class: CPSC 1720

using namespace std;

//Rational Class Definition
class TannRational
//I/O operator Overloaders
friend ostream &operator<<(ostream &strm, TannRational &obj);
friend istream &operator>>(istream &strm, TannRational &obj);
TannRational(int numer, int denom);
void setRational(int numerator, int denominator);
void setRational(TannRational ratio2);
void setNumerator(int numerator);
void setDenominator(int denominator);
int getNumerator()const;
int getDenominator()const;
void printRational();
TannRational add(int numerator2, int denominator2);
TannRational subtract(int numerator2, int denominator2);

//Operator Overloaders
TannRational operator=(const TannRational &right);
TannRational operator+(const TannRational &right)const;
TannRational operator-(const TannRational &right)const;
TannRational operator*(const TannRational &right)const;
TannRational operator/(const TannRational &right)const;
TannRational operator++();
TannRational operator++(int);
TannRational operator--();
TannRational operator--(int);

//Relational Operator Overloaders
bool operator>(const TannRational &right)const;
bool operator<(const TannRational &right)const;
bool operator==(const TannRational &right)const;
bool operator!=(const TannRational &right)const;
bool operator>=(const TannRational &right)const;
bool operator<=(const TannRational &right)const;

//Type Conversion Overloaded Operator
operator double();

int numerator_;
int denominator_;
void reduce();
void zeroError();


No comments:

Post a Comment