enumerations
play

Enumerations http://cs.mst.edu Your First Enums // syntax enum - PowerPoint PPT Presentation

Enumerations http://cs.mst.edu Your First Enums // syntax enum enum_name {enumerator1, enumerator2, ... enumeratorN}; // example 1 enum color {green, blue, yellow}; // example 2 enum number {one, two, three, four, five}; //example 3 enum


  1. Enumerations http://cs.mst.edu

  2. Your First Enums // syntax enum enum_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}; http://cs.mst.edu

  3. Your First Enums // syntax enum enum_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}; http://cs.mst.edu

  4. Your First Enums // syntax enum enum_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}; http://cs.mst.edu

  5. Your First Enums // syntax enum enum_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}; http://cs.mst.edu

  6. Your First Enums // syntax enum enum_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}; http://cs.mst.edu

  7. Your First Enums // syntax enum enum_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}; http://cs.mst.edu

  8. Your First Enums // example 1 enum color {green, blue, yellow}; color hue; color hue = green; color hue = 2; color hue = color(2); int x = hue; // example 2 enum exit {door = 7, window = 3, chimney, drain = 1}; http://cs.mst.edu

  9. Your First Enums // example 1 enum color {green, blue, yellow}; color hue; color hue = green; color hue = 2; color hue = color(2); int x = hue; // example 2 enum exit {door = 7, window = 3, chimney, drain = 1}; http://cs.mst.edu

  10. Your First Enums // example 1 enum color {green, blue, yellow}; color hue; color hue = green; color hue = 2; color hue = color(2); int x = hue; // example 2 enum exit {door = 7, window = 3, chimney, drain = 1}; http://cs.mst.edu

  11. Your First Enums // example 1 enum color {green, blue, yellow}; color hue; color hue = green; color hue = 2; color hue = color(2); int x = hue; // example 2 enum exit {door = 7, window = 3, chimney, drain = 1}; http://cs.mst.edu

  12. Your First Enums // example 1 enum color {green, blue, yellow}; color hue; color hue = green; color hue = 2; color hue = color(2); int x = hue; // example 2 enum exit {door = 7, window = 3, chimney, drain = 1}; http://cs.mst.edu

  13. Your First Enums // example 1 enum color {green, blue, yellow}; color hue; color hue = green; color hue = 2; color hue = color(2); int x = hue; // example 2 enum exit {door = 7, window = 3, chimney, drain = 1}; http://cs.mst.edu

  14. Your First Enums // example 1 enum color {green, blue, yellow}; color hue; color hue = green; color hue = 2; color hue = color(2); int x = hue; // example 2 enum exit {door = 7, window = 3, chimney, drain = 1}; http://cs.mst.edu

  15. Readable int main() { srand(time(NULL)); short count_heads = 0; short count_tails = 0; enum coin_toss {head, tail}; coin_toss flip; string tosses[2] = {“head”,”tail”}; for (int i=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; } http://cs.mst.edu

  16. Readable output int main() { we flipped a head srand(time(NULL)); we flipped a head short count_heads = 0; we flipped a tail short count_tails = 0; we flipped a head enum coin_toss {head, tail}; coin_toss flip; we flipped a tail string tosses[2] = {“head”,”tail”}; we flipped a tail ... for (int i=1; i<=200; i++) We tossed:110 heads { 90 tails 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; } http://cs.mst.edu

  17. End of Session http://cs.mst.edu

Recommend


More recommend