Last active
January 1, 2016 22:28
-
-
Save satishgoda/8209935 to your computer and use it in GitHub Desktop.
Enumeration Classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
using namespace std; | |
enum class TrafficLight { green, yellow, red }; | |
TrafficLight& operator++(TrafficLight& light) { | |
switch (light) { | |
case TrafficLight::green: | |
return light = TrafficLight::yellow; | |
case TrafficLight::yellow: | |
return light = TrafficLight::red; | |
case TrafficLight::red: | |
return light = TrafficLight::green; | |
} | |
} | |
ostream& operator<<(ostream& out, const TrafficLight& light) | |
{ | |
static const vector<string> names {"green", "yellow", "red"}; | |
return out << names[static_cast<string::size_type>(light)]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment