← Back to C++ Course | Chapter 8: OOP Core | Lesson 14 of 14

C++ के Enums

एक enum named choices की एक fixed list है, हफ्ते के दिनों या traffic light रंगों की तरह। यह आपको list में न होने वाली values इस्तेमाल करने से रोकता है।
Syntax
cpp
enum EnumName { VALUE_ONE, VALUE_TWO, VALUE_THREE };

EnumName variable_name = VALUE_ONE;

Enum क्या है?

एक enum एक type define करता है जिसकी values एक छोटे named set तक restricted हैं, जैसे Color { RED, GREEN, BLUE }, एक fixed set of options model करने वाले code को unclear meaning वाले raw integers इस्तेमाल करने से कहीं ज़्यादा readable बनाते हुए।

अंदर से हर enumerator एक integer की तरह store होता है, default से 0 पर शुरू होकर हर बाद के नाम के लिए increment होते हुए।

उदाहरण: What Is an Enum?

cpp
// Include std::cout and std::cin
#include <iostream>

enum Color { RED, GREEN, BLUE };

// Program execution starts in main()
int main() {
	Color c = GREEN;
	// Print to the console with cout
	std::cout << c << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Plain Enums और उनके Pitfalls

एक plain enum implicitly int में convert होता है और इसके enumerator names आस-पास की scope में leak हो जाते हैं, जिसका मतलब है same scope में दो plain enums RED जैसा नाम बिना naming collision के reuse नहीं कर सकते।

यह looseness आपको accidentally unrelated enums से values compare या assign करने भी देती है, क्योंकि compiler implicit int conversion को नहीं रोकता।

उदाहरण: Plain Enums and Their Pitfalls

cpp
#include <iostream>

enum Color { RED, GREEN, BLUE };

int main() {
	int x = RED; // implicitly converts to int
	std::cout << x << std::endl;
	return 0;
}

Type Safety के लिए Enum Class

enum class (C++11+) अपने enumerators को enum के अपने नाम के अंदर scope करता है (Color::RED) और implicitly int में convert नहीं होता, इसलिए दो अलग enum types मिला देना एक silent bug की बजाय एक compile error बन जाता है।

यही वजह है कि modern C++ style नए code में plain, unscoped enum से enum class कहीं ज़्यादा पसंद करती है।

उदाहरण: Enum Class for Type Safety

cpp
#include <iostream>

enum class Color { RED, GREEN, BLUE };

int main() {
	Color c = Color::RED; // scoped, no implicit int conversion
	std::cout << (c == Color::RED) << std::endl;
	return 0;
}

Underlying Type Set करना

Default से एक enum का underlying integer type implementation-defined है, लेकिन आप इसे explicitly pin कर सकते हैं, जैसे enum class Status : uint8_t, type के storage size को control करने के लिए, जो मायने रखता है जब enum एक specific memory footprint या serialized format के लिए laid out struct का हिस्सा हो।

उदाहरण: Setting the Underlying Type

cpp
#include <iostream>
#include <cstdint>

enum class Status : uint8_t { Active, Inactive }; // explicitly pinned type

int main() {
	Status s = Status::Active;
	std::cout << static_cast<int>(s) << std::endl;
	return 0;
}

Switch Statements में Enums

Enums naturally switch statements के साथ pair होते हैं, क्योंकि values का छोटा closed set programmer और compiler दोनों के लिए हर possible case के बारे में reason करना आसान बनाता है; कई compilers आपको warn भी करेंगे अगर एक enum class पर switch हर enumerator handle न करे।

उदाहरण: Enums in Switch Statements

cpp
// Include std::cout and std::cin
#include <iostream>

enum class Color { RED, GREEN, BLUE };

// Program execution starts in main()
int main() {
	Color c = Color::GREEN;
	// Choose a branch based on the value of c
	switch (c) {
		case Color::RED: std::cout << "Red" << std::endl; break;
		case Color::GREEN: std::cout << "Green" << std::endl; break;
		case Color::BLUE: std::cout << "Blue" << std::endl; break;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक plain enum इस्तेमाल करना जिसके names आस-पास की scope में leak हों और दूसरे names से clash करें।
  2. यह मान लेना कि एक enum class implicitly int में convert होता है, जब आपको static_cast<int>(value) चाहिए।
  3. यह उम्मीद करना कि cout << color enumerator नाम print करेगा, जब यह इसकी integer value print करता है।
चैप्टर सारांश
  • Classes और objects data और methods bundle करते हैं, access specifiers visibility control करते हुए।
  • Constructors, destructors, copy constructor, और this pointer manage करते हैं कि objects कैसे बनते, copy होते, और destroy होते हैं।
  • Static members, friend functions, operator overloading, structures, और enums classes जो कर सकती हैं उसे extend करते हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.