Structured Data Chapter 11. Combining Data Into Structures Structure: C++ construct that allows multiple variables to be grouped together Format: struct.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Advertisements

Lesson 11 Structured Data CS1 Lesson John Cole1.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 10: Pointers by.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Starting Out with C++: Early Objects 5th Edition
Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 10: Pointers.
Chapter 7: Introduction to Classes and Objects
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 10 Structured Data.
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.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified by use by the MSU CMPS Dept. Chapter 10:
C++ Pointers Copies from SEE C++ programming course and from Starting Out with C++: Early Objects, 8/E by Tony Gaddis, Judy Walters and Godfrey Muganda.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 10: Records ( struct s)
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
Programmer Defined Structures (Records)
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 7: Introduction.
Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified.
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 and Classes Chapter 7. Combining Data into Structures Structure: C++ construct that allows multiple variables to be grouped together Structure.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Chapter 7 A Data Types – Structures Structures Structure: C++ construct that allows multiple variables to be grouped together Structure Declaration.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
Review 1 List Data Structure List operations List Implementation Array Linked List.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
Structured Data and Classes
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
C Programming Structured Data.
1 1  Lecture 11 – Structured Data FTMK, UTeM – Sem /2014.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7. 12: Structures Starting Out with C++ Early Objects Eighth.
Chapter Structured Data 11. Combining Data into Structures 11.2.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Chapter 7: Introduction to Classes and Objects
Standard Version of Starting Out with C++, 4th Edition
11 Chapter Structured Data
Chapter 7: Introduction to Classes and Objects
Chapter 10: Pointers Starting Out with C++ Early Objects
Chapter 11: Structured Data.
Chapter 7: Introduction to Classes and Objects
Chapter 10: Pointers Starting Out with C++ Early Objects
Data Types – Structures
Data Types – Structures
Heterogeneous aggregate datatypes
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Chapter 11: Structured Data.
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Standard Version of Starting Out with C++, 4th Edition
Standard Version of Starting Out with C++, 4th Edition
Programming Fundamental
Presentation transcript:

Structured Data Chapter 11

Combining Data Into Structures Structure: C++ construct that allows multiple variables to be grouped together Format: struct { Type1 field1; Type2 field2;... };

Example struct Declaration struct Student { int studentID; string name; short yearInSchool; float gpa; }; structure tag structure members

struct Declaration Notes Must have ; after closing } struct names commonly begin with uppercase letter Multiple fields of same type can be in comma-separated list: string name, address;

Defining Variables struct declaration does not allocate memory or create variables To define variables, use structure tag as type name: Student stu1; studentID name yearInSchool gpa stu1

Accessing Structure Members Use the dot (.) operator to refer to members of struct variables: cin >> stu1.studentID; getline(cin, stu1.name); stu1.gpa = 3.75; Member variables can be used in any manner appropriate for their data type

Displaying a struct Variable To display the contents of a struct variable, must display each field separately, using the dot operator: cout << stu1; // won’t work cout << stu1.studentID << endl; cout << stu1.name << endl; cout << stu1.yearInSchool; cout << " " << stu1.gpa;

Comparing struct Variables Cannot compare struct variables directly: if (stu1 == stu2) // won’t work Instead, must compare on a field basis: if (stu1.studentID == stu2.studentID)...

Initializing A Structure struct variable can be initialized when defined: Student stu2 = (11465, "Joan", 2, 3.75); Can also be initialized member-by- member after definition: stu2.name = "Joan"; stu2.gpa = 3.75;

More On Initializing A Structure May initialize only some members: Student stu3 = (14579); Cannot skip over members: Student stu4 = (1234, "John",, 2.83); // illegal Cannot initialize in the structure declaration, since this does not allocate memory

Arrays Of Structures Structures can be defined in arrays Can be used in place of parallel arrays Student stuList[20]; Individual structures accessible using subscript notation Fields within structures accessible using dot notation: cout << stuList[5].studentID;

Nested Structures A structure can contain another structure as a member: struct PersonInfo { string name, address, city; }; struct Student {int studentID; PersonInfo pData; short yearInSchool; float gpa; };

Members Of Nested Structures Use the dot operator multiple times to refer to fields of nested structures: Student stu5; stu5.pData.name = "Joanne"; stu5.pData.city = "Tulsa";

Structures As Function Arguments May pass members of struct variables to functions: computeGPA(stu1.gpa); May pass entire struct variables to functions: showData(stu5); Can use reference parameter if function needs to modify contents of structure variable

Structures As Function Arguments - Notes Using value parameter for structure can slow down a program, waste space Using a reference parameter will speed up program, but function may change data in structure Using a const reference parameter allows read-only access to reference parameter, does not waste space, speed

Returning A Structure From A Function Function can return a struct : Student getStuData(); // prototype stu1 = getStuData(); // call Function must define a local structure –for internal use –for use with return statement

Returning A Structure From A Function - Example Student getStuData() { Student tmpStu; cin >> tmpStu.studentID; getline(cin, tmpStu.pData.name); getline(cin, tmpStu.pData.address); getline(cin, tmpStu.pData.city); cin >> tmpStu.yearInSchool; cin >> tmpStu.gpa; return tmpStu; }

Pointers To Structures A structure variable has an address Pointers to structures are variables that can hold the address of a structure: Student *stuPtr; Can use & operator to assign address: stuPtr = & stu1; Structure pointer can be a function parameter

Accessing Structure Members Via Pointer Variables Must use () to dereference pointer variable, not field within structure: cout << (*stuPtr).studentID; Can use structure pointer operator to eliminate () and use clearer notation: cout studentID;

When To Use., When To Use ->, And When To Use * Consider the following: struct Xmpl { int *intPtr; }; Xmpl xmpl1, *xmplPtr = &xmpl1; *xmpl1.intPtr accesses the value pointed at by intPtr *xmplPtr->intPtr does the same thing