[go: up one dir, main page]

100% found this document useful (1 vote)
64 views29 pages

Records (Structs)

This chapter discusses structs (or records) in C++. A struct is a collection of variables of different types that are grouped together under a single name. Structs allow programmers to define new data types that bundle multiple related data items. The chapter covers defining structs, accessing struct members, passing structs to functions, and nesting structs within other structs or arrays.

Uploaded by

mohamed hemdan
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
100% found this document useful (1 vote)
64 views29 pages

Records (Structs)

This chapter discusses structs (or records) in C++. A struct is a collection of variables of different types that are grouped together under a single name. Structs allow programmers to define new data types that bundle multiple related data items. The chapter covers defining structs, accessing struct members, passing structs to functions, and nesting structs within other structs or arrays.

Uploaded by

mohamed hemdan
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/ 29

Chapter 9:

Records (structs)
Objectives
In this chapter, you will:
• Learn about records (structs)
• Examine operations on a struct
• Manipulate data using a struct
• Learn about the relationship between a
struct and functions
• Discover how arrays are used in a struct
• Create an array of struct items
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 2
Records (structs)
• struct: collection of a fixed number of
components (members), accessed by name
– Members may be of different types
• Syntax:

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 3


Records (structs) (cont’d.)
• A struct is a definition, not a declaration
– Must declare a variable of that type to use it

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 4


Records (structs) (cont’d.)

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 5


C++ Programming: From Problem Analysis to Program Design, Sixth Edition 6
Accessing struct Members
• Syntax to access a struct member:

• The dot (.) is called the member access


operator

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 7


Accessing struct Members (cont’d.)
• To initialize the members of newStudent:
newStudent.GPA = 0.0;
newStudent.firstName = "John";
newStudent.lastName = "Brown";

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 8


C++ Programming: From Problem Analysis to Program Design, Sixth Edition 9
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 10
Assignment
• Value of one struct variable can be
assigned to another struct variable of the
same type using an assignment statement
• The statement:
student = newStudent;
copies the contents of newStudent into
student

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 11


Assignment (cont’d.)
• The assignment statement:
student = newStudent;

is equivalent to the following statements:


student.firstName = newStudent.firstName;
student.lastName = newStudent.lastName;
student.courseGrade = newStudent.courseGrade;
student.testScore = newStudent.testScore;
student.programmingScore = newStudent.programmingScore;
student.GPA = newStudent.GPA;

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 12


Comparison (Relational Operators)
• Compare struct variables member-wise
– No aggregate relational operations allowed
• To compare the values of student and
newStudent:

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 13


Input/Output
• No aggregate input/output operations on a
struct variable
• Data in a struct variable must be read or
written one member at a time
• Example: output newStudent contents

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 14


struct Variables and Functions
• A struct variable can be passed as a
parameter by value or by reference
• A function can return a value of type struct

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 15


Arrays in structs
• Two items are associated with a list:
– Values (elements)
– Length of the list
• Define a struct containing both items:

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 16


Arrays in structs (cont’d.)

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 17


Arrays in structs (cont’d.)

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 18


C++ Programming: From Problem Analysis to Program Design, Sixth Edition 19
structs in Arrays

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 20


structs in Arrays (cont’d.)

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 21


C++ Programming: From Problem Analysis to Program Design, Sixth Edition 22
C++ Programming: From Problem Analysis to Program Design, Sixth Edition 23
structs within a struct

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 24


C++ Programming: From Problem Analysis to Program Design, Sixth Edition 25
structs within a struct

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 26


C++ Programming: From Problem Analysis to Program Design, Sixth Edition 27
Summary
• struct: collection of a fixed number of
components
• Components can be of different types
– Called members
– Accessed by name
• struct is a reserved word
• No memory is allocated for a struct
– Memory when variables are declared

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 28


Summary (cont’d.)
• Dot (.) operator: member access operator
– Used to access members of a struct
• The only built-in operations on a struct are the
assignment and member access
• Neither arithmetic nor relational operations are
allowed on structs
• A struct can be passed by value or reference
• A function can return a value of type struct
• structs can be members of other structs

C++ Programming: From Problem Analysis to Program Design, Sixth Edition 29

You might also like