Direction example using Enumeration in C++


The code defines an enumeration type named direction, which has four constants: East, West, North, and South. The main() function creates an instance of the direction enumeration named dir and initializes it to the constant South. The cout statement then attempts to print the value of dir on the console.

However, this code will not produce the expected output. When we attempt to print an enumeration value using cout, the integer value of the constant is printed, not the name of the constant. To print the name of the constant instead of its integer value, we need to use a switch statement to map the enumeration value to its corresponding name.

Source Code

#include <iostream>
using namespace std;
enum direction {East, West, North, South};
int main()
{
  direction dir;
  dir = South;
  cout<<dir;
  return 0;
}
To download raw file Click Here

Output

3

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples