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

Enumerated data types

Embed Size (px)

Citation preview

Page 1: Enumerated data types

BY

KAUSHAL JHUNJHUNWALA-1BM15IS031

ANKIT SINHA-1BM15IS007

Juan Martin Carlés.

ENUMERATED DATA TYPES

Page 2: Enumerated data types

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

TOPICS

Page 3: Enumerated data types

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

Page 4: Enumerated data types

SYNTAX

enum enum_name {identifier list} enum_type;

example: Example:

enum BOOLEAN enum BOOLEAN

{TRUE,FALSE}

{TRUE=0,FALSE=1}

b1,b2; b1,b2;

Page 5: Enumerated data types

A N O T H E R E X A M P L EG U E S S T H E O U T P U T ?

void main() { enum WeekDays {Mon,Tues,Wed,Thurs,Fri,Sat,Sun}days; int i; for(i=Mon;i<=Sun;i++) { cout<<i<<“ “; } }

outputCase 1: Mon=0 0 1 2 3 4 5 6Case 2: Mon=32767 Error: Numeric constants too large.Case 3: Mon=2 Thurs=9 Sun=Mon+Wed 2 3 4 9 10 11 6Case 4: Mon=32760 32760 32761 32762 32763 32764 32765 32766

Page 6: Enumerated data types

SCOPED AND UNSCOPED ENUMS

SYNTAX

//unscoped enum: //scoped

enum:

enum [identifier] [: type] enum

[class] [struct]

{enum_list}; [identifier]

[: type]

{enum_list};

Page 7: Enumerated data types

SAMPLE PROGRAM SNIPPETNamespace 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.

Page 8: Enumerated data types

CASTING RULESnamespace 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 }

Page 9: Enumerated data types

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.

Page 10: Enumerated data types

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.

Page 11: Enumerated data types

PR INT ING TH E “ST R ING” REPRESENTAT ION 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.

Page 12: Enumerated data types

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

Page 13: Enumerated data types

THANK YOU..