Object not actually being modified by a class method
I have this constructor for my rational class:
1 2 3 4 5 6
|
rational(int a,int b)
{
num=a;
denom=b;
simplify();
}
| |
simplify() is a private method within the same class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
void simplify()
{
if(denom<0)
{
num = num * (-1);
denom = denom * (-1);
}
int x=gcd(num,denom);
if(x!=1)
{
num /= x;
denom /= x;
}
}
| |
In main :
1 2 3 4 5 6 7 8
|
int main()
{
rational a(3,-2);
cout<<a.get_num()<<" "<<a.get_denom();
system("pause");
return 0;
}
| |
ok so it should actually set a to be -3,2 but it displays 3,-2. Everything seems to be correct and it's driving me crazy..
Last edited on
Does your gcd function handle negative values? gcd should never return a negative value.
Last edited on
Oh yeah that was it.. gcd was -1 and it was dividing by -1 ... Thanks.
Topic archived. No new replies allowed.