Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Edit View Insert Cell Kemel Widgets In [ ]: enum CustomDayofweek { sUN - 1, MON, TUE, WED, THU, FRI, SAT s; Here will MON
Edit View Insert Cell Kemel Widgets In [ ]: enum CustomDayofweek \{ sUN - 1, MON, TUE, WED, THU, FRI, SAT s; Here will MON will the be assigned the next integer after SUN which is 2, TUE will be 3 , and so on. Notice here that the enumerated values (SUN through SAT) defined in the CustomDayofweek enumeration are the same names defined in the Dayofweek enumeration. This can confuse the compiler. To see that, uncomment the following to create a variable of this new enumeration and print its value. In [ ]: // CustomDayofiweek aday =WED; 1/ cout "A day is: " aday endl; Are you getting the error: reference to 'WED' is ambiguous error? Why? It turns out that using the value WED, confuses the compiler because it does not know whether you meant WED from Dayofweek or WED from CustomDayofweek. There are two ways to fix that: - either be specific and use the resolution operator :: to fully qualify the enumerated values we are using. For example, you can redefine the above a_day and print it as follows (Notice the use of CustomDayofweek: :WED instead of just WED ): Scoped enumerations To deal with the above ambiguity problem, C++11 introduced scoped enumerations which are the same as the above C-style enumerations, except that we'll always have to use the resolution operator :: with their values. Here is how we define a scoped enumeration for days of the week. Notice the keyword class right after enum. This keyword makes this enumeration scoped. In [ ]: enum class DayofWeek_ 2\{ SUNDAY - 1, MONDAY, TUESDAY, WEDNESOAY, THURSDAY, FRIDAY, SATURDAY 3; By doing this, we can no longer use the names SUNDAY. WEDNESDAY, and so on, directly. If you do so, you'll get an error. Uncomment the following line and re-run the cell to see the error. In [ ]: // DayofWeek_2 day_2 - WEDNESDAY; Instead, we must qualify these values using the name of the enumeration and the resolution operator :: In [ ]: DayofWeek_2 day_2 - DayofWeek_2: :wEDNESDAY; This makes scoped enumerations safer to use. As a matter of fact, it is highly recommended that you use scoped enumerations over the C-style unscoped ones. CODING CHALLENGE 3 In the code cell below, define a scoped enumeration for the money change (penny, nickel, dime, quarter). In the next cell, define a variable named change and make it equal to a dime. By convention, use uppercase for values of the enumeration. In [ ]: /1 Tooo: define enumeration In [ ] : //TOD0: define an emumeration variable named change
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started