Chapter 11: Records (structs) 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
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
Records (structs) (continued) The general syntax of a struct is: C++ Programming: From Problem Analysis to Program Design, Third Edition
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
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
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
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
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
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=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
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
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
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
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