Q:

C++ Enumeration | Find output programs

belongs to collection: C++ find output programs

0

This section contains the C++ find output programs with their explanations on C++ Enumeration.

Program 1:

#include <iostream>
using namespace std;

enum WeekDays { SUN,
    MON,
    TUE,
    WED,
    THU,
    FRI,
    SAT };

int main()
{
    WeekDays Today;
    Today = MON;
    cout << "Week Day: " << Today + 1;
    
    return 0;
}

Program 2:

#include <iostream>
using namespace std;

enum WeekDays {
    SUN = 1,
    MON,
    TUE,
    WED,
    THU,
    FRI,
    SAT
};

int main()
{
    WeekDays Today;
    Today = MON;
    cout << "Week Day: " << Today;

    return 0;
}

Program 3:

#include <iostream>
using namespace std;

enum Forces {
    Army = 101,
    Navy = 102,
    Airforce = 103
};

int main()
{
    Forces F;
    cout << F.Navy;

    return 0;
}

Program 4:

#include <iostream>
using namespace std;

enum Force {
    Army = 2,
    Navy = 4,
    Airforce = 8
};

int main()
{
    int WAR = Army | Airforce;
    cout << WAR;

    return 0;
}

Program 5:

#include <iostream>
using namespace std;

enum Forces {
    Army = 2,
    Navy = 4,
    Airforce = 8
};

int main()
{
    Army = 5;
    cout << Army;
    return 0;
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Program 1:

Output:

Week Day: 2

Explanation:

It will print "Week Day: 2" on the console screen.  In the program, we created an enum WeekDays That contains 7 items they automatically initialized from 0 to 6 sequentially by default.

In the main() function, here we created enum variable Today which is initialized with MON, and the value of MON is 1. Then finally we print Today+1 that is 2 on the console screen.

Answer Program 2:

Output:

Week Day: 2

Explanation:

It will print "Week Day: 2" on the console screen. In the program, we created an enum WeekDays that contains 7 items, and we initialized SUN by 1, then the other values are,

MON = 2
TUE = 3
WED = 4
THU = 5
FRI = 6
SAT = 7

In the main() function, here we created enum variable Today which is initialized with MON, and the value of MON is 2. Then finally we print Today that is 2 on the console screen.

Answer Program 3:

Output:

main.cpp: In function ‘int main()’:
main.cpp:13:15: error: request for member ‘Navy’ in ‘F’, 
which is of non-class type ‘Forces’
     cout << F.Navy;
               ^~~~

Explanation:

It will generate a compilation error because we can use Navy directly instead of creating a variable of enum Forces, and accessing the Navy using membership operator.

Answer Program 4:

Output:

10

Explanation:

It will print 10 on the console screen. In the program, we created an enum Force that contains items "Army", "Navy", and "Airforce".

In the main() function, we initialized WAR with "Army|Airforce", here we performed BITWISE OR (|) operation between "Army" and "Airforce" whose values are 2 and 8.

0000 0010
0000 1000
=========
0000 1010

Then, the corresponding decimal value of the above binary number is 10. Thus, the Final "10" will be printed on the console screen.

Answer Program 5:

Output:

main.cpp: In function ‘int main()’:
main.cpp:12:12: error: lvalue required as left operand of assignment
     Army = 5;
            ^

Explanation:

It will generate compilation error because Enumerations are constants, we cannot modify the value of defined enumerations.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ Inheritance | Find output programs | Set 1... >>
<< C++ Operator Overloading | Find output programs | ...