0% found this document useful (0 votes)
3 views20 pages

Lec21 Enum

Chapter 11 discusses pointers to structures and enumerated data types in programming. It explains how to use pointers to access structure members and the concept of enumerated types, which are programmer-defined data types consisting of integer constants. Additionally, it covers the rules for assigning values to enum variables and the uniqueness of enumerators within the same scope.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views20 pages

Lec21 Enum

Chapter 11 discusses pointers to structures and enumerated data types in programming. It explains how to use pointers to access structure members and the concept of enumerated types, which are programmer-defined data types consisting of integer constants. Additionally, it covers the rules for assigning values to enum variables and the uniqueness of enumerators within the same scope.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Chapter 11:

Structured Data

Copyright © 2012 Pearson Education, Inc.


11.9
Pointers to Structures

Copyright © 2012 Pearson Education, Inc.


Pointers to Structures
• A structure variable has an address
• Pointers to structures are variables that
can hold the address of a structure:
Student *stuPtr;
• Can use & operator to assign address:
stuPtr = & stu1;
• Structure pointer can be a function
parameter

11-3
Copyright © 2012 Pearson Education, Inc.
Accessing Structure Members
via Pointer Variables
• Must use () to dereference pointer
variable, not field within structure:
cout << (*stuPtr).studentID;

• Can use structure pointer operator to


eliminate () and use clearer notation:
cout << stuPtr->studentID;

11-4
Copyright © 2012 Pearson Education, Inc.
From Program 11-8

Copyright © 2012 Pearson Education, Inc.


11.12
Enumerated Data Types

Copyright © 2012 Pearson Education, Inc.


Enumerated Data Types

• An enumerated data type is a programmer-


defined data type. It consists of values
known as enumerators, which represent
integer constants.

Copyright © 2012 Pearson Education, Inc.


Enumerated Data Types
• Example:
enum Day { MONDAY, TUESDAY,
WEDNESDAY, THURSDAY,
FRIDAY };
• The identifiers MONDAY, TUESDAY,
WEDNESDAY, THURSDAY, and FRIDAY,
which are listed inside the braces, are
enumerators. They represent the values
that belong to the Day data type.

Copyright © 2012 Pearson Education, Inc.


Enumerated Data Types

enum Day { MONDAY, TUESDAY,


WEDNESDAY, THURSDAY,
FRIDAY };

Note that the enumerators are not strings,


so they aren’t enclosed in quotes.
They are identifiers.

Copyright © 2012 Pearson Education, Inc.


Enumerated Data Types
• Once you have created an enumerated
data type in your program, you can define
variables of that type. Example:

Day workDay;

• This statement defines workDay as a


variable of the Day type.

Copyright © 2012 Pearson Education, Inc.


Enumerated Data Types

• We may assign any of the enumerators


MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, or FRIDAY to a variable of the
Day type. Example:

workDay = WEDNESDAY;

Copyright © 2012 Pearson Education, Inc.


Enumerated Data Types

• So, what is an enumerator?


• Think of it as an integer named constant
• Internally, the compiler assigns integer
values to the enumerators, beginning at 0.

Copyright © 2012 Pearson Education, Inc.


Enumerated Data Types
enum Day { MONDAY, TUESDAY,
WEDNESDAY, THURSDAY,
FRIDAY };
In memory...
MONDAY = 0
TUESDAY = 1
WEDNESDAY = 2
THURSDAY = 3
FRIDAY = 4
Copyright © 2012 Pearson Education, Inc.
Enumerated Data Types
• Using the Day declaration, the following
code...
cout << MONDAY << " "
<< WEDNESDAY << " “
<< FRIDAY << endl;

...will produce this output:

0 2 4
Copyright © 2012 Pearson Education, Inc.
Assigning an integer to an enum
Variable
• You cannot directly assign an integer value
to an enum variable. This will not work:

workDay = 3; // Error!
• Instead, you must cast the integer:

workDay = static_cast<Day>(3);

Copyright © 2012 Pearson Education, Inc.


Assigning an Enumerator to an int
Variable
• You CAN assign an enumerator to an int
variable. For example:

int x;
x = THURSDAY;

• This code assigns 3 to x.

Copyright © 2012 Pearson Education, Inc.


Comparing Enumerator Values

• Enumerator values can be compared using


the relational operators. For example, using
the Day data type the following code will
display the message "Friday is greater than
Monday.“
if (FRIDAY > MONDAY)
{
cout << "Friday is greater "
<< "than Monday.\n";
}
Copyright © 2012 Pearson Education, Inc.
Using an enum Variable to Step
through an Array's Elements
• Because enumerators are stored in memory as
integers, you can use them as array subscripts.
For example:
enum Day { MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY };
const int NUM_DAYS = 5;
double sales[NUM_DAYS];
sales[MONDAY] = 1525.0;
sales[TUESDAY] = 1896.5;
sales[WEDNESDAY] = 1975.63;
sales[THURSDAY] = 1678.33;
sales[FRIDAY] = 1498.52;

Copyright © 2012 Pearson Education, Inc.


Enumerators Must Be Unique
Within the same Scope
• Enumerators must be unique within the same
scope. For example, an error will result if both
of the following enumerated types are
declared within the same scope:
enum Presidents { MCKINLEY, ROOSEVELT, TAFT };

enum VicePresidents { ROOSEVELT, FAIRBANKS,


SHERMAN };

ROOSEVELT is declared twice.


Copyright © 2012 Pearson Education, Inc.
Declaring the Type and Defining
the Variables in One Statement
• You can declare an enumerated data type
and define one or more variables of the
type in the same statement. For example:

enum Car { PORSCHE, FERRARI, JAGUAR } sportsCar;

This code declares the Car data type and defines a


variable named sportsCar.

Copyright © 2012 Pearson Education, Inc.

You might also like