Answers for "c++ enum"

C++
2

enum in c++

#include <iostream>
enum Example :unsigned char // char will work but float will not work because it is not an interger
{
	A = 9, B = 56, c = 12//values must be integers
};//enum is a just a name for a group of numeric values 
int main()
{
	Example example = A;
	//	Example example2 = 5;// This gives error because the values must be A , B ,C no other values are accepted in our code however we can break this behaiour in c++
	if (example == A)//We can use A directly because it is just an unsigned char var but is grouped in Example enum with other values
		std::cout << "Good" << std::endl;
	std::cin.get();
  
}
Posted by: Guest on August-04-2020
0

c++ enum

enum Color { red, green, blue };
Color r = red;
switch(r)
{
    case red  : std::cout << "red\n";   break;
    case green: std::cout << "green\n"; break;
    case blue : std::cout << "blue\n";  break;
}
Posted by: Guest on October-25-2021

Browse Popular Code Answers by Language