fraction class assignment, help please!

posted this in beginner, got no answer, could really use some help, having trouble understanding all the parts of creating a class.

I'm supposed to write a class Fraction 2 constructors: Fraction (); and Fraction (int numerator, int denominator); The second constructor will reduce the fraction if needed. Section 4.2 has an algorithm for finding the greatest common divisor of two numbers. Use it to write a function GCD; if the denominator is negative, the constructor will make it positive and make the numerator negative. the denominator of a fraction should not be 0. If the user passes a 0 denominator, the proper thing to do would be throw an exception, but we have not studied exception handling yet. So for now, we will assume a well-behaved use who will never pass a denominator of 0 to the constructor this is what i have so far all on main, have not separated into header and function.cpp yet



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

#include <iostream>
#include <string>

using namespace std;

class Fraction{
public:
	Fraction();
	Fraction(int numerator, int denominator);
	string fractionToString();
	int GCD(int a, int b);


private:
	int numerator;
	int denominator;
	int a;
	int b;
	void reduce();
};

int main(int argc, const char * argv[])
{
	return 0;
}

Fraction::Fraction()
{
	numerator = 0;
	denominator = 1;
}

Fraction::Fraction(int a, int b)
{
	numerator = a;
	denominator = b;
}

void Fraction::reduce()
{
	int a, b, r, gcd;
	a = (numerator);
	b = (denominator);
	if (a == 0)
	{
		numerator = 0;
		denominator = 1;
		return;
	}
	while (a != 0)
	if (a<b)
	{
		r = a; a = b; b = r;
		a = a - b;
	}
	int gcd = b;
	numerator = numerator / gcd;
	denominator = denominator / gcd;

}

int Fraction::GCD(int a, int b)
{
	while (a != b){
		if (b > a){
			b = b - a;
		}
		else { a = a - b; }
	}
	return a, b;
}






string Fraction::fractionToString()
{
	return "numerator"; to_string(numerator); "/"; "denomenator"; to_string(denominator);
}

Last edited on
posted this in beginner, got no answer, could really use some help


Well you only waited 2 hours...please wait longer before cross-posting.
http://www.cplusplus.com/forum/beginner/127052/
my bad, won't happen again
Topic archived. No new replies allowed.