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 {Ace, King, Queen, Jack}; 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 {Ace, King, Queen, Jack}; 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 {Ace, King, Queen, Jack}; 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 {Ace, King, Queen, Jack}; 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 {Ace, King, Queen, Jack}; 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 {Ace, King, Queen, Jack}; http://cs.mst.edu
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
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
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
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
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
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
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
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
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
End of Session http://cs.mst.edu
Recommend
More recommend