{"id":84045,"date":"2021-09-14T09:00:32","date_gmt":"2021-09-14T03:30:32","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=84045"},"modified":"2021-09-14T09:00:32","modified_gmt":"2021-09-14T03:30:32","slug":"enumerations-in-cpp","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/enumerations-in-cpp\/","title":{"rendered":"Enumerations in C++ | C++ Enum"},"content":{"rendered":"<p>The C++ programming language offers various useful features and functionalities to the programmers. You will get great advantage while using the C++ programming language. In this tutorial, we are going to teach you about Enumerations in C++ (C++ Enum) in detail.<\/p>\n<h3>What is Enumeration in C++?<\/h3>\n<p>To put it short, enumeration is a user-defined data type and it consists of fixed and constant values. A programmer defines these values during declaring the enumerated type.<\/p>\n<p>For example, you can use it for the day within a week or for directions etc.<br \/>\n<strong>Syntax:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">enum Name_enumerated_type{data1,data2,....,dataN};\n<\/pre>\n<p>enum keyword is used to define an enumeration. After defining enumerated types, variables are created. You can create it in two ways:-<\/p>\n<p>1. First, you can declare it while declaring enumerated types.<\/p>\n<p>2. Second, you can create enumerated type variables like the normal variables.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Name_enumerated_type name_of_variable = value;\n<\/pre>\n<p><strong>Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">enum Week{Mon,Tue,Sun};<\/pre>\n<p>Above, Week is the name of the enumeration. And Mon, Tue, Sun are values of type Week. You can also change the value of an enum element while declaring:-<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">enum Week{\nMon=1,\nTue=2,\nSun=3\n};\n<\/pre>\n<h3>Enumerated Type Declaration in C++<\/h3>\n<p>Before using it, you have to create variables of enum type:-<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">enum bool{True,False};\n\/\/ within function\nenum bool checking;\n<\/pre>\n<p>Above, checking is a variable of type enum bool which is created. You can also write the above one in another way:-<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">enum bool{\nTrue,False\n}checking;\n<\/pre>\n<p><strong>Example:- Simple Enumeration Type in C++<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\n\nenum Week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };\n\nint main()\n{\n  Week day;\n  day = Saturday;\n  cout &lt;&lt; \"Day: \" &lt;&lt; day+1;\n  return 0;\n}\n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">Day: 7<\/div>\n<p><strong>Example:- Changing values of enum in C++<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\n\nenum directions{ north=23, south=21, west=2, east=5};\n\nint main() {\n\n  directions d;\n\n  d = west;\n  cout &lt;&lt; \"West: \" &lt;&lt; d &lt;&lt; endl;\n\n  return 0;\n}\n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">West: 2<\/div>\n<h3>Why to use enums in C++?<\/h3>\n<p>From many possible values, an enum variable only takes one value. Below is an example on why to use enums:-<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\n\nenum treat{\n  pasta=40,\n  pizza=200,\n  spaghetti=65\n} food;\n\nint main()\n{\n  food = pizza;\n  cout &lt;&lt; \"Enum variable's size: \" &lt;&lt; sizeof(food) &lt;&lt; \" bytes!\";   \n  return 0;\n}\n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">Enum variable&#8217;s size: 4 bytes!<\/div>\n<p>Size of an integer is 4 bytes. That\u2019s why the output of the above program is 4 bytes. Using enum, you can easily work with flags. While using enum, you will get efficiency along with flexibility.<\/p>\n<h3>Important points to remember for C++ enum<\/h3>\n<ul>\n<li>You can easily use enum in switch<\/li>\n<li>You can also traverse enums<\/li>\n<li>With the help of enum, you can improve type safety<\/li>\n<li>Enum can have fields, constructors and methods<\/li>\n<\/ul>\n<h4>Using enums for flags<\/h4>\n<p>Let\u2019s take an example:-<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">enum design{\n    Bold = 2,\n    Underline = 4\n}click;\n<\/pre>\n<p>Let\u2019s say, to work with text on windows applications, you are creating a button. And you can set flags Bold and Underline. The integral constants are power of 2. Using bitwise OR(|) operator, you can also add two or more flags at once without overlapping. With that, you can choose two or more flags at once.<\/p>\n<p><strong>For Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\nenum design{\n  Bold = 5,\n  Underline = 2\n};\nint main()\n{\n  int create = Bold | Underline;\n  cout &lt;&lt; create;\n  return 0;\n}\n<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">7<\/div>\n<p>Above, the output is 7. So, you can say that bold and underline are used.<\/p>\n<h3>C++ Enumerator Scope<\/h3>\n<p>The enumerator name must be qualified by the enum type name in scoped enums. In C or C++, the unqualified enums are visible throughout the scope in which the enum is declared.<\/p>\n<p>Below is an example of the basic difference between two kinds of enums:-<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">namespace Scoped_game\n{\n  enum class Cards Diamonds, Hearts, Clubs, Spades };\n\n  void Play(Cards card)\n  {\n    \tif (card == Cards::Spades) \n    \t{ \/*...*\/}\n  }\n}\n\nnamespace NonScoped_game\n{\n  enum Cards{ Diamonds, Hearts, Clubs, Spades };\n\n  void Play(Cards card)\n  {\n    \tif (card == Clubs) \n    \t{ \/*...*\/\n    \t}\n  }\n}\n<\/pre>\n<p>Every enumerator must have a unique name and is always treated as a constant within the scope where enum is defined or within the enum itself. There is no need to give unique names to the values.<\/p>\n<h3>Enums with no enumerators<\/h3>\n<p>You can define an enum with an explicit underlying type and no enumerators. With that, you can introduce a new integral type that has no implicit conversion to any other data type.<br \/>\nYou can also avoid the potential for subtle errors caused by implicit conversions.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">enum class byte : unsigned char { };<\/pre>\n<p>Below is an example on how to initialize enums with no enumerators:-<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">enum class byte : unsigned char { };\n\nenum class T : int { };\nT t1{ 0 };\nT t2 = T{ 0 };\n\nstruct A\n{\n  T t{ 0 };\n  A() : t{ 0 } { }\n};\n\nT* ptr = new T{ 0 };\n\nvoid fun(T t) {};\n\nint main()\n{\n  fun(T{ 0 });\n  byte i{ 42 };\n  byte j = byte{ 42 };\n\n  return 0;\n}\n<\/pre>\n<h3>Unscoped Enumeration<\/h3>\n<p>With scoped enumerators, you cannot implicitly convert to their underlying type. You cannot use a scoped enumeration when you are thinking to implicitly convert the enum values to their underlying type.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">enum Bob{ a, c = 10, f, g = f + c };\n\/\/a = 0, c = 10, f = 2, g = 12\n<\/pre>\n<p>The values of the unscoped enumeration type are implicitly convertible to integral types. You can convert the values to their underlying type if the underlying type is fixed.<\/p>\n<p>With the help of unscoped enums, you can replace <strong>#define Bob 43<\/strong> style macros. Instead of a token, here is a token that has the same meaning. Scoped enumeration prevents accidental conversion to the underlying type.<\/p>\n<h3>Summary<\/h3>\n<p>In this tutorial, we discussed enumeration in C++, declaration of enumerated types and why you should use enums in C++. We discussed how you can use enums for flags.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The C++ programming language offers various useful features and functionalities to the programmers. You will get great advantage while using the C++ programming language. In this tutorial, we are going to teach you about&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":84640,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3405],"tags":[4210,4211,4212],"class_list":["post-84045","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cpp","tag-c-enum","tag-c-enum-example","tag-enumeration-in-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Enumerations in C++ | C++ Enum - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn enumeration in C++, declaration of enumerated types and why you should use enums in C++ with example. See how to use enums for flags.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/enumerations-in-cpp\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Enumerations in C++ | C++ Enum - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn enumeration in C++, declaration of enumerated types and why you should use enums in C++ with example. See how to use enums for flags.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/enumerations-in-cpp\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-09-14T03:30:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/Enumerations-C.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Enumerations in C++ | C++ Enum - TechVidvan","description":"Learn enumeration in C++, declaration of enumerated types and why you should use enums in C++ with example. See how to use enums for flags.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/enumerations-in-cpp\/","og_locale":"en_US","og_type":"article","og_title":"Enumerations in C++ | C++ Enum - TechVidvan","og_description":"Learn enumeration in C++, declaration of enumerated types and why you should use enums in C++ with example. See how to use enums for flags.","og_url":"https:\/\/techvidvan.com\/tutorials\/enumerations-in-cpp\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-09-14T03:30:32+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/Enumerations-C.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/enumerations-in-cpp\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/enumerations-in-cpp\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Enumerations in C++ | C++ Enum","datePublished":"2021-09-14T03:30:32+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/enumerations-in-cpp\/"},"wordCount":697,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/enumerations-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/Enumerations-C.jpg","keywords":["C++ Enum","C++ Enum Example","Enumeration in C++"],"articleSection":["C++ Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/enumerations-in-cpp\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/enumerations-in-cpp\/","url":"https:\/\/techvidvan.com\/tutorials\/enumerations-in-cpp\/","name":"Enumerations in C++ | C++ Enum - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/enumerations-in-cpp\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/enumerations-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/Enumerations-C.jpg","datePublished":"2021-09-14T03:30:32+00:00","description":"Learn enumeration in C++, declaration of enumerated types and why you should use enums in C++ with example. See how to use enums for flags.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/enumerations-in-cpp\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/enumerations-in-cpp\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/enumerations-in-cpp\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/Enumerations-C.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/08\/Enumerations-C.jpg","width":1200,"height":628,"caption":"Enumerations C++"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/enumerations-in-cpp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Enumerations in C++ | C++ Enum"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/84045","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=84045"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/84045\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/84640"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=84045"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=84045"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=84045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}