Sow C++ Cso Chapter 11 10e
Sow C++ Cso Chapter 11 10e
Structured Data
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
11.1
Abstract Data Types
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Abstract Data Types
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Abstraction and Data Types
• Abstraction: a definition that captures
general characteristics without details
• Ex: An abstract triangle is a 3-sided polygon.
A specific triangle may be scalene, isosceles,
or equilateral
• Data Type defines the values that can be
stored in a variable and the operations that
can be performed on it
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
11.2
Combining Data into Structures
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Combining Data into Structures
• Structure: C++ construct that allows multiple
variables to be grouped together
• General Format:
struct <structName>
{
type1 field1;
type2 field2;
. . .
};
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Example struct Declaration
struct Student structure tag
{
int studentID;
string name; structure
short yearInSchool; members
double gpa;
};
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
struct Declaration Notes
• Must have ; after closing }
• struct names commonly begin with
uppercase letter
• Multiple fields of same type can be in
comma-separated list:
string name,
address;
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Defining Variables
• struct declaration does not allocate
memory or create variables
• To define variables, use structure tag as
type name:
bill
Student bill; studentID
name
yearInSchool
gpa
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
11.3
Accessing Structure Members
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Accessing Structure Members
• Use the dot (.) operator to refer to members of
struct variables:
cin >> stu1.studentID;
getline(cin, stu1.name);
stu1.gpa = 3.75;
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Displaying a struct Variable
• To display the contents of a struct
variable, must display each field
separately, using the dot operator:
cout << bill; // won’t work
cout << bill.studentID << endl;
cout << bill.name << endl;
cout << bill.yearInSchool;
cout << " " << bill.gpa;
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Comparing struct Variables
• Cannot compare struct variables
directly:
if (bill == william) // won’t work
william.studentID) ...
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
11.4
Initializing a Structure
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Initializing a Structure
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
More on Initializing a Structure
• May initialize only some members:
Student bill = {14579};
• Cannot skip over members:
Student s = {1234, "John", ,
2.83}; // illegal
• Cannot initialize in the structure
declaration, since this does not allocate
memory
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Excerpts From Program 11-3
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
11.5
Arrays of Structures
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Arrays of Structures
• Structures can be defined in arrays
• Can be used in place of parallel arrays
const int NUM_STUDENTS = 20;
Student stuList[NUM_STUDENTS];
• Individual structures accessible using subscript
notation
• Fields within structures accessible using dot
notation:
cout << stuList[5].studentID;
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
11.6
Nested Structures
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Nested Structures
A structure can contain another structure as a
member:
struct PersonInfo
{ string name,
address,
city;
};
struct Student
{ int studentID;
PersonInfo pData;
short yearInSchool;
double gpa;
};
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Members of Nested Structures
• Use the dot operator multiple times to refer
to fields of nested structures:
Student s;
s.pData.name = "Joanne";
s.pData.city = "Tulsa";
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
11.7
Structures as Function Arguments
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Structures as Function
Arguments
• May pass members of struct variables to
functions:
computeGPA(stu.gpa);
• May pass entire struct variables to functions:
showData(stu);
• Can use reference parameter if function needs
to modify contents of structure variable
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Excerpts from Program 11-6
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Structures as Function
Arguments - Notes
• Using value parameter for structure can
slow down a program, waste space
• Using a reference parameter will speed
up program, but function may change
data in structure
• Using a const reference parameter
allows read-only access to reference
parameter, does not waste space, speed
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Revised showItem Function
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
11.8
Returning a Structure from a
Function
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Returning a Structure from a
Function
• Function can return a struct:
Student getStudentData(); // prototype
stu1 = getStudentData(); // call
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Returning a Structure from a
Function - Example
Student getStudentData()
{ Student tempStu;
cin >> tempStu.studentID;
getline(cin, tempStu.pData.name);
getline(cin, tempStu.pData.address);
getline(cin, tempStu.pData.city);
cin >> tempStu.yearInSchool;
cin >> tempStu.gpa;
return tempStu;
}
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
11.9
Using Structured Binding
Declarations with Structures
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Structured Binding Declarations
• A structured binding declaration defines a set of
variables and initializes them with the values
that are stored in a structure.
• A process known as unpacking a structure
• Can also be used to unpack arrays (see Chapter 7)
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Structured Binding Declarations
• General format:
auto [variable1, variable2, etc...] = structureVar;
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Structured Binding Declarations
• Example:
struct Automobile
{
string make;
int year;
double mileage;
};
Automobile car = { "Porsche", 2020, 12400.0 };
auto [first, second, third] = car;
This
This code
code will
will display:
display:
cout << first << endl; Porsche
Porsche
cout << second << endl; 2020
2020
cout << third << endl; 12400.0
12400.0
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
11.10
Pointers to Structures
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
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
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Accessing Structure Members
via Pointer Variables
• Must use () to dereference pointer
variable, not field within structure:
cout << (*stuPtr).studentID;
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
From Program 11-9
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
11.12
Enumerated Data Types
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Enumerated Data Types
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Enumerated Data Types
• Example:
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Enumerated Data Types
• Once you have created an enumerated
data type in your program, you can define
variables of that type. Example:
Day workDay;
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
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 © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
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 © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Enumerated Data Types
enum Day { MONDAY, TUESDAY,
WEDNESDAY, THURSDAY,
FRIDAY };
In memory...
MONDAY =0
TUESDAY =1
WEDNESDAY =2
THURSDAY =3
FRIDAY =4
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Enumerated Data Types
• Using the Day declaration, the following
code...
cout << MONDAY << " "
<< WEDNESDAY << " “
<< FRIDAY << endl;
0 2 4
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
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 © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Assigning an Enumerator to an int
Variable
• You CAN assign an enumerator to an int
variable. For example:
int x;
x = THURSDAY;
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Comparing Enumerator Values
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Anonymous Enumerated Types
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Using Math Operators with enum
Variables
• You can run into problems when trying to perform math
operations with enum variables. For example:
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Using Math Operators with enum
Variables
• You can fix this by using a cast to explicitly
convert the result to Day, as shown here:
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
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 © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Using an enum Variable to Step
through an Array's Elements
• Remember, though, you cannot use the ++
operator on an enum variable. So, the
following loop will NOT work.
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Using an enum Variable to Step
through an Array's Elements
• You must rewrite the loop’s update
expression using a cast instead of ++:
for (workDay = MONDAY; workDay <= FRIDAY;
workDay = static_cast<Day>(workDay + 1))
{
cout << "Enter the sales for day "
<< workDay << ": ";
cin >> sales[workDay];
}
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Continued…
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Enumerators Must Be Unique
Within the same Scope
• Enumerators must be unique within the same scope.
(Unless strongly typed)
• For example, an error will result if both of the following
enumerated types are declared within the same scope:
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Strongly Typed enums
• In C++ 11 and later, you can use a new type of enum ,
known as a strongly typed enum
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Strongly Typed enums
• Prefix the enumerator with the name of the enum ,
followed by the :: operator:
Presidents prez = Presidents::ROOSEVELT;
VicePresidents vp = VicePresidents::ROOSEVELT;
int x = static_cast<int>(Presidents::ROOSEVELT);
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
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:
Copyright © 2021, 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.