Docsity

C++ Programming: Structures and Abstract Data Types, Diapositivas de Artes Escénicas y Danza

Vista previa parcial del texto

¡Descarga C++ Programming: Structures and Abstract Data Types y más Diapositivas en PDF de Artes Escénicas y Danza solo en Docsity!

Structure & Abstract Data

Types

By Prof. J. V. Joshua, PhD.

Structure

Records (structs) (cont’d.)

  • A struct is a definition, not a declaration
    • Must declare a variable of that type to use it 4 C++ Programming: From Problem Analysis to Program

Records (structs) (cont’d.)

5 C++ Programming: From Problem Analysis to Program

Accessing struct Members (cont’d.)

  • To initialize the members of newStudent: newStudent.GPA = 0.0; newStudent.firstName = "John"; newStudent.lastName = "Brown"; 7 C++ Programming: From Problem Analysis to Program Figure: struct newStudent

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 8 C++ Programming: From Problem Analysis to Program

Comparison (Relational Operators)

  • Compare struct variables member-wise
    • No aggregate relational operations allowed
  • To compare the values of student and newStudent: 10 C++ Programming: From Problem Analysis to Program

Input/Output

  • No aggregate input/output operations on a struct variable
  • Data in a struct variable must be read or written one member at a time
  • Example: output newStudent contents 11 C++ Programming: From Problem Analysis to Program

Arrays in structs (cont’d.)

13 C++ Programming: From Problem Analysis to Program Figure: struct variable intList

Arrays in structs (cont’d.)

14 C++ Programming: From Problem Analysis to Program

structs in Arrays (cont’d.)

16 C++ Programming: From Problem Analysis to Program

structs within a struct

17 C++ Programming: From Problem Analysis to Program

Summary

  • Dot (.) operator: member access operator
    • Used to access members of a struct
  • The only built-in operations on a struct are the assignment

and member access

  • Neither arithmetic nor relational operations are allowed on

structs

  • A struct can be passed by value or reference
  • A function can return a value of type struct
  • structs can be members of other structs 19 C++ Programming: From Problem Analysis to Program

Exercise

  • Write a statement that declares a structure named Movie. The structure should have fields to hold the following data about a movie. - The name of the movie - The director of the movie - The producer of the movie - The year the movie was released