1 / 17

Enumerations

Explore the fundamental concepts of enumerations (enums) in programming. This guide provides an in-depth look into the syntax and usage of enums, utilizing examples such as colors, numbers, and playing cards. Learn how enums can enhance code readability and organization by creating named constants. Discover practical applications, like flipping a coin and tallying results with enum types. Whether you're a beginner or looking to refresh your knowledge, this resource is perfect for understanding how and when to implement enums in your projects.

savea
Télécharger la présentation

Enumerations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Enumerations

  2. Your First Enums // syntax enumenum_name {enumerator1, enumerator2, ... enumeratorN}; // example 1 enum color {green, blue, yellow}; // example 2 enum number {one, two, three, four, five}; //example 3 enum {Ace, King, Queen, Jack};

  3. Your First Enums // syntax enumenum_name {enumerator1, enumerator2, ... enumeratorN}; // example 1 enum color {green, blue, yellow}; // example 2 enum number {one, two, three, four, five}; //example 3 enum {Ace, King, Queen, Jack};

  4. Your First Enums // syntax enumenum_name {enumerator1, enumerator2, ... enumeratorN}; // example 1 enum color {green, blue, yellow}; // example 2 enum number {one, two, three, four, five}; //example 3 enum {Ace, King, Queen, Jack};

  5. Your First Enums // syntax enumenum_name {enumerator1, enumerator2, ... enumeratorN}; // example 1 enum color {green, blue, yellow}; // example 2 enum number {one, two, three, four, five}; //example 3 enum {Ace, King, Queen, Jack};

  6. Your First Enums // syntax enumenum_name {enumerator1, enumerator2, ... enumeratorN}; // example 1 enum color {green, blue, yellow}; // example 2 enum number {one, two, three, four, five}; //example 3 enum {Ace, King, Queen, Jack};

  7. Your First Enums // syntax enumenum_name {enumerator1, enumerator2, ... enumeratorN}; // example 1 enum color {green, blue, yellow}; // example 2 enum number {one, two, three, four, five}; //example 3 enum {Ace, King, Queen, Jack};

  8. Your First Enums // example 1 enum color {green, blue, yellow}; color hue; color hue = green; color hue = 2;  color hue = color(2);  intx = hue; // example 2 enumexit {door = 7, window = 3, chimney, drain = 1};

  9. Your First Enums // example 1 enum color {green, blue, yellow}; color hue; color hue = green; color hue = 2;  color hue = color(2);  intx = hue; // example 2 enumexit {door = 7, window = 3, chimney, drain = 1};

  10. Your First Enums // example 1 enum color {green, blue, yellow}; color hue; color hue = green; color hue = 2;  color hue = color(2);  intx = hue; // example 2 enumexit {door = 7, window = 3, chimney, drain = 1};

  11. Your First Enums // example 1 enum color {green, blue, yellow}; color hue; color hue = green; color hue = 2;  color hue = color(2);  intx = hue; // example 2 enumexit {door = 7, window = 3, chimney, drain = 1};

  12. Your First Enums // example 1 enum color {green, blue, yellow}; color hue; color hue = green; color hue = 2;  color hue = color(2);  intx = hue; // example 2 enumexit {door = 7, window = 3, chimney, drain = 1};

  13. Your First Enums // example 1 enum color {green, blue, yellow}; color hue; color hue = green; color hue = 2;  color hue = color(2);  intx = hue; // example 2 enumexit {door = 7, window = 3, chimney, drain = 1};

  14. Your First Enums // example 1 enum color {green, blue, yellow}; color hue; color hue = green; color hue = 2;  color hue = color(2);  intx = hue; // example 2 enumexit {door = 7, window = 3, chimney, drain = 1};

  15. Readable int main() { srand(time(NULL));     short count_heads = 0;     short count_tails = 0; enumcoin_toss {head, tail}; coin_toss flip;     string tosses[2] = {“head”,”tail”};      for (inti=1; i<=200; i++) {         flip = coin_toss(rand()%2); cout<<”we flipped a “<<tosses[flip]<<endl;         flip ? count_tails++ : count_heads++ ;     // tally the flips } cout<<endl<<"We tossed: "<<count_heads<<" heads"<<endl<<" "<<count_tails<<" tails"<<endl; return 0; }

  16. Readable output we flipped a headwe flipped a head we flipped a tail we flipped a head we flipped a tail we flipped a tail ...We tossed:110 heads 90 tails int main() { srand(time(NULL));     short count_heads = 0;     short count_tails = 0; enumcoin_toss {head, tail}; coin_toss flip;     string tosses[2] = {“head”,”tail”};      for (inti=1; i<=200; i++) {         flip = coin_toss(rand()%2); cout<<”we flipped a “<<tosses[flip]<<endl;         flip ? count_tails++ : count_heads++ ;     // tally the flips } cout<<endl<<"We tossed: "<<count_heads<<" heads"<<endl<<" "<<count_tails<<" tails"<<endl; return 0; }

  17. End of Session

More Related