SlideShare a Scribd company logo
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}da
ys;
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..

More Related Content

PPTX
structures in C and Union in C, enumerated datatype
PPTX
Sparse matrix and its representation data structure
PPTX
String, string builder, string buffer
PPT
358 33 powerpoint-slides_6-strings_chapter-6
PPTX
QBE.pptx
PPTX
PDF
Python-List comprehension
PPTX
Asymptotic Notation and Data Structures
structures in C and Union in C, enumerated datatype
Sparse matrix and its representation data structure
String, string builder, string buffer
358 33 powerpoint-slides_6-strings_chapter-6
QBE.pptx
Python-List comprehension
Asymptotic Notation and Data Structures

What's hot (20)

PDF
Tuples in Python
PPTX
Array in C
PDF
Function arguments In Python
PPTX
PPTX
TRIES_data_structure
PDF
Trie Data Structure
PPTX
Exception handling c++
PDF
Immutable vs mutable data types in python
PPTX
Arrays in c
PPTX
Joins & constraints
PPTX
NumPy.pptx
PPTX
Stored procedures
PPT
Structures
PDF
Python Advanced – Building on the foundation
PDF
Strings in java
PPTX
NUMPY-2.pptx
PPTX
Database connectivity to sql server asp.net
PPT
Algo>Abstract data type
PPTX
C++ string
PPTX
SQL - DML and DDL Commands
Tuples in Python
Array in C
Function arguments In Python
TRIES_data_structure
Trie Data Structure
Exception handling c++
Immutable vs mutable data types in python
Arrays in c
Joins & constraints
NumPy.pptx
Stored procedures
Structures
Python Advanced – Building on the foundation
Strings in java
NUMPY-2.pptx
Database connectivity to sql server asp.net
Algo>Abstract data type
C++ string
SQL - DML and DDL Commands
Ad

Viewers also liked (19)

PPTX
eCommerce and online consumer behaviour Infographics
PDF
Kristyna_Erbenova_-_Paul_Fierlinger
PPTX
Viví un verano
PPTX
GICSA
PPT
Reeder-Bio
PPT
85. transformando nuestro rincon de lecturas
PPTX
Tamás M. Kocsis - Hungarian case law FINAL
PPT
Solo entre chavos
PDF
Normas de present. de trabajos de investig. 2016 6-16
DOCX
Metodos ll politicas de inclusion
PPTX
55. por una alimentación balanceada
PPTX
Presentation BLOG
PPTX
Que los alumnos tengan buena alimentación
PPTX
44.el daño irreparable a nuestro planeta
PPTX
272. mejorando mi ambiente
PDF
Aslam CV
PPTX
Осінній марафон
PPTX
The Heart of Jesus (Part 2) Separate Yourself For Battle: John 17:6-19
PPTX
192.calentamiento global
eCommerce and online consumer behaviour Infographics
Kristyna_Erbenova_-_Paul_Fierlinger
Viví un verano
GICSA
Reeder-Bio
85. transformando nuestro rincon de lecturas
Tamás M. Kocsis - Hungarian case law FINAL
Solo entre chavos
Normas de present. de trabajos de investig. 2016 6-16
Metodos ll politicas de inclusion
55. por una alimentación balanceada
Presentation BLOG
Que los alumnos tengan buena alimentación
44.el daño irreparable a nuestro planeta
272. mejorando mi ambiente
Aslam CV
Осінній марафон
The Heart of Jesus (Part 2) Separate Yourself For Battle: John 17:6-19
192.calentamiento global
Ad

Similar to Enumerated data types (20)

PPTX
Chapter1.pptx
PPTX
Intermediate code generation1
PPTX
PDF
Introduction to programming - class 11
PDF
Week 02_Development Environment of C++.pdf
PPTX
C programming enumeration
PPT
02a fundamental c++ types, arithmetic
PDF
POLITEKNIK MALAYSIA
PPTX
02. Primitive Data Types and Variables
PPTX
lecture 2.pptx
PPTX
module 3 BTECH FIRST YEAR ATP APJ KTU PYTHON
PDF
23UCACC11 Python Programming (MTNC) (BCA)
PPTX
Pseudocode
DOCX
C++ Tutorial.docx
PPT
keyword
PPT
keyword
PPTX
CPP Homework Help
PPT
C++ programming
PPTX
C PROGRAMMING document for beginners....
DOC
Arrays In General
Chapter1.pptx
Intermediate code generation1
Introduction to programming - class 11
Week 02_Development Environment of C++.pdf
C programming enumeration
02a fundamental c++ types, arithmetic
POLITEKNIK MALAYSIA
02. Primitive Data Types and Variables
lecture 2.pptx
module 3 BTECH FIRST YEAR ATP APJ KTU PYTHON
23UCACC11 Python Programming (MTNC) (BCA)
Pseudocode
C++ Tutorial.docx
keyword
keyword
CPP Homework Help
C++ programming
C PROGRAMMING document for beginners....
Arrays In General

Recently uploaded (20)

PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
history of c programming in notes for students .pptx
PDF
AI in Product Development-omnex systems
PPTX
Introduction to Artificial Intelligence
PPTX
Odoo POS Development Services by CandidRoot Solutions
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Digital Strategies for Manufacturing Companies
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
L1 - Introduction to python Backend.pptx
PDF
System and Network Administration Chapter 2
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
Transform Your Business with a Software ERP System
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
ai tools demonstartion for schools and inter college
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
history of c programming in notes for students .pptx
AI in Product Development-omnex systems
Introduction to Artificial Intelligence
Odoo POS Development Services by CandidRoot Solutions
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Digital Strategies for Manufacturing Companies
How to Migrate SBCGlobal Email to Yahoo Easily
L1 - Introduction to python Backend.pptx
System and Network Administration Chapter 2
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Transform Your Business with a Software ERP System
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Reimagine Home Health with the Power of Agentic AI​
CHAPTER 2 - PM Management and IT Context
ai tools demonstartion for schools and inter college
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Design an Analysis of Algorithms II-SECS-1021-03
VVF-Customer-Presentation2025-Ver1.9.pptx

Enumerated data types

  • 2. 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
  • 3. 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
  • 4. SYNTAX enum enum_name {identifier list} enum_type; example: Example: enum BOOLEAN enum BOOLEAN {TRUE,FALSE} {TRUE=0,FALSE=1} b1,b2; b1,b2;
  • 5. ANOTHER EXAMPLE GUESS THE OUTPUT? void main() { enum WeekDays {Mon,Tues,Wed,Thurs,Fri,Sat,Sun}da ys; 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
  • 6. SCOPED AND UNSCOPED ENUMS SYNTAX //unscoped enum: //scoped enum: enum [identifier] [: type] enum [class] [struct] {enum_list}; [identifier][: type] {enum_list};
  • 7. 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.
  • 8. 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 }
  • 9. 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.
  • 10. 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.
  • 11. 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.
  • 12. 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; }