12-4 unions

40
Computer Science: A Structured Programming Approach Using C 1 12-4 Unions The union is a construct that allows memory to The union is a construct that allows memory to be shared by different types of data. This be shared by different types of data. This redefinition can be as simple as redeclaring an redefinition can be as simple as redeclaring an integer as four characters or as complex as integer as four characters or as complex as redeclaring an entire structure. redeclaring an entire structure. Referencing Unions Initializers Unions and Structures Internet Addresses Topics discussed in this section: Topics discussed in this section:

Upload: jason

Post on 06-Jan-2016

38 views

Category:

Documents


0 download

DESCRIPTION

12-4 Unions. The union is a construct that allows memory to be shared by different types of data. This redefinition can be as simple as redeclaring an integer as four characters or as complex as redeclaring an entire structure. Topics discussed in this section:. Referencing Unions - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 1

12-4 Unions

The union is a construct that allows memory to be The union is a construct that allows memory to be shared by different types of data. This redefinition can shared by different types of data. This redefinition can be as simple as redeclaring an integer as four be as simple as redeclaring an integer as four characters or as complex as redeclaring an entire characters or as complex as redeclaring an entire structure.structure.

Referencing UnionsInitializersUnions and StructuresInternet Addresses

Topics discussed in this section:Topics discussed in this section:

Page 2: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 2

FIGURE 12-20 Unions

Page 3: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 3

PROGRAM 12-7 Demonstrate Effect of Union

Page 4: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 4

PROGRAM 12-7 Demonstrate Effect of Union

Page 5: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 5

FIGURE 12-21 A Name Union

Page 6: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 6

PROGRAM 12-8 Demonstrate Unions in Structures

Page 7: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 7

PROGRAM 12-8 Demonstrate Unions in Structures

Page 8: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 8

PROGRAM 12-8 Demonstrate Unions in Structures

Page 9: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 9

PROGRAM 12-9 Convert IP Address to long

Page 10: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 10

PROGRAM 12-9 Convert IP Address to long

Page 11: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 11

PROGRAM 12-9 Convert IP Address to long

Page 12: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 12

12-5 Programming Application

In this section, we develop a program that simulates In this section, we develop a program that simulates the operation of an elevator. The elevator serves floors the operation of an elevator. The elevator serves floors from zero (the basement) to the top floor. The elevator from zero (the basement) to the top floor. The elevator is very old and is not fully automatic. When people is very old and is not fully automatic. When people enter the elevator, they enter their desired floor enter the elevator, they enter their desired floor number. number.

Page 13: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 13

FIGURE 12-22 Elevator Structure

Page 14: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 14

FIGURE 12-23 Elevator Structure Chart

Page 15: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 15

FIGURE 12-24 Elevator State Diagram

Page 16: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 16

PROGRAM 12-10 Elevator: main

Page 17: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 17

PROGRAM 12-10 Elevator: main

Page 18: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 18

PROGRAM 12-11 Elevator: Initialize

Page 19: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 19

PROGRAM 12-12 Elevator: Run Elevator

Page 20: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 20

PROGRAM 12-12 Elevator: Run Elevator

Page 21: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 21

PROGRAM 12-12 Elevator: Run Elevator

Page 22: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 22

PROGRAM 12-13 Elevator: Move

Page 23: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 23

PROGRAM 12-13 Elevator: Move

Page 24: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 24

PROGRAM 12-13 Elevator: Move

Page 25: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 25

PROGRAM 12-14 Elevator: Move Up and Move Down

Page 26: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 26

PROGRAM 12-14 Elevator: Move Up and Move Down

Page 27: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 27

PROGRAM 12-14 Elevator: Move Up and Move Down

Page 28: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 28

PROGRAM 12-14 Elevator: Move Up and Move Down

Page 29: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 29

PROGRAM 12-15 Elevator: Any Up and Any Down Request

Page 30: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 30

PROGRAM 12-15 Elevator: Any Up and Any Down Request

Page 31: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 31

PROGRAM 12-16 Elevator: Time Pass

Page 32: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 32

PROGRAM 12-17 Elevator: Terminate

Page 33: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 33

12-6 Software Engineering

In this section we discuss two important aspects of In this section we discuss two important aspects of program design: function coupling and data hiding.program design: function coupling and data hiding.

CouplingData Hiding

Topics discussed in this section:Topics discussed in this section:

Page 34: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 34

Well-structured functions are highly cohesive and loosely coupled.

NoteNote

Page 35: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 35

Stamp coupling should pass only the data needed.

NoteNote

Page 36: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 36

Don’t bundle unrelated data to reduce the number of parameters.

NoteNote

Page 37: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 37

Control coupling should be used only to pass status.

NoteNote

Page 38: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 38

Avoid global coupling within a program.

NoteNote

Page 39: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 39

Never use content coupling.

NoteNote

Page 40: 12-4   Unions

Computer Science: A Structured Programming Approach Using C 40

Programming Standard:Do not place any variables in the global area of a program.

NoteNote