0% found this document useful (0 votes)
38 views8 pages

Enumeration

The document discusses enumerations in C++ programming. It defines an enumeration as a user-defined data type consisting of integral constants that specifies all possible values a variable of that type can have. An example enum defines the days of the week. Enums improve code readability and understanding by allowing a variable to only take on set values. Sample code shows declaring an enum for suits and setting a variable to one of the enum values.

Uploaded by

saad.pisjes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views8 pages

Enumeration

The document discusses enumerations in C++ programming. It defines an enumeration as a user-defined data type consisting of integral constants that specifies all possible values a variable of that type can have. An example enum defines the days of the week. Enums improve code readability and understanding by allowing a variable to only take on set values. Sample code shows declaring an enum for suits and setting a variable to one of the enum values.

Uploaded by

saad.pisjes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 8

Programming Language

INSTRUCTOR: Dr. Benish Amin


2

Fundamentals of Enumeration

• Enumeration
– An enumeration is a user-defined data type that consists of
integral constants.
– It specifies all the possible values a variable of that type can
have. E.g. week enumeration can have seven days of week.
– To define an enumeration, keyword enum is used.
3

Fundamentals of Enumeration

• Example
– enum season {spring, summer, autumn, winter};
– Here season is name of enumeration.
– And, spring, summer and winter are values of type season.
– By default, spring is 0, summer is 1 and so on. You can change
the default value of an enum element during declaration (if
necessary).
4

Example
• Example
enum season
{
spring=0,
summer=4,
autumn=8,
winter=12
};
5

Why to use Enums in C++ Programming

• An enum variable takes only one value out of


many possible values.
• It improves code readability and understanding.
6
1 #include <iostream>
2
3 using namespace std;
4
5 enum week{ Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
6
7 int main()
8 {
9 week today;
10 today = Wednesday;
11 cout<<“Day ”<< today+1;
12 return 0;
13 }

Day 4
7
1 // code 15
2 // Using enum.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8
9
10
11
12 enum suit{
13 club =0,
14 diamonds =10,
15 hearts =20,
16 spades =30,
17 } card;
18 int main()
19 {
20 card =club;
21
• cout<<“value is ”<< hearts;
• return 0;
• }
25

value is 20
8
1 // code 15
2 // Using enum.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8
9
10
11
12 enum suit{
13 club =0,
14 diamonds =10,
15 hearts =20,
16 spades =30,
17 } card;
18 int main()
19 {
20 card =club;
21
• cout<<“Size of enum variable ”<< sizeof(card) << “bytes.”;
• return 0;
• }
25

Size of enum variable 4 bytes.

You might also like