logo

Enum-luokat C++-kielellä ja niiden etu Enum DataTypeen verrattuna

Enums tai lueteltu tyyppi (luettelo) on käyttäjän määrittämä tietotyyppi, jolle voidaan määrittää joitain rajoitettuja arvoja. Ohjelmoija määrittelee nämä arvot ilmoitettaessa lueteltua tyyppiä.

Enum-luokan tarve Enum-tyypin sijaan:
Alla on joitain syitä siihen, mitkä ovat Enum Typen rajoitukset ja miksi tarvitsemme Enum-luokan kattamaan ne.



1.Enum on kokoelma nimettyjä kokonaislukuvakioita, mikä tarkoittaa, että jokainen elementti on määritetty kokonaislukuarvon mukaan. 2.Se ilmoitetaan enum-avainsanalla.

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;> }>

>

>

arraylist.sort
Lähtö

The roll no of avinash=68>

Kaksi luetteloita ei voi jakaa samoja nimiä:

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;> }>

>

>

Kokoonpanovirhe:

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; ^>

Millään muuttujalla ei voi olla nimeä, joka on jo jossain luettelossa:

java merkkijono cmp

CPP




#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;> }>

javascriptin pudotusvalikosta

>

>

Kokoonpanovirhe:

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, ^>

Enum ei ole tyyppiturvallinen :

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;> }>

>

>

Varoitus:

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 luokka

C++11 on ottanut käyttöön enum-luokat (kutsutaan myös kattavat luettelot ), mikä tekee luetteloista molemmat vahvasti kirjoitettu ja vahvasti soveltuva . Luokkaluettelo ei salli implisiittistä muuntamista int-muotoon, eikä myöskään vertaa eri luetteloiden luetteloijia.
Enum-luokan määrittämiseen käytämme luokka-avainsanaa enum-avainsanan perään.
Syntaksi:

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

Esimerkki:

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

Alla on toteutus Enum-luokan näyttämiseksi

CPP




java merkkijono korvaaminen
// 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;> }>

>

>

Lähtö

It's not Red Good people 1>

Enumeroidut tyypit, jotka on ilmoitettu enum-luokassa, hallitsevat myös paremmin taustalla olevaa tyyppiään; se voi olla mikä tahansa integroitu tietotyyppi, kuten char, short tai unsigned int, joka olennaisesti määrittää tyypin koon.

Tämä määritellään kaksoispisteellä ja alla olevalla tyypillä lueteltua tyyppiä seuraavana:

array vs arraylist
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:'<

>

>

Lähtö

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

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