-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArithmeticOperator.cpp
More file actions
85 lines (68 loc) · 1.63 KB
/
ArithmeticOperator.cpp
File metadata and controls
85 lines (68 loc) · 1.63 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
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
/**
* \file ArithmeticOperator.cpp
* \brief
*
* \review
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//--------------------------------------------------------------------------------------------------
class Cents
{
public:
Cents(int cents) :
_cents(cents)
{
}
int getCents() const
{
return _cents;
}
friend Cents
operator + (const Cents &c1, const Cents &c2)
{
return Cents(c1._cents + c2._cents);
}
friend Cents
operator - (const Cents &c1, const Cents &c2)
{
return Cents(c1._cents - c2._cents);
}
friend Cents
operator + (const Cents &c1, int value)
{
// we can access _cents directly because this is a friend function
return Cents(c1._cents + value);
}
friend
Cents operator + (int value, const Cents &c1)
{
// we can access _cents directly because this is a friend function
return Cents(c1._cents + value);
}
private:
int _cents {};
};
//--------------------------------------------------------------------------------------------------
int main()
{
Cents c1(5);
Cents c2(10);
Cents cent_sum = c1 + c2;
Cents cent_diff = c2 - c1;
std::cout << "I have " << cent_sum.getCents() << " cents" << std::endl;
std::cout << "I have " << cent_diff.getCents() << " cents." << std::endl;
Cents c3 = Cents(4) + 6;
Cents c4 = 6 + Cents(4);
std::cout << "I have " << c3.getCents() << " cents." << std::endl;
std::cout << "I have " << c4.getCents() << " cents." << std::endl;
return 0;
}
//--------------------------------------------------------------------------------------------------
#if OUTPUT
I have 15 cents
I have 5 cents.
I have 10 cents.
I have 10 cents.
#endif