Concepts and use of enum

एक enum integer constants के एक fixed set को पढ़ने लायक़ नाम देता है, तो code mystery number 1 के बजाय MEAL कहता है।

8 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Category 1 का मतलब क्या है?

Till लिखने के छह महीने बाद, आप अपना ही code पढ़ते हैं:

if (item.category == 1) { applyMealDiscount(); }

क्या 1 एक meal है? एक drink? पता लगाने के लिए आप बीस मिनट scroll करते हैं। इसी दौरान एक teammate category = 7 store करता है, एक category जो exist ही नहीं करती, और compiler ख़ुशी-ख़ुशी सहमत हो जाता है।

Magic numbers programs को सड़ाते हैं। C++ के पास बिल्कुल इसी के लिए बना एक छोटा tool है: numbers को नाम दीजिए, और set को एक type दीजिए।

Theory

Menu board के fixed sections

Canteen के menu board में exactly तीन sections हैं: Snacks, Meals, Drinks। कोई dish कभी किसी section के बाहर pin नहीं होती, और कोई भी अपनी मर्ज़ी से चौथा section नहीं बनाता।

एक enum आपके program के अंदर वही board है: named options की एक बंद list। कोई भी item इनमें से एक में ही बैठता है, और सब section numbers नहीं, section names पढ़ते हैं।

Theory

Enumeration, formally

एक enum (enumeration) एक user-defined type है जिसकी values named integer constants हैं:

enum Category { SNACK, MEAL, DRINK };

Default से नाम 0 से numbered होते हैं: SNACK 0 है, MEAL 1 है, DRINK 2 है।

Type का एक variable declare और इस्तेमाल कीजिए:

Category c = MEAL;

अब c meaningfully सिर्फ़ list के members ही hold कर सकता है, और if (c == MEAL) English जैसा पढ़ा जाता है।

Practical

Till की categories, आख़िरकार named

#include <iostream>
using namespace std;

enum Category { SNACK, MEAL, DRINK };        // 0, 1, 2
enum Tier { REGULAR = 1, MEMBER = 5, STAFF = 10 };  // custom values

int main() {
    Category c = MEAL;

    if (c == MEAL)
        cout << "Thali section" << endl;

    cout << c << endl;        // prints 1, the underlying integer
    cout << STAFF << endl;    // prints 10

    switch (c) {              // enums shine in switch
        case SNACK: cout << "snack"; break;
        case MEAL:  cout << "meal";  break;
        case DRINK: cout << "drink"; break;
    }
    return 0;
}

Theory

Custom values और continuation rule

आप values ख़ुद assign कर सकते हैं: enum Tier { REGULAR = 1, MEMBER = 5, STAFF = 10 };

अगर आप कुछ assign करते हैं और बाक़ी skip करते हैं, एक unassigned नाम पिछली value + 1 लेता है:

enum Code { A = 100, B, C = 200, D };

यह B = 101 और D = 201 देता है।

Custom values की परवाह क्यों करें? बाहरी facts से match करने के लिए: error codes, discount percentages, keyboard codes, कोई भी जगह जहाँ number ख़ुद meaning ढोता है।

Think first

Output का अंदाज़ा लगाइए

enum Category { SNACK, MEAL, DRINK }; के साथ, till यह run करता है:

Category c = DRINK;

cout << c;

tap करने से पहले: screen पर exactly क्या दिखता है?

Show the answer

2, word DRINK नहीं। एक enum name एक integer के ऊपर एक costume है: cout costume उतारकर number print करता है (SNACK 0, MEAL 1, DRINK 2)। अगर screen पर word चाहिए तो आपको इसे ख़ुद translate करना पड़ेगा, आमतौर पर एक switch या strings के array से। नाम print होने की उम्मीद करना सबसे common enum surprise है।

Quiz

enum Code { A = 100, B, C = 200, D }; दिया गया, B और D की values क्या हैं?

  1. B = 101, D = 201
  2. B = 0, D = 1
  3. B = 100, D = 200
  4. B = 200, D = 300
Show the answer

B = 101, D = 201

Continuation rule: एक unassigned नाम पिछली value + 1 है। B, A(100) के बाद आता है तो B 101 है; D, C(200) के बाद आता है तो D 201 है। Option B default-from-zero rule apply करता है, पर वह तभी लागू होता है जब कोई values assign न हों। Options C और D copying और jumping rules invent करते हैं जो enums के पास हैं ही नहीं।

Watch out

जानने लायक़ looseness

Classic enums चुपचाप int में convert हो जाते हैं, तो MEAL + DRINK जैसी बकवास compile होती है (यह बस 1 + 2 है)। Type पढ़ने को protect करता है, arithmetic को नहीं।

Modern C++ (C++11) ने enum class जोड़ा: enum class Category { SNACK, MEAL }; को Category::MEAL चाहिए और silent conversion refuse करता है। अगर कोई exam पूछे enums को type-safe कैसे बनाएँ, यही जवाब है। इस syllabus के लिए, पहले classic enum master कीजिए।

Theory

आपके class designs में यह कहाँ fit होता है

Enums class और struct के साथ user-defined family का तीसरा member हैं। पिछले lesson की Item class में, member variable के रूप में Category category; एक int से साफ़ है और ख़ुद को document करता है। आप यही idea हर जगह मिलेंगे: HTTP status groups, weekdays, एक game की states, semester numbers।

Summary

Key takeaways

  • enum Name { A, B, C }; named integer constants का एक user-defined type बनाता है।
  • Default numbering 0 से शुरू होती है; custom values allowed; unassigned names पिछली value + 1 लेते हैं।
  • cout underlying integer print करता है, नाम कभी नहीं।
  • Classic enums चुपचाप int में convert होते हैं; enum class (C++11) strict version है।
  • बंद sets के लिए enums इस्तेमाल कीजिए: categories, tiers, states; वे switch में अच्छे पढ़े जाते हैं।
  • Memory hook: menu board के fixed sections।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Data Encapsulation and inheritance

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Concepts and use of enum · Object Oriented Programming and Data Structures (OOPs & D.S.) · Gri-Learn