Enumerated data types

AnkitSinha69 741 views 13 slides Nov 02, 2016
Slide 1
Slide 1 of 13
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13

About This Presentation

The concept of enumeration in C


Slide Content

BY KAUSHAL JHUNJHUNWALA-1BM15IS031 ANKIT SINHA-1BM15IS007 Juan Martin Carlés . ENUMERATED DATA TYPES

WHAT IS ENUMERATED DATA TYPES? Syntax for using enums . Scoped and unscoped enums Why enums are used in c++ programming . How to use enums with flags . Printing the string representation of enums . TOPICS

WHAT IS ENUMERATED DATA TYPES? ENUMER ATED DATA TYPES GIVES YOU AN OPPORTUNITY TO: INVENT YOUR OWN DATATYPE DEFINE WHAT VALUES OF THE VARIABLES THIS DATATYPE CAN TAKE REPLACE NUMBERS WITH WORDS

syntax enum enum_name { identifier list } enum_type ; example : Example : enum BOOLEAN enum BOOLEAN {TRUE,FALSE} {TRUE=0,FALSE=1} b1,b2; b1,b2;

Another example guess the output? void main() { enum WeekDays { Mon,Tues,Wed,Thurs,Fri,Sat,Sun }days; int i ; for( i = Mon;i <= Sun;i ++) { cout << i <<“ “; } } output Case 1: Mon=0 0 1 2 3 4 5 6 Case 2: Mon=32767 Error: Numeric constants too large. Case 3: Mon=2 Thurs=9 Sun= Mon+Wed 2 3 4 9 10 11 6 Case 4: Mon=32760 32760 32761 32762 32763 32764 32765 32766

SCOPED AND UNSCOPED ENUMS SYNTAX // unscoped enum : //scoped enum : enum [identifier] [: type] enum [class] [ struct ] { enum_list }; [identifier][: type] { enum_list };

Sample program snippet Namespace card_scoped { enum class Suit { Diamonds,Hearts,Clubs,Spades }; void play_card (Suit suit ) { if(suit==Suit::Clubs) {/* ……………*/} } } //enumerator must be qualified by enum type. Namespace card_nonscoped { enum Suit { Diamonds,Hearts,Clubs,Spades }; void play_card (Suit suit ) { if(suit==Clubs) {/* ……………*/} } } //enumerator is visible without qualification.

Casting rules namespace ScopedEnumConversions { enum class Suit{ Diamonds,Hearts,Clubs,Spades }; void attemptConversions () { Suit hands; hands=Clubs; //error ‘ Clubs’:undeclared idenifier hands=Suit::Clubs; //correct int a=135692; hands=a; // error: cannot convert from ‘ int ’ to ‘Suit’ a=Suit::Hearts; //error: cannot convert from ‘suit’ to ‘ int ’ a= static_cast < int >(Suit::Hearts); // correct }

WHY ENUMS ARE USED? enum variables take only one value out of many possible values . # include< iostream > enum suites{clubs=0,diamonds=10,hearts=20,spades=3}card; int main() { card=club; cout <<“size of enum variable=“<< sizeof (card); return 0; } Output: size of enum variable=4 This is because the size of an integer is 4 bytes.This makes enums a good choice to work with flags.

HOW TO USE ENUMS WITH FLAGS? #include< stdio.h > Enum designflags {Bold=1,Italics=2,Underline=4}button; int main() { int mydesign = Bold|Underline ; /*Bold=00000001 cout << mydesign ; Italics=00000010 return 0; Underline=00000100 } 00000010|00000100 */ Output: 5 Explanation: Suppose you are designing a button for Windows application.You cam set the flags Italics,Bold and Underline to work with the text.There is a reason why all integral constants are chosen in powers of 2. Since the integral constants are in powers of 2,we can combine two or more flags at once without overlapping using bitwise OR| operator. The output 5 indicates both Bold and Underline flags are used.

Printing the “ string ” representation of enums int main() { enum weekdays{ Sun,Mon,Tues,Wed,Thurs,Fri,Sat }days; const char* daynames []={ Sun,Mon,Tues,Wed,Thurs,Fri,Sat }; for( int i = Sun;i <= Sat,i ++) { cout << daynames [ i ]<<“ “; } return 0; } Explanation: To print the string associated with the enum values,we make use of the above method.The trick is to create an array of strings and use the enums to index the array.

An alternative way... void main() { enum fruits{ apple,guava,orange } myFruit ; int i;0 cout <<“Enter your choice(0 to 2)””<< endl ; switch( i ) { case apple: cout <<“Apple”; break; case guava: cout <<“Guava”; break; case orange: cout <<“Orange”; break; }

THANK YOU..
Tags