-
Notifications
You must be signed in to change notification settings - Fork 0
/
76.cpp
227 lines (164 loc) · 3.76 KB
/
76.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
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
193
194
195
196
197
198
199
200
201
202
#include <iostream>
using namespace std;
class FractionType
{
private:
int iNum, iDen;
//int *ptr;
public:
FractionType(){}
FractionType(int,int);
void fnSetFraction(int, int);
void fnShowFraction();
//binary operators
FractionType operator+(FractionType);//f1+f2 --> f1.operator+(f2)
FractionType operator+(int); //f1.operator(int)
FractionType operator-(FractionType);
FractionType operator*(FractionType);
bool operator==(FractionType);
FractionType operator=(FractionType);
//unary operator
FractionType operator-(void); // -f2 --> f2.operator-()
FractionType operator++(void); // ++f3 --> f3.operator++()
FractionType operator--(void);
FractionType operator++(int); // postfix version
};
//unary version
FractionType FractionType :: operator-(void)
{
FractionType temp;
temp.iNum = -iNum;
temp.iDen = iDen;
return temp;
}
// p = q p.operator=(q)
FractionType FractionType :: operator=(FractionType f2)
{
iNum = f2.iNum;
iDen = f2.iDen;
return *this;
}
// p = q = r; p = q.operator=(r)
//FractionType FractionType :: operator++(void)
//{
// FractionType temp;
// temp.iNum = iNum + iDen;
// temp.iDen = iDen;
// return temp;
//}
//prefix version
FractionType FractionType :: operator++(void)
{
cout << "\nPrefix version" << endl;
iNum = iNum + iDen;
return *this;
}
//postfix version
FractionType FractionType :: operator++(int dummy)
{
cout << "\nPostfix version" << endl;
FractionType old(*this);
iNum = iNum + iDen;
return old;
}
FractionType FractionType :: operator--(void)
{
iNum = iNum - iDen;
return *this;
}
FractionType FractionType :: operator+(int val) //f3+5 f3.operator+(5)
{
FractionType f3;
f3.iNum = iNum + iDen * val;
f3.iDen = iDen;
return f3;
}
bool FractionType :: operator==(FractionType f2)
{
if(iNum * f2.iDen == iDen * f2.iNum)
return true;
else
return false;
}
FractionType FractionType :: operator*(FractionType f2)
{
FractionType f3;
f3.iNum = iNum * f2.iNum;
f3.iDen = iDen * f2.iDen;
return f3;
}
FractionType FractionType :: operator-(FractionType f2)
{
FractionType f3;
f3.iNum = iNum * f2.iDen - iDen * f2.iNum;
f3.iDen = iDen * f2.iDen;
return f3;
}
FractionType FractionType :: operator+(FractionType f2)
{
FractionType f3;
f3.iNum = iNum * f2.iDen + iDen * f2.iNum;
f3.iDen = iDen * f2.iDen;
return f3;
}
FractionType :: FractionType(int iVal1, int iVal2)
{
// cout << "\nTwo parameter constructor\n";
iNum = iVal1;
iDen = iVal2;
}
void FractionType :: fnSetFraction(int iN, int iD)
{
iNum = iN;
iDen = iD;
}
void FractionType :: fnShowFraction()
{
cout << "Fraction : " << "( " << iNum << " / " << iDen << " )" << endl;
}
int main()
{
int a = 2, b = 7, c;
FractionType p(3,4), q(1,3);
FractionType r;
++a;
a++;
// f3 = f1.fnAddFraction(f2);
// r = p + q; //p.operator+(q)
//
// p.fnShowFraction();
// r = q + 2;
// q.fnShowFraction();
// r.fnShowFraction();
// r = 5 + q; //doesnt work
// if(p==q) //p.operator==(q)
// cout << "Fractions are equal" << endl;
// else
// cout << "Fractions are not equal" << endl;
// r = -p; // p.operator-()
// p.fnShowFraction();
// r.fnShowFraction();
// q.fnShowFraction();
// //r = ++q
// r = --q; // q.operator++()
// q.fnShowFraction();
// r.fnShowFraction();
cout << "****CASE 1****\n";
cout << "p : ";
p.fnShowFraction();
q = ++p;
cout << "q : ";
q.fnShowFraction();
cout << "p : ";
p.fnShowFraction();
cout << "****CASE 2****\n";
p.fnSetFraction(3,4);
cout << "p : ";
p.fnShowFraction();
q = p++;
cout << "q : ";
q.fnShowFraction();
cout << "p : ";
p.fnShowFraction();
return 0;
}