Download presentation
1
Chapter 11: Records (structs)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 11: Records (structs)
2
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
3
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
4
Records (structs) (continued)
The general syntax of a struct is: C++ Programming: From Problem Analysis to Program Design, Third Edition
7
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
9
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
10
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;
11
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
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 C++ Programming: From Problem Analysis to Program Design, Third Edition
13
Input/Output (continued)
cout << newStudent.firstName << " " << newStudent.lastName << " " << newStudent.courseGrade << " " << newStudent.testScore << " " << newStudent.programmingScore << " " << newStudent.GPA << endl;
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, Third Edition
16
Example 1: #include <iostream> #include <string>
using namespace std; void main() { struct studentRecord{ // defining struct for student string name; // student name as a c-string int number; // student number int marks[5]; // 5 courses marks }; studentRecord classA[2]; classA[0].name="Ali"; // if the student number is larger then you should use another data type classA[0].number= ; 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
17
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
18
Example 2:
19
Example 2: (continue)
20
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
21
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
22
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.