fraction class
Nov 26, 2011 at 12:58am UTC
For some reason, I can not get my bool functions to work. When I take those parts out. The program works.
This is what the output is supposed to be. Any hints or tips are much appreciated. Thanks!!
The result starts off at 0/1
The product of 9/8 and 2/3 is 3/4
2/3 is less than 3/4
The sum of 9/8 and 2/3 is 43/24
The difference of 9/8 and 2/3 is 11/24
The two fractions are not equal.
The quotient of 3/2 and 2/3 is 9/4
Press any key to continue . . .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
#include <iostream>
using namespace std;
class fraction
{
private :
int numerator;
int denominator;
public :
fraction (); //default constractor....check
fraction (int ,int ); //default constractor with parameters ....check
fraction AddedTo (fraction value) const ;
fraction MultipliedBy (fraction value) const ; //binary observer type of operation
fraction Subtract (fraction value) const ;
fraction DividedBy (fraction value) const ;
fraction isGreaterThan ();
fraction isEqualTo ();
fraction print() const ;
};
#include <iostream>
using namespace std;
int main()
{
fraction f1(9,8); //calling a parameterized class constructor
fraction f2(2,3); //calling a parameterized class constructor
fraction result; //calling a default class constructor
fraction f3; //calling a default class constructor
cout << "The result starts off at " ;
result.print(); //calling an observer function
cout << endl;
cout << "The product of " ;
f1.print();
cout << " and " ;
f2.print();
cout << " is " ;
result = f1.MultipliedBy(f2); //a class binary operation - function
result.print();
cout << endl;
f3 = result; //assignment
/*
if (f2.isGreaterThan(f3)){ //a class relational expression - boolean operation/function
f2.print();
cout <<" is greater than ";
f3.print();
cout<<endl;
} else {
f2.print();
cout <<" is less than ";
f3.print();
cout<<endl;
}
*/
cout << "The sum of " ;
f1.print();
cout << " and " ;
f2.print();
cout << " is " ;
result = f1.AddedTo(f2); //a class binary operation - function
result.print();
cout << endl;
cout << "The difference of " ;
f1.print();
cout << " and " ;
f2.print();
cout << " is " ;
result = f1.Subtract(f2); //a class binary operation - function
result.print();
cout << endl;
if (f1.isEqualTo(f2)){ //a class relational expression - boolean operation/function
cout << "The two fractions are equal." << endl;
} else {
cout << "The two fractions are not equal." << endl;
}
const fraction f4(12, 8);
const fraction f5(202, 303);
result = f4.DividedBy(f5); //a class binary operation - function
cout << "The quotient of " ;
f4.print();
cout << " and " ;
f5.print();
cout << " is " ;
result.print();
cout << endl;
system ("PAUSE" );//exclude statement if not using Dev-C++
return 0;
}
fraction::fraction()
{
numerator = 0;
denominator = 1;
}
//**********************************
fraction::fraction(int newNumerator, int newDenominator)
{
numerator=newNumerator;
denominator=newDenominator;
}
//***************************
fraction fraction::AddedTo (fraction value) const
{
fraction result;
result.numerator = (numerator * value.denominator)+(value.numerator*denominator);
result.denominator = denominator * value.denominator;
return result;
}
//********************************
fraction fraction::Subtract (fraction value) const
{
fraction result;
result.numerator = (numerator * value.denominator)-(value.numerator*denominator);
result.denominator = denominator * value.denominator;
return result;
}
//**************************************
fraction fraction::MultipliedBy (fraction value) const
{
fraction result;
result.numerator = numerator * value.numerator;
result.denominator = denominator * value.denominator;
return result;
}
//************************************
fraction fraction::DividedBy (fraction value) const
{
fraction result;
result.numerator = numerator * value.denominator;
result.denominator = denominator * value.numerator;
return result;
}
//**********************************
bool fraction::isGreaterThan (fraction value)
{
int fraction1;
int fraction2;
fraction1=(numerator/denominator);
fraction2=(value.numerator/value.denominator);
fraction1>fraction2;
}
//*************************************
bool fraction::isEqualTo(fraction value)
{
(numerator/denominator)==(value.numerator/value.denominator);
}
//**************************************
fraction fraction::print() const
{
int num=numerator;
int den=denominator;
if (num>den)
{
for (int counter=2;counter<den;counter++)
{
while (num%counter==0 & den%counter==0)
{
num=(num/counter);
den=(den/counter);
}
}
}
else if (den>num)
{
for (int counter=2;counter<num;counter++)
{
while (num%counter==0 & den%counter==0)
{
num=(num/counter);
den=(den/counter);
}
}
}
cout<<num<<"/" <<den;
}
Nov 26, 2011 at 1:07am UTC
You don't have return statements in those functions. You have to return a bool value, just like any other kind of function except void.
Nov 26, 2011 at 1:37am UTC
I get what you are saying. I am just lost with the Boolean functions not working and saying they cant overload.
Nov 26, 2011 at 1:39am UTC
lol never mind I figured it out. Just need to take a break from programming. Thanks for the help!!
Nov 26, 2011 at 2:18am UTC
Heh...yeah, we all get to that point where staring at a problem no longer returns results. Glad you got this figured out.
Topic archived. No new replies allowed.