-1

This question is driving me crazy. I need to get it right so I can pass my class. Thank you to all that try it.

Write a simple program in C++ to investigate the safety of its enumeration types. Include at least 10 different operations on enumeration types to determine what incorrect or just silly things are legal.

Name is Nilay
  • 2,743
  • 4
  • 35
  • 77
Jewpacabra
  • 33
  • 1
  • 4
  • 1
    This is a complex answer, I will post an answer soon, however, showing what you have so far will be of a great help to us, and you. – josephthomas Apr 07 '12 at 03:06
  • 2
    Which standard? C++03 or C++11? – bitmask Apr 07 '12 at 03:08
  • How is this "not a real question"? He did not violate anything from the faq. – josephthomas Apr 07 '12 at 05:13
  • The reason is "not a real question", with the description "It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form". However, the question was clear with what he was asking, it was able to be solved with what he was asking for. I don't think there is any reason to penalize the poster/community for not like the answer that was posted and accepted. I am not trying to seem argumentative, I am just curious for future reference. – josephthomas Apr 07 '12 at 05:21

1 Answers1

-1

Here is a fully operational program that shows ten different types of operations with enums. Details will be in the code, references will be at the bottom of the post.

First, it is very important you understand what enums are. For a reference on this, take a look at Enumerated Types - enums. In the code below I use mathematical operations, bitwise operators and use an enum value to initialize an array to a default size.

#include <iostream>
using namespace std;

enum EnumBits
{
    ONE = 1,
    TWO = 2,
    FOUR = 4,
    EIGHT = 8
};

enum Randoms
{
    BIG_COUNT = 20,
    INTCOUNT = 3
};

int main(void)
{
    // Basic Mathimatical operations
    cout << (ONE + TWO) << endl;    // Value will be 3.
    cout << (FOUR - TWO) << endl;   // Value will be 2.
    cout << (TWO * EIGHT) << endl;  // Value will be 16.
    cout << (EIGHT / TWO) << endl;  // Value will be 4.

    // Some bitwise operations
    cout << (ONE | TWO) << endl;    // Value will be 3.
    cout << (TWO & FOUR) << endl;   // Value will be 0.
    cout << (TWO ^ EIGHT) << endl;  // Value will be 10.
    cout << (EIGHT << 1) << endl;   // Value will be 16.
    cout << (EIGHT >> 1) << endl;   // Value will be 4.

    // Initialize an array based upon an enum value
    int intArray[INTCOUNT];

    // Have a value initialized be initialized to a static value plus
    // a value to be determined by an enum value.
    int someVal = 5 + BIG_COUNT;

    return 0;
}

The code sample done above can be done in another way, which is where you overload the operator|, etc for EnumBits. This is a commonly used technique, for additional references please see How to use enums as flags in C++?.

For a reference on bitwise operations, take a look at Bitwise Operators in C and C++: A Tutorial

Using C++ 11 you can use enums in additional ways, such as strongly typed enums.

Community
  • 1
  • 1
josephthomas
  • 3,256
  • 15
  • 20