i have the following for my operator overload

fraction fraction::operator+(const fraction& object)const
{
	fraction temp;
	temp.a = a*object.b + b*object.a;
	temp.b = b * object.b;
	return temp;
}

fraction fraction::operator-(const fraction& object) const
{
	fraction temp;
	temp.a = a*object.b - b*object.a;
	temp.b = b * object.b;
	return temp;
}

fraction fraction::operator*(const fraction& object)const
{
	fraction temp;
	temp.a = a*object.b * b*object.a;
	temp.b = b * object.b;
	return temp;
}

fraction fraction::operator/(const fraction& object)const
{
	fraction temp;
	temp.a = a*object.b / b*object.a;
	temp.b = b * object.b;
	return temp;
}

<< moderator edit: added [code][/code] tags >>

but am getting the wrong output. Is my algorithm wrong ? if so how can i correct ?

Recommended Answers

All 10 Replies

i am inputing two fractions and performing arithmetic and relational operations on the fractions (+, -, *, /)

the output is correct for addition and subtraction ex. if i enter 2/3 4/5 i get:
x + y = 22/15
x - y = -2/15
x * y = 120/15
x / y = 12/15

but not correct for multiplication and division.

a & b represent numerator & denominators

>a & b represent numerator & denominators

I meant the type -- int, double, etc.

I think it should be like this.

fraction operator* (const fraction& object) const
   {
      fraction temp;
      temp.a = a * object.a;
      temp.b = b * object.b;
      return temp;
   }
   fraction operator/ (const fraction& object) const
   {
      fraction temp;
      temp.a = a * object.b;
      temp.b = b * object.a;
      return temp;
   }

That did it .. Thank you

I have one more question. how would i check to see if a denominator entered is equal to zero ? would this be an if statement in the constructor such as:

fraction::fraction()
{
if (b = 0)
{
cout << "ERROR";
} }

I have one more question. how would i check to see if a denominator entered is equal to zero ? would this be an if statement in the constructor such as:

fraction::fraction()
{
if (b = 0)
{
cout << "ERROR";
} }

More like,

if (b == 0)

when i enter the statement:

if (b == 0)
{
cout << "error";
}

the program runs and outputs:
errorerrorerrorEnter fraction 1: 5/0
Enter fraction 2: 6/7
Fraction1: 5/0
Fraction2: 6/7
errorx + y = 35/0
errorx - y = 35/0
errorx * y = 30/0
errorx / y = 35/0

is this if statement supposed to be put in the default constructor?

You may want to allow the denominator to be zero, but refuse to calculate a division by it. Or your constructor might throw an exception, but since that's unfamiliar territory I won't advise it (even though that might be the correct thing to do).

[edit]http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.2

here is what i have thusfar:

fraction fraction::operator+(const fraction& object) const
{
	fraction temp;
	temp.a = a*object.b + b*object.a;
	temp.b = b * object.b;
	return temp;
}

fraction fraction::operator-(const fraction& object) const
{
	fraction temp;
	temp.a = a*object.b - b*object.a;
	temp.b = b * object.b;
	return temp;
}

fraction fraction::operator*(const fraction& object) const
{
	fraction temp;
	temp.a = a * object.a;
	temp.b = b * object.b;
	return temp;
}

fraction fraction::operator/(const fraction& object) const
{
	fraction temp;
	temp.a = a * object.b;
	temp.b = b * object.a;
	return temp;
}

ostream& operator<<(ostream& osobject, const fraction& cobject)
{
	osobject << cobject.a << "/" <<cobject.b;
	return osobject;
}

istream& operator>>(istream& isobject, fraction& cobject)
{
char ch;
	isobject >> cobject.a >> ch >> cobject.b;
	return isobject;
}

fraction::fraction() 
{
if (b == 0)
{
cout << "error";
}
}

<< moderator edit: added [code][/code] tags -- learn to use them yourself! >>

>here is what i have thusfar

Is there a question?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.