C Programming From Problem Analysis to Program Design

C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 11: Records (structs) C++ Programming: From Problem Analysis to Program Design, Third Edition

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

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

C++ Programming: From Problem Analysis to Program Design, Third Edition 6

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

C++ Programming: From Problem Analysis to Program Design, Third Edition 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 = new. Student; copies the contents of new. Student into student C++ Programming: From Problem Analysis to Program Design, Third Edition 9

Assignment (continued) The assignment statement: student = new. Student; is equivalent to the following statements: student. first. Name = new. Student. first. Name; student. last. Name = new. Student. last. Name; student. course. Grade = new. Student. course. Grade; student. test. Score = new. Student. test. Score; student. programming. Score = new. Student. programming. Score; student. GPA = new. Student. GPA; C++ Programming: From Problem Analysis to Program Design, Third Edition 10

Comparison (Relational Operators) • Compare struct variables member-wise • To compare the values of student and new. Student: 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 << << << new. Student. first. Name " " << new. Student. last. Name " " << new. Student. course. Grade " " << new. Student. test. Score " " << new. Student. programming. Score " " << new. Student. GPA << endl; C++ Programming: From Problem Analysis to Program Design, Third Edition 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 C++ Programming: From Problem Analysis to Program Design, Third Edition 14

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

Example 1: #include <iostream> #include <string> using namespace std; void main() { struct student. Record{ string name; int number; int marks[5]; }; student. Record class. A[2]; // defining struct for student // student name as a c-string // student number // 5 courses marks class. A[0]. name="Ali"; // if the student number is larger then you should use another data type class. A[0]. number=1234567; class. A[0]. marks[0]=97; class. A[0]. marks[1]=88; class. A[0]. marks[2]=83; class. A[0]. marks[3]=90; class. A[0]. marks[4]=78; C++ Programming: From Problem Analysis to Program Design, Third Edition 16
![Example 1: (continue) class. A[1]. name="Hala"; class. A[1]. number=3456789; class. A[1]. marks[0]=88; class. A[1]. Example 1: (continue) class. A[1]. name="Hala"; class. A[1]. number=3456789; class. A[1]. marks[0]=88; class. A[1].](http://slidetodoc.com/presentation_image_h2/9198af6871f49efe1cc72f61214a353f/image-17.jpg)
Example 1: (continue) class. A[1]. name="Hala"; class. A[1]. number=3456789; class. A[1]. marks[0]=88; class. A[1]. marks[1]=79; class. A[1]. marks[2]=76; class. A[1]. marks[3]=92; class. A[1]. marks[4]=83; //printing the second students marks for(int i=0; i<5; i++) cout << class. A[1]. marks[i] << " "; } C++ Programming: From Problem Analysis to Program Design, Third Edition 17

Example 2: C++ Programming: From Problem Analysis to Program Design, Third Edition 18

Example 2: (continue) C++ Programming: From Problem Analysis to Program Design, Third Edition 19

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
- Slides: 22