logo

Výčet tříd v C++ a jejich výhoda oproti Enum DataType

Výčty nebo Výčtový typ (výčet) je uživatelsky definovaný datový typ, kterému lze přiřadit určité omezené hodnoty. Tyto hodnoty jsou definovány programátorem v době deklarace výčtového typu.

Potřeba třídy Enum oproti typu Enum:
Níže jsou uvedeny některé z důvodů, jaká jsou omezení typu Enum a proč k jejich pokrytí potřebujeme třídu Enum.



1.Enum je kolekce pojmenovaných celočíselných konstant, což znamená, že každému prvku je přiřazena celočíselná hodnota. 2.Je deklarován klíčovým slovem enum.

C++








#include> using> namespace> std;> enum> roll_no {> >satya = 70,> >aakanskah = 73,> >sanket = 31,> >aniket = 05,> >avinash = 68,> >shreya = 47,> >nikita = 69,> };> int> main()> {> >enum> roll_no obj;> >obj = avinash;> >cout <<>'The roll no of avinash='> << obj;> }>

>

>

nejhezčí úsměv na světě
Výstup

The roll no of avinash=68>

Dva výčty nemohou sdílet stejná jména:

CPP




#include> using> namespace> std;> int> main()> {> >// Defining enum1 Gender> >enum> Gender { Male,> >Female };> >// Defining enum2 Gender2 with same values> >// This will throw error> >enum> Gender2 { Male,> >Female };> >// Creating Gender type variable> >Gender gender = Male;> >Gender2 gender2 = Female;> >cout << gender << endl> ><< gender2;> >return> 0;> }>

>

>

Chyba kompilace:

prog.cpp:13:20: error: redeclaration of 'Male' enum Gender2 { Male, ^ prog.cpp:8:19: note: previous declaration 'main()::Gender Male' enum Gender { Male, ^ prog.cpp:14:20: error: redeclaration of 'Female' Female }; ^ prog.cpp:9:19: note: previous declaration 'main()::Gender Female' Female }; ^ prog.cpp:18:23: error: cannot convert 'main()::Gender' to 'main()::Gender2' in initialization Gender2 gender2 = Female; ^>

Žádná proměnná nemůže mít název, který je již v nějakém výčtu:

CPP


procházení řádu



#include> using> namespace> std;> int> main()> {> >// Defining enum1 Gender> >enum> Gender { Male,> >Female };> >// Creating Gender type variable> >Gender gender = Male;> >// creating a variable Male> >// this will throw error> >int> Male = 10;> >cout << gender << endl;> >return> 0;> }>

>

java pseudokód
>

Chyba kompilace:

prog.cpp: In function 'int main()': prog.cpp:16:9: error: 'int Male' redeclared as different kind of symbol int Male = 10; ^ prog.cpp:8:19: note: previous declaration 'main()::Gender Male' enum Gender { Male, ^>

Výčty nejsou typově bezpečné :

CPP




#include> using> namespace> std;> int> main()> {> >// Defining enum1 Gender> >enum> Gender { Male,> >Female };> >// Defining enum2 Color> >enum> Color { Red,> >Green };> >// Creating Gender type variable> >Gender gender = Male;> >Color color = Red;> >// Upon comparing gender and color> >// it will return true as both have value 0> >// which should not be the case actually> >if> (gender == color)> >cout <<>'Equal'>;> >return> 0;> }>

>

algoritmus řazení vložení
>

Varování:

prog.cpp: In function 'int main()': prog.cpp:23:19: warning: comparison between 'enum main()::Gender' and 'enum main()::Color' [-Wenum-compare] if (gender == color) ^>

Třída Enum

C++11 zavedlo třídy výčtu (také nazývané rozsahové výčty ), což dělá výčty obojí silně typizované a silně rozsahové . Výčet třídy neumožňuje implicitní převod na int a také neporovnává enumerátory z různých výčtů.
K definování třídy enum používáme klíčové slovo class za klíčovým slovem enum.
Syntax:

// Declaration enum class EnumName{ Value1, Value2, ... ValueN}; // Initialisation EnumName ObjectName = EnumName::Value;>

Příklad:

// Declaration enum class Color{ Red, Green, Blue}; // Initialisation Color col = Color::Red;>

Níže je implementace pro zobrazení třídy Enum

CPP




// C++ program to demonstrate working> // of Enum Classes> #include> using> namespace> std;> int> main()> {> >enum> class> Color { Red,> >Green,> >Blue };> >enum> class> Color2 { Red,> >Black,> >White };> >enum> class> People { Good,> >Bad };> >// An enum value can now be used> >// to create variables> >int> Green = 10;> >// Instantiating the Enum Class> >Color x = Color::Green;> >// Comparison now is completely type-safe> >if> (x == Color::Red)> >cout <<>'It's Red '>;> >else> >cout <<>'It's not Red '>;> >People p = People::Good;> >if> (p == People::Bad)> >cout <<>'Bad people '>;> >else> >cout <<>'Good people '>;> >// gives an error> >// if(x == p)> >// cout<<'red is equal to good';> >// won't work as there is no> >// implicit conversion to int> >// cout<< x;> >cout <<>int>(x);> >return> 0;> }>

>

>

Výstup

It's not Red Good people 1>

Výčtové typy deklarované třídou enum mají také větší kontrolu nad svým základním typem; může to být jakýkoli integrální datový typ, jako je char, short nebo unsigned int, který v podstatě slouží k určení velikosti typu.

To je určeno dvojtečkou a základním typem za výčtovým typem:

eg: enum class eyecolor : char {char,green,blue}; Here eyecolor is a distinct type with the same size as a char (1 byte).>

C++




#include> using> namespace> std;> enum> rainbow{> >violet,> >indigo,> >blue,> >green,yellow,orange,red> }colors;> enum> class> eyecolor:>char>{> >blue,green,brown> }eye;> int> main() {> >cout<<>'size of enum rainbow variable: '><<>sizeof>(colors)< cout<<'size of enum class eyecolor variable:'<

>

npm cache vymazat
>

Výstup

size of enum rainbow variable: 4 size of enum class eyecolor variable:1>

Odkaz: https://en.cppreference.com/w/cpp/language/enum