CPPF 1511
CPPF 1511
*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis*
COMPUTER PROGRAMMING
LECTURE 11
STRUCTURED DATA
IMRAN IHSAN
ASSISTANT PROFESSOR
AIR UNIVERSITY, ISLAMABAD
• 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
• General Format:
struct <structName>
{
type1 field1;
type2 field2;
. . .
};
structure tag
struct Student
{
int studentID; structure members
string name;
short yearInSchool;
double gpa;
};
DEFINING VARIABLES
Student bill;
bill
studentID
name
yearInSchool
gpa
• Member variables can be used in any manner appropriate for their data type
10
• To display the contents of a struct variable, must display each field separately, using the dot
operator:
11
12
• Cannot initialize in the structure declaration, since this does not allocate memory
13
INITIALIZING A STRUCTURE
PROGRAM
14
15
ARRAYS OF STRUCTURES
16
17
ARRAYS OF STRUCTURES
18
• Use the dot operator multiple times to refer to fields of nested structures:
Student s;
s.pData.name = "Joanne";
s.pData.city = "Tulsa";
19
• Can use reference parameter if function needs to modify contents of structure variable
20
21
• 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
22
23
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;
}
24
25
26
27
POINTERS TO STRUCTURES
• Pointers to structures are variables that can hold the address of a structure:
Student *stuPtr;
28
• Can use structure pointer operator to eliminate () and use clearer notation:
cout << stuPtr->studentID;
29
30
Anonymous Union
• A union without a union tag:
union { ... };
31
• Example:
• Note that the enumerators are not strings, so they aren’t enclosed in quotes.
They are identifiers.
32
• Once you have created an enumerated data type in your program, you can define variables of
that type. Example:
Day workDay;
• We may assign any of the enumerators MONDAY, TUESDAY, WEDNESDAY, THURSDAY, or FRIDAY
to a variable of the Day type. Example:
workDay = WEDNESDAY;
33
In memory...
MONDAY = 0
TUESDAY = 1
WEDNESDAY = 2
THURSDAY = 3
FRIDAY = 4
34
• 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);
int x;
x = THURSDAY;
35
• 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.“
36
37
38
39
• An anonymous enumerated type is simply one that does not have a name. For example, in
Program 11-13 we could have declared the enumerated type as:
40
• You can run into problems when trying to perform math operations with enum
variables. For example:
• The third statement will not work because the expression day1 + 1 results in the
integer value 2, and you cannot store an int in an enum variable.
• You can fix this by using a cast to explicitly convert the result to Day, as shown here:
41
• Because enumerators are stored in memory as integers, you can use them as array
subscripts. For example:
42
• Remember, though, you cannot use the ++ operator on an enum variable. So, the following
loop will NOT work.
43
• You must rewrite the loop’s update expression using a cast instead of ++:
44
45
46
• 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:
47
• You can declare an enumerated data type and define one or more variables of the type in the
same statement. For example:
• This code declares the Car data type and defines a variable named sportsCar.
48