Building Basic Classes

I need to write a code that perfoms all the main operations (+ - * /) on inputed fractions. I need to do this using a class. I can't get it to run. This is the code I have so far.

main.cpp
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
#include <iostream>
#include "fraction.h"
using namespace std;

int main ()
{
	fraction f1, f2, fresult;
	f1.inputFrac(); //input the first fraction
	f2.inputFrac(); //input the second fraction
	cout << "The result of a * b is: ";
	fresult = f1.fracMult(f2); // calculate a * b
	fresult.printFrac(); // print out the result

	cout << "The result of a / b is: ";
	fresult = f1.fracDiv(f2);   // calculate a / b
	fresult.printFrac();
	cout << endl;

	cout << "The result of a + b is: ";
	fresult = f1.fracAdd(f2);   // calculate a + b
	fresult.printFrac();
	cout << endl;

	cout << "The result of a - b is: ";
	fresult = f1.fracSub(f2);   // calculate a - b
	fresult.printFrac();
	cout << endl;
	
	return 0;
}


fraction.cpp
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
#include <iostream>
#include "fraction.h"
using namespace std;

fraction fraction:: fracMult(fraction b)
{
	fraction result;
	result.numerator = numerator * b.numerator;
	result.denom = denom * b.denom;
	result.positive = (positive && b.positive) || (!positive && !b.positive);
	return(result);
}
fraction fraction:: fracDiv(fraction b)
{
	fraction result;
	result.numerator = numerator * b.denom;
	result.denom = denom * b.numerator;
	result.positive = (positive && b.positive) || (!positive && !b.positive);
	return(result);
}
fraction fraction:: fracAdd(fraction b)
{
	fraction result;
	if(!positive)
		numerator = -numerator;
	if(!positive)
		b.numerator = -b.numerator;

	result.numerator = numerator * b.denom + b.numerator * denom;
	result.denom = denom * b.denom;

	if(result.numerator < 0)
	{
		result.numerator = -result.numerator;
		result.positive = false;
	}
	else
		result.positive = true;
	return(result);
}
fraction fraction:: fracSub(fraction b)
{
	fraction result;
	if(!positive)
		numerator = -numerator;
	if(!b.positive)
		b.numerator = -b.numerator;

	result.numerator = numerator * b.denom - b.numerator * denom;
	result.denom = denom * b.denom;

	if(result.numerator < 0)
	{
		result.numerator = -result.numerator;
		result.positive = false;
	}
	else
		result.positive = true;
	return(result);
}
void fraction::printFrac()
{
if (positive == false)
{
cout << "-";
}
cout << numerator << " / " << denom;
}
fraction inputFrac()
{
	fraction f1;
	char sign;

	cout << "Imput the numerator: ";
	cin >> f1.numerator;

	cout << "Imput the denomenator: ";
	cin >> f1.denom;

	cout << "Is the fraction positive?  (Y or N): ";
	cin >> sign;
	
	if (sign=='N' || sign=='n')
	{
		f1.positive = false;
	}
	else
	{
		f1.positive = true;
	}
	return(f1);
}


fraction.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef FRACTION_H_
#define FRACTION_H_

class fraction
{
private:
int numerator;
int denom;
bool positive;
public:
void inputFrac();
void printFrac();
fraction fracMult(fraction b);
fraction fracDiv(fraction b);
fraction fracAdd(fraction b);
fraction fracSub(fraction b);
};

#endif /*FRACTION_H_*/ 


Any suggestions?

My error messages:

'bool fraction::positive' is private (fraction.h--line 9)
'int fraction::denom' is private (fraction.h--line 8)
'int fraction::numerator' is private (fraction.h--line 7)
within this context (fraction.cpp-- line 82, 85, 92, 96)
The error message says quite clearly. You're trying to access private members from a non-method to the class. If you want to access those members, write an interface or make them public.
Topic archived. No new replies allowed.