C++ Program Class Function

I have homework that I've been procrastinating. Here's the problem :

"Create a class for working with fractions. Only 3 private data members are needed: the int whole number of the fraction, the int numerator of the fraction, and the positive int denominator of the fraction. For example, the fraction 3 5/8 will have the three private data member values of 3, 5, and 8. The following methods should be in your class:

a.A default constructor that should use default arguments in case no initializers are included in the main.The fraction needs to be stored in reduced form.Make sure the denominator is not set to 0 or a negative value.
b.Add two fractions and store the sum in reduced form.
c.Subtract two fractions and store the difference in reduced form.
d.Multiply two fractions and store the product in reduced form.
e.Divide two fractions and store the quotient in reduced form.
f.Print a fraction.
g.Change a fraction to its reciprocal. For example, the reciprocal of the fraction 3 1/2 should be 7 / 2. The reciprocal of 4/19 is 4 3/4.
Your main should instantiate two fractions and call each of the class methods.The two fractions should be printed a long with the sum, difference, product, quotient, and after being changed to its reciprocal. (Please do not overload the operators for this program)."
He wants the main to look like this:
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
int main()
{
Fraction f1(4, 8, 6), f2(2, 1, 2), sum, diff, prod, quo;
cout << "The fractions are"; 
f1.printData();
cout << " ";
f2.printData();
cout << endl << endl;
sum.add(f1, f2);
diff.sub(f1, f2);
prod.mult(f1, f2);
quo.div(f1, f2);
cout << "The sum is: ";
sum.printData();
cout << endl << endl;
cout << "The difference is: ";
diff.printData();
cout << endl << endl;
cout << "The product is ";
prod.printData();
cout << endl << endl;
cout << "The quotient is ";
quo.printData();
cout << endl << endl;
f1.reciprocal();
f2.reciprocal();
cout << "The reciprocals are: ";
f1.printData();
cout << " ";
f2.printData();
cout << endl << endl;
return 0;
}

All i have is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

class Fraction
{
private:
int whole, num, den;
public:
Fraction();


}

Fraction::Fraction(int a, int b, int c)
{
whole = a;
num = b; 
den = c; 
}

I would really appreciate the help!!!!
Last edited on
You haven't done anything at all on your own. What sort of help are you looking for, short of doing it all for you??
You should ask your teacher what a negative numerator would do. What is -4 -2/3 vs. -4 2/3 vs. 4 -2/3? Maybe suggest that all values should be unsigned and an additional boolean flag indicating positive/negative.

Hey, if you procrastinate and ask others to do your work for you, this is the kind of help you get.

By the way, I like this homework problem. It should be relatively straightforward to solve and will be a good exercise for a newby.
Topic archived. No new replies allowed.