***Calling class functions in main***

As you can see, I'm struggling with classes. How do I call my fraction plus(fraction second), fraction minus(fraction second), etc. functions?

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
#include<iostream>
using namespace std;

class fraction
{
private:
void reduce() //I will do this later.
{}           


int num, denom;


public:


fraction()
{
num = 0;
denom = 1;
}

fraction(int n, int d)
{
	num = n;
	if (d = 0)
	{ cout << "Can't have a zero denominator!" << endl;
	}
	else
	{denom = d;
	}
}



fraction plus(fraction second)
{second.num = (num * second.denom) + (second.num *denom);
second.denom = denom * second.denom;
second;
return second;
}

fraction minus(fraction second)
{second.num = (num * second.denom) - (second.num * denom);
second.denom = denom * second.denom;
return second;
}

fraction times(fraction second)
{second.num = num * second.num;
second.denom = denom * second.denom;
return second;
}

fraction divides(fraction second)
{second.num = num * second.denom;
second.denom = denom * second.num;
return second;
}

double toDecimal()
{   double decimal = (double)num/denom;
    cout << decimal;
	return decimal;}

void input()
{char div, op;
fraction f2;
cout << "Please enter a fraction in x/y form: ";
cin >> num >> div >> denom;
cout << endl;
 cout << "Please enter an operation (*, -, *, /): ";
cin >> op;
if (op == '+')
{plus(f2);}             //******I don't think this is right. How do I call  fraction plus(second fraction) here?******
else if (op == '-')
{minus(f2);}
else if (op == '*')
{times(f2);}
else if (op == '/')
{divides(f2);}
else {cout << "That is not a valid operation!" << endl;
exit(1);}
}

void output()
{
	cout << num << "/" << denom;

}

};

int main()
{ int myFraction = 1/10;

 fraction f, sec;

 fraction::fraction(1, 10);
 
 
 f.input();



cout << "1/10 + ";  
f.output();
cout << " = ";
                    //I just want to get this calculation printed out. (1/10 + the fraction the user enters) So I need to call fraction plus(second fraction), right? How do I do this?
cout << ". " << "which is also ";
f.toDecimal(); 
cout << endl;

 return 0;
}
f.plus(sec);
I tried that, and it didn't print out the sum. :\
plus() doesn't print (it is not its responsibility)

Please indent your code.
Last edited on
I got it to print the sum, but the sum is wrong. I think it's because I called the functions incorrectly in this part:
1
2
3
4
5
6
7
8
if (op == '+')
{plus(f2);}             //******I don't think this is right. How do I call  fraction plus(second fraction) here?******
else if (op == '-')
{minus(f2);}
else if (op == '*')
{times(f2);}
else if (op == '/')
{divides(f2);}


And I'm not sure how to indent my code...
Topic archived. No new replies allowed.