overload the following operators
we are going to overload the following operators in the fract class.
math operator add (+) operator
logical comparison greater than (>) operator
both pre and post increament (++) and decrement (--) operators
output stream (<<) operator
Here are some of the example runs:
Enter fraction, operator, fraction
form 3/4 + 3/8 (or 0/1 + 0/1 to exit): 3/4 > 3/8
3/4 is greater than 3/8
Enter fraction, operator, fraction
form 3/4 + 3/8 (or 0/1 + 0/1 to exit): 3/8 > 3/4
3/8 is NOT greater than 3/4
Enter fraction, operator, fraction
form 3/4 + 3/8 (or 0/1 + 0/1 to exit): 3/5 1 1/2 where operator (1) is a pre-increament of operand 1
4/5
frac_oo.cpp, is
int main()
{
frac f1, f2, fans;
char op;
do {
cout << "nEnter fraction, operator, fraction";
cout << "nform 3/4 + 3/8 (or 0/1 + 0/1 to exit): ";
f1.inputfrac();
cin >> op;
f2.inputfrac();
switch(op) {
case '+':
fans = f1 + f2;
fans.outputfrac();
break;
case '-':
fans = f1 - f2;
fans.outputfrac();
break;
case '*':
fans = f1 * f2;
fans.outputfrac();
break;
case '/':
fans = f1 / f2;
fans.outputfrac();
break;
default:
cout << "No such operator";
} //end switch
} while( f1 != frac(0/1) || f2 != frac(0,1) );
cout << endl;
return 0;
}
12 years ago
Purchase the answer to view it
- fraction.zip