1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
#include <iostream>
using namespace std;
class Rational
{
int num;
int den;
public:
Rational(); //Default set to 0/1
Rational(int num);//set to whole number over 1
Rational(int num, int den); //set to any values
void setnum(int);
void setden(int);
void simplify(int num, int den); //simplify rational number
void displayRational(); // displays rational number
friend istream& operator>>(istream& ins, Rational& ratnum); //input opperator overload
friend ostream& operator>>(ostream& outs, const Rational& ratnum); //output operator overload
friend Rational operator+(const Rational& ratnum1, const Rational& ratnum2); //addition
friend Rational operator-(const Rational& ratnum1, const Rational& ratnum2); //subtraction
friend Rational operator*(const Rational& ratnum1, const Rational& ratnum2); //multiply
friend Rational operator/(const Rational& ratnum1, const Rational& ratnum2); //divide
friend bool operator<(const Rational & ratnum1, const Rational & ratnum2); //less than
friend bool operator>(const Rational & ratnum1, const Rational & ratnum2); //greater than
friend bool operator<=(const Rational & ratnum1, const Rational & ratnum2); //less than or equal to
friend bool operator>=(const Rational & ratnum1, const Rational & ratnum2); //greater than or equal to
friend bool operator==(const Rational & ratnum1, const Rational & ratnum2); //equal to
};
Rational::Rational(): num(0), den(1)
{}//default constructor
Rational::Rational(int num): num(num), den(1)//sets numerator to whole number
{}
Rational::Rational(int num, int den): num(num), den(den)//sets to any value
{}
//simplify
void Rational::displayRational()// displays a rational number
{
cout << num <<"/"<< den << endl;
}
//input
ostream& operator << (ostream& outs, const Rational& RationalNmber) //output
{
int num, den;
outs << num << "/" << den ;
return outs;
}
int main()
{
Rational test1, test2(10), test3(1, 2);
test1.displayRational();
test2.displayRational();
test3.displayRational();
cout << "\nTest1 equals " << test1;
cout << "\nTest2 equals " << test2;
cout << "\nTest3 equals " << test3;
system ("pause");
return 0;
}
|