Enums vai uzskaitījuma veids (uzskaitījums) ir lietotāja definēts datu tips, kuram var piešķirt dažas ierobežotas vērtības. Šīs vērtības nosaka programmētājs uzskaitītā tipa deklarēšanas laikā.
Nepieciešamība pēc Enum klases, nevis Enum veida:
Tālāk ir norādīti daži no iemesliem, kādi ir Enum Type ierobežojumi un kāpēc mums ir nepieciešama Enum klase, lai tos aptvertu.
1.Enum ir veselu skaitļu konstantu kopums, kas nozīmē, ka katram elementam tiek piešķirta vesela skaitļa vērtība. 2.Tas tiek deklarēts ar enum atslēgvārdu.
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;> }> |
>
>
kā pārvērst virkni par java veselu skaitliIzvade
The roll no of avinash=68>
Divas uzskaitījumi nevar koplietot tos pašus vārdus:
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;> }> |
>
>
Kompilācijas kļūda:
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; ^> Nevienam mainīgajam nevar būt nosaukums, kas jau ir kādā uzskaitījumā:
CPP
dijkstra
#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;> }> |
>
fmoviez
>
Kompilācijas kļūda:
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, ^> Enums nav tipa drošs :
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;> }> |
>
zemesrieksts vs zemesrieksts
>
Brīdinājums:
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) ^>
Enum klase
C++11 ir ieviesis enum klases (sauktas arī vērienīgie uzskaitījumi ), kas veido uzskaitījumus gan stingri drukāts un stingri tvērēts . Klases uzskaitījums neļauj netieši pārveidot par int, kā arī nesalīdzina skaitītājus no dažādiem uzskaitījumiem.
Lai definētu enum klasi, mēs izmantojam klases atslēgvārdu pēc enum atslēgvārda.
Sintakse:
// Declaration enum class EnumName{ Value1, Value2, ... ValueN}; // Initialisation EnumName ObjectName = EnumName::Value;> Piemērs:
// Declaration enum class Color{ Red, Green, Blue}; // Initialisation Color col = Color::Red;> Tālāk ir sniegta ieviešana, lai parādītu Enum klasi
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;> }> |
>
>Izvade
It's not Red Good people 1>
Uzskaitītajiem tipiem, kas deklarēti par enum klasi, ir arī lielāka kontrole pār to pamatā esošo tipu; tas var būt jebkurš integrāls datu tips, piemēram, char, short vai unsigned int, kas būtībā kalpo, lai noteiktu tipa lielumu.
To norāda ar kolu un pamatā esošo tipu, kas seko uzskaitītajam tipam:
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:'< |
>
vlc lejupielādēt youtube
>Izvade
size of enum rainbow variable: 4 size of enum class eyecolor variable:1>
Atsauce: https://en.cppreference.com/w/cpp/language/enum