Chapter 11: Records (structs)

Slides:



Advertisements
Similar presentations
Chapter 2: Basic Elements of C++
Advertisements

Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
1 Chapter 11 Introducing the Class Pages ( )
Structure.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 10: Records ( structs )
Chapter 12: Classes and Data Abstraction
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 11: Records (structs)
Chapter 9: Arrays and Strings
EGR 2261 Unit 10 Records (structs)
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 10: Records (structs)
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
Pointer Data Type and Pointer Variables II By: Nouf Aljaffan Edited by : Nouf Almunyif.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 11: Records ( struct s)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 10: Records ( struct s)
Chapter 10: Records (structs)
Edited from Powerpoint Slides provided by Thomson Learning
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
C++ Review (3) Structs, Classes, Data Abstraction.
1 Lecture 19 Structs HW 5 has been posted. 2 C++ structs l Syntax:Example: l Think of a struct as a way to combine heterogeneous data values together.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Structures (aka records, structs) Textbook Chapter 11 sections:
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 11: Records ( struct s)
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
LESSON 06.
REVISION CSC 138 – Structured Programming One Dimensional Array
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
MIS 120 Structures.
Records C++ Structs Chapter 10.1.
Pointer Data Type and Pointer Variables II
C++ Structs.
Records C++ Structs Chapter 12.
Pointer Data Type and Pointer Variables
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Heterogeneous aggregate datatypes
Structs And Arrays.
Chapter 9: Records (structs)
Chapter 9: Records (structs)
Chapter 10: Records (structs)
Chapter 9: Records (structs)
Review for Final Exam.
Pointers & Functions.
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
Objectives In this chapter, you will: - Learn about records (structs) - Examine operations on a struct - Manipulate data using a struct - Learn about the.
Chapter 7: User-Defined Functions II
Review for Final Exam.
Chapter 11: Records (structs)
Pointers & Functions.
Pointer Data Type and Pointer Variables
Pointer Data Type and Pointer Variables
Chapter 12: Classes and Data Abstraction
Chapter 9: Records (structs)
Presentation transcript:

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