-
Notifications
You must be signed in to change notification settings - Fork 1
/
Enum_operator_overload_2.cpp
47 lines (42 loc) · 1.2 KB
/
Enum_operator_overload_2.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
//Supplementary example on overloading operator
#include<iostream>
#include<cstdint>
using namespace std;
enum class days:std::int8_t{SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY};
// ostream& operator<<(ostream& out, const days& d)
// {
// out << static_cast<int>(d);
// return out;
// }
ostream& operator<<(ostream& out, const days& d)
{
switch (d)
{
case days::SUNDAY : out <<"Sun";break;
case days::MONDAY : out <<"Mon";break;
case days::TUESDAY : out <<"Tue";break;
case days::WEDNESDAY: out <<"Wed";break;
case days::THURSDAY : out <<"Thus";break;
case days::FRIDAY : out <<"Fri";break;
case days::SATURDAY : out <<"Sat";break;
}
return out;
}
days operator++(days& d) //PREFIX OPERATOR
{
d = static_cast<days>((static_cast<int>(d) + 1) % 7);
return d;
}
days operator++(days& d, int) //POSTFIX OPERATOR
{
days temp = d;
d = static_cast<days>((static_cast<int>(d) + 1) % 7);
return temp;
}
int main()
{
days today{days::SUNDAY} , dd;// today = days::SUNDAY
dd = today++;
cout<<today<<"\t"<<dd<<endl;
return 0;
}