-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathItem24.cpp
More file actions
52 lines (38 loc) · 1.24 KB
/
Item24.cpp
File metadata and controls
52 lines (38 loc) · 1.24 KB
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
// Declare non-member functions when type conversions should apply to all parameters
class Rational
{
public:
Rational (int numerator = 0, int denominator = 1):iNumerator(numerator),iDenominator(denominator){}
int Numerator() const { return iNumerator; }
int Denominator() const { return iDenominator; }
// Rational operator*(const Rational& rhs);
private:
int iNumerator;
int iDenominator;
};
/*
Rational Rational::operator*(const Rational& rhs)
{
return Rational( this->Numerator() * rhs.Numerator(), this->Denominator() * rhs.Denominator() );
}
*/
Rational operator*(const Rational& lhs, const Rational& rhs)
{
return Rational( lhs.Numerator() * rhs.Numerator(), lhs.Denominator() * rhs.Denominator() );
}
int main()
{
Rational oneFourth(1,4);
Rational oneTenth(1,10);
Rational oneFourteenth = oneFourth * oneTenth;
Rational half = oneFourth * 2;
Rational one = 4 * oneFourth;
}
/* Notes:
The this pointer is not subjected to implicit type conversions
If type conversion is needed on all parameters including the this pointer
declare the function to be a non-member
This advice holds good for Object-Oriented C++. In case of Template C++
where Rational is a Template class new issues arise and this is
discussed in Item 46
*/