0% found this document useful (0 votes)
10 views25 pages

Lecture 5

The chapter discusses structs (also called records) in C++, which allow grouping together different data types under one name. A struct defines the members but does not allocate memory, which occurs when struct variables are declared. Members are accessed using the dot operator, and structs can be assigned, passed to functions, and returned from functions. Arrays of structs are also covered, allowing multiple struct elements to be defined and accessed.

Uploaded by

Zarak Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views25 pages

Lecture 5

The chapter discusses structs (also called records) in C++, which allow grouping together different data types under one name. A struct defines the members but does not allocate memory, which occurs when struct variables are declared. Members are accessed using the dot operator, and structs can be assigned, passed to functions, and returned from functions. Arrays of structs are also covered, allowing multiple struct elements to be defined and accessed.

Uploaded by

Zarak Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

C++ Programming:

From Problem Analysis


to Program Design, Fourth Edition

Chapter 11: Records (structs)

1
Objectives
In this chapter, you will:
• Learn about records (structs)
• Examine various operations on a struct
• Explore ways to manipulate data using a
struct
• Learn about the relationship between a struct
and functions
• Discover how arrays are used in a struct
• Learn how to create an array of struct items

2
Records (structs)
• struct: collection of a fixed number of
components (members), accessed by name
• A struct is a definition, not a declaration
− Members may be of different types
• Syntax:

3
4
Accessing struct Members

• The syntax for accessing a struct


member is:

• The dot (.) is an operator, called the member


access operator

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

6
Accessing struct Members
(continued)
− More examples:

cin >> newStudent.firstName;


cin>>newStudent.testScore>>newStudent.programmingScore;
score = (newStudent.testScore +
newStudent.programmingScore) / 2;
if (score >= 90)
newStudent.courseGrade = 'A';
else if (score >= 80)
newStudent.courseGrade = 'B';
else if (score >= 70)
newStudent.courseGrade = 'C';
else if (score >= 60)
newStudent.courseGrade = 'D';
else
newStudent.courseGrade = 'F';
7
Example:

8
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

9
Assignment (continued)

• 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;
10
Example:

11
Comparison (Relational Operators)

• Compare struct variables member-wise


− No aggregate relational operations allowed
• To compare the values of student and
newStudent:

12
Input/Output

• No aggregate input/output operations on a


struct variable
• Data in a struct variable must be read one
member at a time
• The contents of a struct variable must be
written one member at a time

13
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

14
Arrays versus structs

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

16
17
Arrays in structs (cont'd.)

- 18
18
19
Prepared by: Malak Abdullah---- 20
structs within a struct

versus

21
22
Prepared by: Malak Abdullah---- 23
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

24
Summary (continued)

• 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
• struct can be passed by value or reference
• A function can return a value of type struct
• structs can be members of other structs
25

You might also like