In: Computer Science
(1)Discuss the syntax for combining the definition and declaration of an enumeration type into one statement. Provide some examples.
(2) Describe the syntax of a namespace statement. Discuss the syntax for accessing a namespace member. Also, review the syntax for the using namespace statement and discuss how this simplifies the process of accessing namespace members.
1). ANSWER :
GIVENTHAT :
To Discuss the syntax for combining the definition and declaration of an enumeration type into one statement. Provide some examples
An enumeration type is declared and defined using enum keyword using the following syntax: enum NAME { val1, val2, ... } ENUM_VAR; Here, NAME is the name of the enumeration, val1, val2, etc are values of the enum NAME, and ENUM_VAR is an enum variable of type NAME declared with definition of the enum. The default value of val1 is 0, val2 is 1, and so on. To change default values of enum, we could use the following syntax: enum NAME { val1 = 1, val2 = 3, ... } ENUM_VAR; Here, val1 is 1 and val2 is 3. Some examples: enum Fruit { apple, orange, guava } HealthyFruit; enum Color { red = 1, green = 2, blue = 3 } ShirtColor;
2).To Describe the syntax of a namespace statement. Discuss the syntax for accessing a namespace member. Also, review the syntax for the using namespace statement and discuss how this simplifies the process of accessing namespace members.
A namespace in C++ is declared using the following syntax: namespace NAME{ identifier 1 identifier 2 ... } Here, NAME is the name of the namespace and identifier 1, identifier 2 are members of the namespace NAME. For example: namespace foo{ int x, y; } Here, x and y are members of foo namespace. To access namespace members, we could use the following syntax: NAME::member_name; For example, // To access member x of namespace foo foo::x The using namespace statement is used to access a namespace's member without the need of explicitly mentioning namespace (along with scope resolution operator) everytime we access that member. Syntax for using namespace statement: using namespace foo; Now in order to access members of namespace foo, we can simply write x instead of foo::x. This simplyfies the process of accessing the namespace members by removing the need to write namespace name each and everytime. This is commonly used in case of C++'s std namespace by using namespace std; before the main function.