### HELP: Rational Number Class - Overloading the comparison, insertion & extraction operators

For this program, I have to write a class for Rational Number and do the following:

1) Two data members to store numerator and denominator.

2) Overloaded constructors (by default a rational number should be initialized to 1/1).

3) A reduce() method to store the rational number in the simplest form.

I am having problems with the operator overloading of ==, >, << and >>. If anyone can help me with this I would really appreciate it.

Here's what I have come up with:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134`` ``````#include #include using namespace std; class rational { int n,d; public: rational() : n(1), d(1) {}; ~rational(){}; void getData() { cout<<"\nEnter a numerator: "; cin>>n; cout<<"Enter a denominator: "; cin>>d; cout<>d; } while(d<0) { n *= -1; d *= -1; } } int GCD(int n1, int remainder) { if(remainder==0) return(n1); else { return(GCD(remainder,n1%remainder)); } } void reduce(int &n,int &d) { int rdc = 0; if(d>n) rdc = GCD(d,n); else if(d>n; switch(n) { case 1: c1+c2; getch(); break; case 2: c1-c2; getch(); break; case 3: c1*c2; getch(); break; case 4: c1/c2; getch(); break; default: cout<<"Invalid choice."<

ps: I have already taken some help from this forum regarding the reduce function. So, thanks a lot for that :)
Last edited on
I have absolutely no idea how, in my case, operator overloading is done of these operators ==, >, <<, >>. I have read about them on this forum but am still unable to use them in this program :( If anyone can provide me the code for these I'd be really grateful!

is this what i am supposed to do?

 ``1234567`` ``````void operator ==(rational c1) { rational temp; temp.n=(n==c1.n); temp.d=(d==c1.d); cout<<"\nAfter operating the rational numbers are: "<
Last edited on
Topic archived. No new replies allowed.