Min menu

Pages

VB

enum in C Plus Plus | C++

 type  enum in C++

A type enum  is used to define a list of values ​​or several logically constant set of values.
In the end, this set is considered as a new type of data with several possible values ​​that can only be chosen from.

The type is  enum very useful if you want to build code that is only dedicated to dealing with a specific type of values. And he ran the code, the language translator C++ Runtime will prevent it and give it an error.

In general, if you want to define a set of related values ​​that are impossible to change, the best option is to define these values ​​primarily within enum.

Examples of some immutable information in life that are also immutable in logic are:

  • seasons (Autumn, Winter, Spring, Summer)

  • directione (North - South - East - West)

  • week days (Mon-Tuesday-Wednesday etc..)

  • months of the year (January - February - March etc..)

  • sex ( Male Female )

When dealing with this fixed type of data that we do not want anyone to tamper with, we use the type  enum and this is what we will see later in the examples.

method definition  enum in C++

Definition  enum is very similar to the method of definition  struct , but dealing with the variables that we put in it later is what differs.

When you put variables inside it you can give them values ​​yourself or let the compiler give them default values ​​itself.
By default, the variables you set are given values ​​in order starting from zero, for example, the first variable is its value  0 , the second is its value  1 , and so on.


In the following example, we have defined  enum its name  Days representing the days of the week.
Then we created an object from it and made it equal to one of the constants in  Days and then print its value.

Example

Main.cpp
                    #include <iostream>
	  
		  using namespace std;
	  
		  // we put 7 Days constants named enum here we have defined
		  enum Days
		  {
		  Monday,
		  Tuesday,
		  Wednesday,
		  Thursday,
		  Friday,
		  Saturday,
		  Sunday
		  };
	  
		  int main()
		  {
		  // Just Days and its purpose is to store one of the values ​​in today called Days. Here we created an object from
		  days today;
	  
		  // today in the Days object which is originally on Monday here we have stored the value of the constant
		  today = Monday;
	  
		  // today here we have printed the value of the object
		  cout << "today value = " << today;
	  
		  return 0;
		  }
	

We will get the following result when running.

                    today value = 0
		

In the previous example, you might say why all this torment!
Why don't we define the variable  today directly as follows?!

                  int today = 0;
	  


important information

In the previous example, if you declared  today it as a regular variable, you could put any value in it like  20 or any other value.

The benefit of defining  today an object  Days is that you are forced to use the constants in it to specify a value  today , which makes you guarantee that no random value can be passed other than the values ​​that you specified. For example, in the previous example, you can change the value of the object  today as follows.

                  // Days because it is already there in Monday you can put the value
		today = Monday;
	
		// Days because it is originally in Friday you can put the value
		today = Friday;
  

Important concepts about gender  enum in C-Plus Plus


first concept

Here we put an example showing how to set values ​​for the constants set in  enum and how to take advantage of these values.

If you want to set values ​​for the constants that you intend to put in  enum yourself instead of the default values ​​set by the compiler, you can put integers as values ​​for them.
Note:  You cannot put text values ​​(words or sentences) as values ​​for the constants we put in it.


In the following example, we have defined  enum its name  Months . It contains constants representing the months of the year and the number of days in it.
Then we created an object from it and made it equal to one of the constants (ie the months) in  Months and then print its value.

Example

Main.cpp
                    #include <iostream>
	  
		  using namespace std;
	  
		  // We put 12 constants in it and give them the values ​​of Months called enum. Here we have defined
		  enum Months
		  {
		  Jan = 31,
		  Feb = 28,
		  Mar = 31,
		  Apr = 30,
		  May = 31,
		  Jun = 30,
		  Jul = 31,
		  Aug = 31,
		  Sep = 30,
		  Oct = 31,
		  Nov = 30,
		  Dec = 31
		  };
	  
		  int main()
		  {
		  // Just Months and its purpose is to store one of the values ​​in selectedMonth called Months. Here we created an object from
		  Months selectedMonth;
	  
		  // selectedMonth in the Months object which is basically in May here we have stored the value of the constant
		  selectedMonth = May;
	  
		  // May which is the number of days in the month selectedMonth Here we have printed the value of the constant
		  cout << "May contain " << selectedMonth << " days";
	  
		  return 0;
		  }
	  
	

We will get the following result when running.

                    May contain 31 days
		


second concept

Here we have put an example that teaches you how to check the value of the object we create from  enum using the syntax  switch , which makes writing the code much easier and clearer.

One of the things that distinguishes the type  enum is that the syntax can be used  switch to check the value of the object we are creating from it, which makes writing the code easier and clearer.


In the following example, we have defined  enum its name  Days representing the days of the week.
Then we created an object from it and made it equal to one of the constants (days) in Days.
Finally, we used the sentence  switch to check the value of the object and based on it we print the appropriate sentence.

Example

Main.cpp
                    #include <iostream>
	  
		  using namespace std;
	  
		  // we put 7 Days constants named enum here we have defined
		  enum Days
		  {
		  Monday,
		  Tuesday,
		  Wednesday,
		  Thursday,
		  Friday,
		  Saturday,
		  Sunday
		  };
	  
		  int main()
		  {
		  // Just Days and its purpose is to store one of the values ​​in selectedDay called Days. Here we created an object from
		  Days selectedDay;
	  
		  // selectedDay in the Days object which is basically on Thursday here we have stored the value of the constant
		  selectedDay = Thursday;
	  
		  // selectedDay Here we have checked the object's value
		  switch(selectedDay)
		  {
		  // The next print command will be executed Thursday, Wednesday, Tuesday, or Monday if it is equal to the value of the constant
		  case Monday:
		  case Tuesday:
		  case Wednesday:
		  case Thursday:
		  cout << "We are available from 8:00 AM to 4:00 PM.";
		  break;
	  
		  // The next print command will be executed Friday if it is equal to the value of the constant
		  case Friday:
		  cout << "We are available from 8:00 AM to 12:00 PM.";
		  break;
	  
		  // And here the next print command will be executed Sunday or Saturday, if it is not equal to any previous value, then it is equal to
		  default:
		  cout << "We are not available on vacations.";
		  break;
		  }
	  
		  return 0;
		  }
	

We will get the following result when running.

                    We are available from 8:00 AM to 4:00 PM.
		


The third concept

Here we have an example showing how to define a function that takes a parameter of its type  enum and how to pass a value to it when it is called.

The following example shows how to define a function that takes a parameter of its type  enum and how to pass a value to it when it is called.


In the following example, we have defined  enum its name  Gender representing the genders (male, female).
Then we defined a function whose name  printInstructions() when called, we pass an object to it,  and it Gender checks its value, and on its basis it prints a specific text.
Finally, we create an object from  Gender and pass it to the function printInstructions().

Example

Main.cpp
                    #include <iostream>
	  
		  using namespace std;
	  
		  // we put in it two constants Gender named enum here we define
		  enum Gender
		  {
		  Male,
		  Female
		  };
	  
		  // Gender When it is called we must pass it a value which is an object from the printInstructions Here we have defined a function named
		  void printInstructions(Gender gender)
		  {
		  // The print command set here will be executed Male equal to gender if the value we passed to it in the object
		  if (gender == Male)
		  {
		  cout << "Males should go to right building.";
		  }
		  // The print command set here will be executed Male not equal to gender if the value we passed to it in the object
		  else
		  {
		  cout << "Females should go to the left building.";
		  }
		  }
	  
		  // main() Here we have defined the function
		  int main()
		  {
		  // just Gender and its purpose is to store one of the values ​​in selectedGender called Gender. Here we created an object from
		  Gender selectedGender;
	  
		  // selectedGender in the Gender object which is basically in Male here we have stored the value of the constant
		  selectedGender = Male;
	  
		  // which will check its value and then print a sentence that fits its selectedGender value and pass the printInstructions() object here we called the function
		  printInstructions(selectedGender);
	  
		  return 0;
		  }
	

We will get the following result when running.

                    Males should go to right building.