Structure (i.e. struct) An structure creates a user defined data type

Slides:



Advertisements
Similar presentations
Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
Advertisements

Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
Chapter 9 Data Structures: Arrays and Structs Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
Multiple-Subscripted Array
Chapter 11: Structured Data. Slide Introduction An array makes it possible to access a list or table of data of the same data type by using a single.
Pointer Data Type and Pointer Variables. Objectives: Pointer Data Type and Pointer Variables Pointer Declaration Pointer Operators Initializing Pointer.
Parameter Passing Mechanisms Reference Parameters Read § §
Parameter Passing Mechanisms Reference Parameters § §
1 Chapter 11 Structured Data. 2 Topics 10.1 Abstract Data Types 10.2 Combining Data into Structures 10.3 Accessing Structure Members 10.4 Initializing.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Structured Data Chapter 11. Combining Data Into Structures Structure: C++ construct that allows multiple variables to be grouped together Format: struct.
Chapter 7 A Data Types – Structures Structures Structure: C++ construct that allows multiple variables to be grouped together Structure Declaration.
Structure TDate struct TDate { int year, month, day; }; // Define a new data type.
CPS120: Introduction to Computer Science Lecture 15A Structures.
Class Student class Student { private: string id; string firstName, lastName; float gpa; public: void Read() void Write() string getGPA() void setGPA(
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
CPS120: Introduction to Computer Science Data Structures.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Class Method Read class Student { private: string id; string firstName, lastName; float gpa; public: // Will the method change any data members? // Yes!
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7. 12: Structures Starting Out with C++ Early Objects Eighth.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
Nested Structures struct TDate { int year, month, day; }; struct StudentType { string id, firstName, lastName; float gpa; TDate DOB; }; struct SectionType.
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
The C++ Data Types Fundamental Data Types
Pointers and Dynamic Arrays
Chapter 7 Pointers and C-Strings
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Chapter 11: Structured Data.
Pointers and Pointer-Based Strings
Student Book An Introduction
8 Pointers.
Data Types – Structures
DATA HANDLING.
CS 1430: Programming in C++.
Student Data Score First Name Last Name ID GPA DOB Phone ...
Pointers and References
C Passing arrays to a Function
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Data Types – Structures
Heterogeneous aggregate datatypes
Pass by Reference, const, readonly, struct
Chapter 10: Records (structs)
Introduction to Programming
More About Data Types & Functions
Chapter 11: Structured Data.
Pointers Kingdom of Saudi Arabia
Pointers Lecture 2 Tue, Jan 24, 2006.
Introduction to Programming
5.1 Introduction Pointers Powerful, but difficult to master
CS111 Computer Programming
Chapter 10 Structures and Unions
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
CPS120: Introduction to Computer Science
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Pointers and Pointer-Based Strings
Data Structures and Algorithms Introduction to Pointers
Arrays An array is a grouping of elements of the same type that share a common base name Can have any number of elements in the array Individual elements.
Pointers and dynamic objects
Pointers and References
CSCE 206 Lab Structured Programming in C
Chapter 10 C Structures and Unions
Programming Fundamental
Programming Fundamental
Presentation transcript:

Structure (i.e. struct) An structure creates a user defined data type Contains a grouping of related data elements of arbitrary type and number A “container” of other data elements Individual components of the structure are called its members Three parts to defining and using a structure declaration of structure use of structure to define a new variable access of members of the structured variable

Structure declaration (generic) Declare a new structure using the keyword struct Provide a name for the new data type List the members and their individual types struct stype { type1 member1; type2 member2; . . . typeN memberN; };

Structure declaration (example) struct student { string firstName; string lastName; int age; float GPA; char gender; int class; }; Creates a new data type called student Members include: Strings to store first and last name Integer for student’s age and class Float for student’s grade point average Char for student’s gender

Structure usage Variable declaration Example: Use the newly defined data type to create a variable as you would any other, built-in, data type Access individual data members using the dot (.) operator variable.member Use this as you would any other variable of the given type Example: student S1; // variable declaration! S1.firstName= “John”; S1.lastName= “Doe”; S1.age= 20; S1.gender= ‘m’; … // happy birthday! S1.age+= 1;

structs as Operands and Arguments struct copy or assignment student S2= S1; // copy S1 to S2 Copies each member from S1 struct to S2 struct

Storage Structure member elements are stored in sequential memory locations Student S1 Memory Address Member Value 0x1xxx firstName* John 0x1yyy lastName* Doe 0x1100 age 20 0x1104 GPA 4.0 0x1108 gender** m 0x1109 class 2 * size of a string ** may actually use more than one byte

Passing a struct as a function argument Argument must be of the same struct type as in the function definition/prototype Using structs as arguments can shorten the argument list Passing structs by value can be inefficient, since it duplicates values of all members “pass by value” copies the entire structure

Passing struct by Reference Same as any other reference parameter use & to identify in parameter list void printStudent(student &s) struct members can be modified Can also use constant reference precede parameter with reserved word const E.g. void printStudent(const student &s) const prevents the function from changing any member in the structure

Function printStudent() void printStudent(const student &s) { cout << “Student Information: " << endl; cout << " Name : " << s.firstName << “ “ << s.lastName << endl; cout << " Age : " << s.age << endl; cout << " Gender : " << s.gender << endl; cout << " Class : " << s.class << endl; cout << " GPA : " << s.GPA << endl; } 0x1104

Array of Structs . Declaration of an array of structs firstName John lastName Doe age 20 GPA 4.0 gender m class 2 Declaration of an array of structs student S[10]; Storage for 10 student structs Can pass to a function printStudent(S[i]); Storage is in sequential memory locations S[0] firstName Mary lastName Doe age 21 GPA 4.0 gender f class 3 S[1] .