rational.h


rational.h

Program listing

/************************************************************************
*									
*	rational.h						
*									
************************************************************************/

#ifndef RATIONAL_H
#define RATIONAL_H

#include 

/************************************************************************
*										
*	Definition of the Rational class	
*										
************************************************************************/

class Rational {
	public:

	//	Constructors

		Rational(int numer = 0, int denom = 1);

	//	Facilitators
	
		Rational Add(const Rational &r) const;
		Rational Subtract(const Rational &r) const;
		Rational Multiply(const Rational &r) const;
		Rational Divide(const Rational &r) const;
		void Insert(ostream &sout) const;
		void Extract(istream &sin);
	
	//	Other member functions
	
		int IntPart();
		Rational FracPart();
		int Round();
		float Approx();
		Rational RatApprox(int demon = 1);
		
	protected:
	
	//	Inspectors
	
		int Numerator() const;
		int Denominator() const;
	
	//	Mutators
	
		void SetNumerator(int numer);
		void SetDenominator(int denom);
	
	//	Facilitators
	
		int gcd(int a, int b) const;
		void Reduce();
		
	private:

	//	Data members
	
		int NumeratorValue;
		int DenominatorValue;
};

//	Operators

Rational operator+(const Rational &r, const Rational &s);
Rational operator-(const Rational &r, const Rational &s);
Rational operator*(const Rational &r, const Rational &s);
Rational operator/(const Rational &r, const Rational &s);

ostream& operator<<(ostream &sout, const Rational &s);
istream& operator>>(istream &sin, Rational &r);

#endif



e-mail me at robbk@hsc.edu

This page was last modified on Sep. 22, 1998

URL: http://people.hsc.edu/faculty-staff/robbk/Coms261/Examples/rational_h.html

Return to Coms 261 home page