theb3s7
February 26th, 2009, 05:57 PM
Hello!
I've just started to learn classes and I kinda got stuck into something..
I created a "rational" class, where I store "a" and "b" from a fraction "a/b". What I did managed to do is to compare, using the overloaded operators, two "rational" objects or a "rational" object with an integer.
What I can't do, is comparing an integer to a "rational" object. In less words, assuming q,r - "rational" and i - int, I can do q==r, q==i but I can't do an i==q.
Can anyone help me?
This is a small portion, not the whole class:
class rational
{
double nr,num;
public:
rational(int a) { nr=a; num=1; }
bool operator==(rational);
bool operator==(int);
};
bool rational::operator==(rational a) { return (nr==a.nr&&num==a.num);}
bool rational::operator==(int a) { return (nr/num == (double)a); }
I've just started to learn classes and I kinda got stuck into something..
I created a "rational" class, where I store "a" and "b" from a fraction "a/b". What I did managed to do is to compare, using the overloaded operators, two "rational" objects or a "rational" object with an integer.
What I can't do, is comparing an integer to a "rational" object. In less words, assuming q,r - "rational" and i - int, I can do q==r, q==i but I can't do an i==q.
Can anyone help me?
This is a small portion, not the whole class:
class rational
{
double nr,num;
public:
rational(int a) { nr=a; num=1; }
bool operator==(rational);
bool operator==(int);
};
bool rational::operator==(rational a) { return (nr==a.nr&&num==a.num);}
bool rational::operator==(int a) { return (nr/num == (double)a); }