-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrefixIncermentDecrementOperator.cpp
83 lines (69 loc) · 1.74 KB
/
PrefixIncermentDecrementOperator.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
/**
* \file PrefixIncermentDecrementOperator.cpp
* \brief
*
* \review
*
* Note that we return *this. The overloaded increment and decrement operators return the current
* implicit object so multiple operators can be “chained” together.
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//--------------------------------------------------------------------------------------------------
class Digits
{
public:
Digits(int digit) :
_digit(digit)
{
}
Digits & operator ++ ();
Digits & operator -- ();
friend
std::ostream& operator << (std::ostream &out, const Digits &d);
private:
int _digit {};
};
//--------------------------------------------------------------------------------------------------
Digits &
Digits::operator ++ ()
{
if (_digit == 9)
_digit = 0;
else
++_digit;
return *this;
}
//--------------------------------------------------------------------------------------------------
Digits &
Digits::operator -- ()
{
if (_digit == 0)
_digit = 9;
else
--_digit;
return *this;
}
//--------------------------------------------------------------------------------------------------
std::ostream &
operator << (std::ostream &out, const Digits &d)
{
out << d._digit;
return out;
}
//--------------------------------------------------------------------------------------------------
int main()
{
Digits d1(6);
std::cout << "The Digit is : " << d1 << std::endl;
std::cout << "Prefix Increment : " << (++ d1) << std::endl;
std::cout << "Prefix Decrement : " << (-- d1) << std::endl;
return EXIT_SUCCESS;
}
//--------------------------------------------------------------------------------------------------
#if OUTPUT
The Digit is : 6
Prefix Increment : 7
Prefix Decrement : 6
#endif