{"id":120,"date":"2026-05-21T04:42:43","date_gmt":"2026-05-21T04:42:43","guid":{"rendered":"https:\/\/gigz.pk\/cpp\/?post_type=lesson&#038;p=120"},"modified":"2026-05-22T10:52:19","modified_gmt":"2026-05-22T10:52:19","slug":"enumerations","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/cpp\/?lesson=enumerations","title":{"rendered":"Enumerations"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Enumerations in C++ are user-defined data types used to assign names to integral constants. They make programs more readable and easier to manage.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is an Enumeration?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">An enumeration (<code>enum<\/code>) is a data type that consists of a set of named integer constants.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Instead of using numbers directly, you can use meaningful names.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Use Enumerations?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Enumerations are useful because they:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Improve code readability<\/li>\n\n\n\n<li>Make programs easier to understand<\/li>\n\n\n\n<li>Reduce use of magic numbers<\/li>\n\n\n\n<li>Help organize related constants<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of Enumeration<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>enum EnumName {<br>    constant1,<br>    constant2,<br>    constant3<br>};<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example of Enumeration<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>enum Day {<br>    Monday,<br>    Tuesday,<br>    Wednesday,<br>    Thursday,<br>    Friday<br>};<br><br>int main() {<br><br>    Day today = Wednesday;<br><br>    cout &lt;&lt; today;<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>2<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How Enumerations Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By default:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First value starts from <code>0<\/code><\/li>\n\n\n\n<li>Next values increase automatically<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Monday = 0<br>Tuesday = 1<br>Wednesday = 2<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Assigning Custom Values<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can assign your own values.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>enum Status {<br>    Success = 1,<br>    Error = 2,<br>    Pending = 3<br>};<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example with Custom Values<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>enum Level {<br>    Low = 10,<br>    Medium = 20,<br>    High = 30<br>};<br><br>int main() {<br><br>    Level l = High;<br><br>    cout &lt;&lt; l;<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>30<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using Enumeration in Conditions<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>enum TrafficLight {<br>    Red,<br>    Yellow,<br>    Green<br>};<br><br>int main() {<br><br>    TrafficLight signal = Green;<br><br>    if (signal == Green) {<br>        cout &lt;&lt; \"Go\";<br>    }<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Enumeration with switch Statement<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>switch(signal) {<br><br>    case Red:<br>        cout &lt;&lt; \"Stop\";<br>        break;<br><br>    case Green:<br>        cout &lt;&lt; \"Go\";<br>        break;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Enum Class (Modern C++)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Modern C++ provides strongly typed enumerations.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>enum class Color {<br>    Red,<br>    Green,<br>    Blue<br>};<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Accessing values:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Color::Red<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Difference Between enum and enum class<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>enum<\/th><th>enum class<\/th><\/tr><\/thead><tbody><tr><td>Less type safety<\/td><td>Strong type safety<\/td><\/tr><tr><td>Values accessible directly<\/td><td>Access using scope operator<\/td><\/tr><tr><td>Older style<\/td><td>Modern C++ style<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of Enumerations<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Makes code readable<\/li>\n\n\n\n<li>Simplifies constant management<\/li>\n\n\n\n<li>Improves program clarity<\/li>\n\n\n\n<li>Reduces errors from numeric values<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Real-Life Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Think of traffic signals:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Red<\/li>\n\n\n\n<li>Yellow<\/li>\n\n\n\n<li>Green<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Using names is much easier than remembering numbers like <code>0<\/code>, <code>1<\/code>, or <code>2<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Enumerations are Important<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Enumerations are important because they:<\/p>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1779447169455\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Improve program organization<\/li>\n\n\n\n<li>Help manage fixed values<\/li>\n\n\n\n<li>Make code self-explanatory<\/li>\n\n\n\n<li>Support better programming practices<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Enumerations in C++ are useful user-defined data types that assign names to integer constants. They improve readability, reduce confusion, and help create clean and maintainable programs.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/cpp\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Intermediate C++ > Structures and Enums > Enumerations<\/span><\/span><\/div>","protected":false},"menu_order":36,"template":"","class_list":["post-120","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Enumerations - Learn C++Language with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn C++ enumerations (enum) with examples to define named constants, improve readability, and manage fixed values in programs.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Enumerations - Learn C++Language with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn C++ enumerations (enum) with examples to define named constants, improve readability, and manage fixed values in programs.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn C++Language with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-22T10:52:19+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?lesson=enumerations\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"Enumerations - Learn C++Language with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\"},\"datePublished\":\"2026-05-21T04:42:43+00:00\",\"dateModified\":\"2026-05-22T10:52:19+00:00\",\"description\":\"Learn C++ enumerations (enum) with examples to define named constants, improve readability, and manage fixed values in programs.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/cpp\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Intermediate C++ > Structures and Enums > Enumerations\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/\",\"name\":\"Learn C++Language with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Enumerations - Learn C++Language with GiGz.PK","description":"Learn C++ enumerations (enum) with examples to define named constants, improve readability, and manage fixed values in programs.","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:\/\/gigz.pk\/","og_locale":"en_US","og_type":"article","og_title":"Enumerations - Learn C++Language with GiGz.PK","og_description":"Learn C++ enumerations (enum) with examples to define named constants, improve readability, and manage fixed values in programs.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Learn C++Language with GiGz.PK","article_modified_time":"2026-05-22T10:52:19+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/cpp\/?lesson=enumerations","url":"https:\/\/gigz.pk\/","name":"Enumerations - Learn C++Language with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/cpp\/#website"},"datePublished":"2026-05-21T04:42:43+00:00","dateModified":"2026-05-22T10:52:19+00:00","description":"Learn C++ enumerations (enum) with examples to define named constants, improve readability, and manage fixed values in programs.","breadcrumb":{"@id":"https:\/\/gigz.pk\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/cpp"},{"@type":"ListItem","position":2,"name":"Intermediate C++ > Structures and Enums > Enumerations"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/cpp\/#website","url":"https:\/\/gigz.pk\/cpp\/","name":"Learn C++Language with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/cpp\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson\/120","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}