-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathItem25.cpp
More file actions
38 lines (31 loc) · 851 Bytes
/
Item25.cpp
File metadata and controls
38 lines (31 loc) · 851 Bytes
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
//Item 25: Consider support for a non-throwing swap
#include <algorithm>
#include <iostream>
using namespace std;
class Rational
{
public:
Rational (int numerator=1, int denominator=1):iNumerator(numerator),iDenominator(denominator){}
int Numerator() const { return iNumerator; }
int Denominator() const { return iDenominator; }
private:
int iNumerator;
int iDenominator;
};
Rational operator*(const Rational& lhs, const Rational& rhs)
{
Rational result( lhs.Numerator() * rhs.Numerator(), lhs.Denominator() * rhs.Denominator() );
return result;
}
int main()
{
int a = 10, b = 20;
cout << a << " " << b << endl;
std::swap (a,b);
cout << a << " " << b << endl;
Rational aRational(3,2);
Rational bRational(5,8);
std::swap(aRational,bRational);
cout << bRational.Numerator() << endl;
cout << aRational.Numerator() << endl;
}