Transcript CH11 Slides

C++ Programming:
From Problem Analysis
to Program Design, Third Edition
Chapter 11: Records (structs)
Objectives
In this chapter you will:
• Learn about records (structs)
• Examine various operations on a struct
• Explore ways to manipulate data using a
struct
C++ Programming: From Problem Analysis to Program Design, Third Edition
2
Records (structs)
• struct: collection of a fixed number of
components, accessed by name
• Components may be of different types
• Components of a struct are called the
members of the struct
• struct is a reserved word
C++ Programming: From Problem Analysis to Program Design, Third Edition
3
Records (structs) (continued)
• The general syntax of a struct is:
C++ Programming: From Problem Analysis to Program Design, Third Edition
4
Accessing struct Members
• The syntax for accessing a struct
member is:
• The dot (.) is an operator, called the member
access operator
C++ Programming: From Problem Analysis to Program Design, Third Edition
7
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, Third Edition
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;
Comparison (Relational Operators)
• Compare struct variables member-wise
• To compare the values of student and
newStudent:
C++ Programming: From Problem Analysis to Program Design, Third Edition
11
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
C++ Programming: From Problem Analysis to Program Design, Third Edition
12
Input/Output (continued)
cout <<
<<
<<
<<
<<
<<
newStudent.firstName
" " << newStudent.lastName
" " << newStudent.courseGrade
" " << newStudent.testScore
" " << newStudent.programmingScore
" " << newStudent.GPA << endl;
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, Third Edition
14
Example 1:
#include <iostream>
#include <string>
using namespace std;
void main()
{
struct studentRecord{
string name;
int number;
int marks[5];
};
studentRecord
classA[2];
// defining struct for student
// student name as a c-string
// student number
// 5 courses marks
classA[0].name="Ali";
// if the student number is larger then you should use another data type
classA[0].number=1234567;
classA[0].marks[0]=97;
classA[0].marks[1]=88;
classA[0].marks[2]=83;
classA[0].marks[3]=90;
classA[0].marks[4]=78;
C++ Programming: From Problem Analysis to Program Design, Third Edition
16
Example 1: (continue)
classA[1].name="Hala";
classA[1].number=3456789;
classA[1].marks[0]=88;
classA[1].marks[1]=79;
classA[1].marks[2]=76;
classA[1].marks[3]=92;
classA[1].marks[4]=83;
//printing the second students marks
for(int i=0;i<5; i++) cout << classA[1].marks[i] << " ";
}
C++ Programming: From Problem Analysis to Program Design, Third Edition
17
Example 2:
Example 2: (continue)
Summary
• struct: collection of a fixed number of
components
• Components can be of different types
• struct is a reserved word
• No memory is allocated for a struct; memory
is allocated for struct variables when
declared
• Components of a struct are called members
C++ Programming: From Problem Analysis to Program Design, Third Edition
20
Summary (continued)
• struct components are accessed by name
• Dot (.) operator is called the member access
operator
• Members of a struct are accessed using
the dot (.) operator
• The only built-in operations on a struct are
the assignment and member access
C++ Programming: From Problem Analysis to Program Design, Third Edition
21
Summary (continued)
• 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
C++ Programming: From Problem Analysis to Program Design, Third Edition
22