×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

C++ Enumeration | Find output programs

This section contains the C++ find output programs with their explanations on C++ Enumeration.
Submitted by Nidhi, on July 03, 2020

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;
}

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.

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;
}

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.

Program 3:

#include <iostream>
using namespace std;

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

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

    return 0;
}

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.

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;
}

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.

Program 5:

#include <iostream>
using namespace std;

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

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

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.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.