{"id":60,"date":"2019-11-27T10:57:41","date_gmt":"2019-11-27T10:57:41","guid":{"rendered":"http:\/\/52.66.89.59\/?p=60"},"modified":"2022-03-28T01:34:00","modified_gmt":"2022-03-28T01:34:00","slug":"enumeration-cpp","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/enumeration-cpp\/","title":{"rendered":"Enumeration C++"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645098511993-Article_309.png\" alt=\"\" \/><\/p>\n\n<p>Enumerated type (or enumeration) in C++ is a <a href=\"https:\/\/prepbytes.com\/blog\/c-programming\/different-types-of-inheritance-in-c\/\">user defined data type<\/a> that contains fixed set of constants.<\/p>\n\n\n<p>It can be used for days of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY and SATURDAY) , directions (NORTH, SOUTH, EAST and WEST) etc. The C++ enum constants are static and final implicitly. Enumerations are declared via the&nbsp;<strong>enum<\/strong>&nbsp;keyword.<\/p>\n\n\n<p>Each enumerator is automatically assigned an integer value based on its position in the enumeration list. By default, the first enumerator is assigned the integer value 0, and each subsequent enumerator has a value one greater than the previous enumerator.<\/p>\n\n\n<p><b>Example:<\/b><\/p>\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Declare a new enumeration named Week\nenum Week {\n\n  \/\/ Here are the enumerators\n  \/\/ These define all the possible values this type Week can hold\n  Monday, \/\/assigned 0\n  Tuesday, \/\/assigned 1\n  Wednesday, \/\/assigned 2\n  Thursday, \/\/assigned 3\n  Friday, \/\/assigned 4\n  Saturday, \/\/assigned 5\n  Sunday \/\/assigned 6\n};\n\nint main() {\n\n  Week day;\n  day = Friday;\n  cout &lt;&lt; \"Day: \" &lt;&lt; day + 1 &lt;&lt; endl;\n  return 0;\n}<\/code><\/pre>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<p>Day: 5<\/p>\n\n\n<p>It is possible to explicitly define the value of enumerator. These integer values can be positive or negative and can share the same value as other enumerators. Any non-defined enumerators are given a value one greater than the previous enumerator.<\/p>\n\n\n<p>Example:<\/p>\n\n\n<pre class=\"wp-block-code\"><code>enum Animal {\n  ANIMAL_CAT = -3,\n    ANIMAL_DOG,\n    \/\/ assigned -2 ANIMAL_PIG, \n    \/\/ assigned -1 ANIMAL_HORSE = 5, \n    ANIMAL_CHICKEN\n  \/\/ assigned 6 };<\/code><\/pre>\n\n\n<p>The compiler will&nbsp;<em>not<\/em>&nbsp;implicitly convert an integer to an enumerated value.<\/p>\n\n\n<p>The following will produce a compiler error:Animal animal = 5; <B> [will cause compiler error]<\/B><\/p>\n\n\n<p>Each enumerated type is considered a distinct type. Consequently, trying to assign enumerators from one enum type to another enum type will cause a compile error.Animal animal = Monday; [<B>It will cause compilation error as we try to assign Week enum type to Animal enum type<\/B>]<\/p>\n\n\n<p><strong>Key Points:<\/strong><\/p>\n\n\n<ul class=\"wp-block-list\"><li>enum improves type safety<\/li><li>enum can be easily used in switch<\/li><li>enum can be traversed<\/li><li>enum can have fields, constructors and methods<\/li><\/ul>\n\n<p>This article tried to discuss <strong>Enumeration in C++<\/strong>. Hope this blog helps you the concept and implementation. To practice more and build your foundation you can check out <a href=\"https:\/\/www.prepbytes.com\/prepbytes-courses\">Prepbytes Courses<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Enumerated type (or enumeration) in C++ is a user defined data type that contains fixed set of constants. It can be used for days of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY and SATURDAY) , directions (NORTH, SOUTH, EAST and WEST) etc. The C++ enum constants are static and final implicitly. Enumerations are declared [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[11,13,10],"class_list":["post-60","post","type-post","status-publish","format-standard","hentry","category-cpp-programming","tag-basic","tag-cpp","tag-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Cpp Programming | Enumeration Cpp | Prepbytes<\/title>\n<meta name=\"description\" content=\"Enumerated type (or enumeration) in C++ is a user defined data type that contains fixed set of constants.Enumerations are declared via the enum keyword.enum can be traversed\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/enumeration-cpp\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Cpp Programming | Enumeration Cpp | Prepbytes\" \/>\n<meta property=\"og:description\" content=\"Enumerated type (or enumeration) in C++ is a user defined data type that contains fixed set of constants.Enumerations are declared via the enum keyword.enum can be traversed\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/enumeration-cpp\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-11-27T10:57:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-03-28T01:34:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645098511993-Article_309.png\" \/>\n<meta name=\"author\" content=\"PrepBytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"PrepBytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/enumeration-cpp\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/enumeration-cpp\/\"},\"author\":{\"name\":\"PrepBytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e\"},\"headline\":\"Enumeration C++\",\"datePublished\":\"2019-11-27T10:57:41+00:00\",\"dateModified\":\"2022-03-28T01:34:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/enumeration-cpp\/\"},\"wordCount\":272,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/enumeration-cpp\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645098511993-Article_309.png\",\"keywords\":[\"basic\",\"cpp\",\"tutorials\"],\"articleSection\":[\"C++ Programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/enumeration-cpp\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/enumeration-cpp\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/enumeration-cpp\/\",\"name\":\"Cpp Programming | Enumeration Cpp | Prepbytes\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/enumeration-cpp\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/enumeration-cpp\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645098511993-Article_309.png\",\"datePublished\":\"2019-11-27T10:57:41+00:00\",\"dateModified\":\"2022-03-28T01:34:00+00:00\",\"description\":\"Enumerated type (or enumeration) in C++ is a user defined data type that contains fixed set of constants.Enumerations are declared via the enum keyword.enum can be traversed\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/enumeration-cpp\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/enumeration-cpp\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/enumeration-cpp\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645098511993-Article_309.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645098511993-Article_309.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/enumeration-cpp\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Programming\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/cpp-programming\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enumeration C++\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e\",\"name\":\"PrepBytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g\",\"caption\":\"PrepBytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/prepbytes\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Cpp Programming | Enumeration Cpp | Prepbytes","description":"Enumerated type (or enumeration) in C++ is a user defined data type that contains fixed set of constants.Enumerations are declared via the enum keyword.enum can be traversed","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:\/\/prepbytes.com\/blog\/enumeration-cpp\/","og_locale":"en_US","og_type":"article","og_title":"Cpp Programming | Enumeration Cpp | Prepbytes","og_description":"Enumerated type (or enumeration) in C++ is a user defined data type that contains fixed set of constants.Enumerations are declared via the enum keyword.enum can be traversed","og_url":"https:\/\/prepbytes.com\/blog\/enumeration-cpp\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2019-11-27T10:57:41+00:00","article_modified_time":"2022-03-28T01:34:00+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645098511993-Article_309.png","type":"","width":"","height":""}],"author":"PrepBytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"PrepBytes","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/enumeration-cpp\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/enumeration-cpp\/"},"author":{"name":"PrepBytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e"},"headline":"Enumeration C++","datePublished":"2019-11-27T10:57:41+00:00","dateModified":"2022-03-28T01:34:00+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/enumeration-cpp\/"},"wordCount":272,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/enumeration-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645098511993-Article_309.png","keywords":["basic","cpp","tutorials"],"articleSection":["C++ Programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/enumeration-cpp\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/enumeration-cpp\/","url":"https:\/\/prepbytes.com\/blog\/enumeration-cpp\/","name":"Cpp Programming | Enumeration Cpp | Prepbytes","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/enumeration-cpp\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/enumeration-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645098511993-Article_309.png","datePublished":"2019-11-27T10:57:41+00:00","dateModified":"2022-03-28T01:34:00+00:00","description":"Enumerated type (or enumeration) in C++ is a user defined data type that contains fixed set of constants.Enumerations are declared via the enum keyword.enum can be traversed","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/enumeration-cpp\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/enumeration-cpp\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/enumeration-cpp\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645098511993-Article_309.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645098511993-Article_309.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/enumeration-cpp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"C++ Programming","item":"https:\/\/prepbytes.com\/blog\/category\/cpp-programming\/"},{"@type":"ListItem","position":3,"name":"Enumeration C++"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e","name":"PrepBytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g","caption":"PrepBytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/prepbytes\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/60","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=60"}],"version-history":[{"count":7,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/60\/revisions"}],"predecessor-version":[{"id":8279,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/60\/revisions\/8279"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=60"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=60"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=60"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}