-
Notifications
You must be signed in to change notification settings - Fork 1
/
Enum_operator_overload_1.cpp
43 lines (41 loc) · 1.29 KB
/
Enum_operator_overload_1.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
// author: jaydattpatel
//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};
enum class days{SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY};
//unlike plain enums C++11 enum class is typesafe and does not silently convert to int
ostream& operator<<(ostream& out, const days& d)
{
out << static_cast<int>(d);
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};// today = days::SUNDAY
cout << "Demonstrate class enum" << endl;
cout << "SUNDAY : " << today << endl;
cout << "MONDAY : " << ++today << endl;
today++;
cout << "TUESDAY : " << today << endl;
cout << "WEDNESDAY : " << ++today << endl;
today++;
cout << "THURSDAY : " << today << endl;
cout << "FRIDAY : " << ++today << endl;
today++;
cout << "SATURDAY : " << today << endl;
cout << "SUNDAY : " << ++today << endl;
return 0;
}