11 Structures
11 Structures
Outline
Structures
2
Structures
Outline
1 Introduction
2 Structure Definitions
3 Accessing Structure Members
4 Implementing a User-Defined Type Time with a struct
5 Implementing a Time Abstract Data Type with a class
3
1 Introduction
2 Structure Definitions
• Structures
– Aggregate data types built using elements of other types
2 Structure Definitions
• Self-referential structure
– Structure member cannot be instance of enclosing struct
– Structure member can be pointer to instance of enclosing
struct (self-referential structure)
• Used for linked lists, queues, stacks and trees
• struct definition
– Creates new data type used to declare variables
– Structure variables declared like variables of other types
– Examples:
• Time timeObject;
• Time timeArray[ 10 ];
• Time *timePtr;
• Time &timeRef = timeObject;
6
OR
timePtr = &timeObject;
cout << timePtr->hour;
– timePtr->hour same as ( *timePtr ).hour
• Parentheses required
– * lower precedence than .
7
4 Implementing a User-Defined Type Time with a
struct
• Default: structures passed by value
– Pass structure by reference
• Avoid overhead of copying structure
• C-style structures
– No “interface”
• If implementation changes, all programs using that struct
must change accordingly
– Cannot print as unit
• Must print/format member by member
– Cannot compare in entirety
• Must compare member by member
8
1 // Fig. 1: fig06_01.cpp
2 // Create a structure, set its members, and print it.
Outline
3 #include <iostream>
4
5 using std::cout;
fig06_01.cpp
6 using std::endl; (1 of 3)
7
8 #include <iomanip>
9
10 using std::setfill;
11 using std::setw;
Define structure type Time
12 with three integer members.
13 // structure definition
14 struct Time {
15 int hour; // 0-23 (24-hour clock format)
16 int minute; // 0-59
17 int second; // 0-59
18 Pass references to constant
19 }; // end struct Time Time objects to eliminate
20
copying overhead.
21 void printUniversal( const Time & ); // prototype
22 void printStandard( const Time & ); // prototype
23
9
24 int main()
25 { Use dot operator to initialize Outline
26 Time dinnerTime; // structure
variable members.
of new type Time
27
28 dinnerTime.hour = 18; // set hour member of dinnerTime
fig06_01.cpp
29 dinnerTime.minute = 30; // set minute member of dinnerTime (2 of 3)
30 dinnerTime.second = 0; // set second member of dinnerTime
31
32 cout << "Dinner will be held at ";
33 printUniversal( dinnerTime );
34 cout << " universal time,\nwhich is ";
35 printStandard( dinnerTime );
36 cout << " standard time.\n"; Direct access to data allows
37 assignment of bad values.
38 dinnerTime.hour = 29; // set hour to invalid value
39 dinnerTime.minute = 73; // set minute to invalid value
40
41 cout << "\nTime with invalid values: ";
42 printUniversal( dinnerTime );
43 cout << endl;
44
45 return 0;
46
47 } // end main
48
10
49 // print time in universal-time format
50 void printUniversal( const Time &t )
Outline
51 {
52 cout << setfill( '0' ) << setw( 2 ) << t.hour << ":"
53 << setw( 2 ) << t.minute << ":"
fig06_01.cpp
54 << setw( 2 ) << t.second; (3 of 3)
55
56 } // end function printUniversal Use parameterizedfig06_01.cpp
stream
57 manipulator setfill.
output (1 of 1)
58 // print time in standard-time format
59 void printStandard( const Time &t )
Use dot operator to access
60 { data members.
61 cout << ( ( t.hour == 0 || t.hour == 12 ) ?
62 12 : t.hour % 12 ) << ":" << setfill( '0' )
63 << setw( 2 ) << t.minute << ":"
64 << setw( 2 ) << t.second
65 << ( t.hour < 12 ? " AM" : " PM" );
66
67 } // end function printStandard
int main()
{
part part1; //define a structure variable
return 0;
}
12
#include <iostream>
EX2: Combining Declaration and Definition using namespace std;
int main()
{
part1.modelnumber = 6244; //give values to
structure members
part1.partnumber = 373;
part1.cost = 217.55F;
//display structure members
cout << "Model " << part1.modelnumber;
cout << ", part " << part1.partnumber;
cout << ", costs $" << part1.cost << endl;
return 0;
}
13
#include <iostream>
int main()
{
part part1 = { 6244, 373, 217.55F };
part part2; //define variable
//display first variable
cout << "Model " << part1.modelnumber;
cout << ", part " << part1.partnumber;
cout << ", costs $" << part1.cost << endl;
return 0;
}
14
//Example4:
#include <iostream>
EX4: demonstrates structures using
using namespace std;
};
int main()
{
Distance d1, d3; //define two lengths
Distance d2 = { 11, 6.25 }; //define & initialize one length
return 0;
}
15
#include <iostream>
using namespace std;
EX5: Initializing nested structures
////////////////////////////////////////////////////////////////
struct Distance //English distance
{
int feet;
float inches;
};
////////////////////////////////////////////////////////////////
struct Room //rectangular area
{
Distance length; //length of rectangle
Distance width; //width of rectangle
};
////////////////////////////////////////////////////////////////
int main()
{
Room dining = {{13,6.5},{10,0.0}}; //define a
room
return 0;
16
#include <iostream>
using namespace std;
EX6: Initializing nested structures
////////////////////////////////////////////////////////////////
struct Distance //English distance
{
int feet;
float inches;
};
////////////////////////////////////////////////////////////////
struct Room //rectangular area
{
Distance length; //length of rectangle
Distance width; //width of rectangle
};
////////////////////////////////////////////////////////////////
int main()
{
Room dining = {{13,6.5},{10,0.0}}; //define a
room
return 0;
17
Arrays in structs
structs in Arrays
• Example:
21
#include <stdio.h>
int main()
{
enum Days{Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday};
Days theDay;
int j = 0;
printf("Please day of the week (0 to 6)\n"); cin >> j;
theDay = Days(j);
int main()
{
days_of_week day1, day2; //define variables of
type days_of_week
day1 = Mon; //give values to
day2 = Thu; //variables
return 0;
}
26
#include <iostream>
#include <conio.h>
using namespace std;
return 0;
}