编辑代码

#include<iostream>
using namespace std;

//自定义 TrafficLight 类型
enum class TrafficLight{red,green,yellow};		

//实现  自定义 TrafficLight 类型 前置 ++ 运算符重载
TrafficLight& operator ++(TrafficLight& light)
{
	switch (light)
	{
	case TrafficLight::red:
		return light = TrafficLight::green;
		//break;
	case TrafficLight::green:
		return light = TrafficLight::yellow;
		//break;
	case TrafficLight::yellow:
		return light = TrafficLight::red;
		//break;
	}
}

//实现  自定义 TrafficLight 类型 后置 ++ 运算符重载
TrafficLight operator ++(TrafficLight& light, int) 
{
	TrafficLight temp = light;
	++light;
	return temp;
}

//自定义 TrafficLight 类型  输出流 << 运算符重载
ostream& operator <<(ostream& o, const TrafficLight& light)  
{
	switch (light) {
	case TrafficLight::green:
		o << "Green" << endl;
		break;
	case TrafficLight::red:
		o << "Red" << endl;
		break;
	case TrafficLight::yellow:
		o << "Yello" << endl;
		break;
	}
	return o;
}
int main() {
	TrafficLight light = TrafficLight::green;
	int i{ 0 };
	while (i != 5) {
		cout << light++;
		i++;
	}
	cout << light << endl;
	++light;
	cout << light << endl;
	light++;
	cout << light << endl;

	return 0;
}