Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7. 12: Structures Starting Out with C++ Early Objects Eighth.

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Advertisements

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Arrays Starting Out with C++ Early Objects Seventh Edition by.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
Starting Out with C++: Early Objects 5th Edition
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 6: Functions by.
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
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
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:
Chapter 6: Functions Starting Out with C++ Early Objects
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9: Pointers.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Arrays Starting Out with C++ Early Objects Seventh Edition by.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Seventh Edition.
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 © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C-Strings, Arrays, and String Class Review.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Arrays Starting Out with C++ Early Objects Seventh Edition by.
Structured Data and Classes
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Eighth Edition.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 8: Arrays by Tony.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Arrays Starting Out with C++ Early Objects Seventh Edition by.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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.
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.
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++
Chapter 7: Introduction to Classes and Objects
11 Chapter Structured Data
Chapter 7: Introduction to Classes and Objects
Abstract Data Types Programmer-created data types that specify
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 7: Introduction to Classes and Objects
Chapter 7: Introduction to Classes and Objects
Chapter 10: Pointers Starting Out with C++ Early Objects
Data Types – Structures
Data Types – Structures
Chapter 11: Structured Data.
Standard Version of Starting Out with C++, 4th Edition
Programming Fundamental
Presentation transcript:

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7. 12: Structures Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Structures Structure: Programmer-defined data type that allows multiple variables to be grouped together Structure Declaration Format: struct structure name { type1 field1; type2 field2; … typen fieldn; };

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7-3 Example struct Declaration struct Student { int studentID; string name; short year; double gpa; } ; structure name structure members Notice the required ;

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7-4 struct Declaration Notes struct names commonly begin with an uppercase letter The structure name is also called the tag Multiple fields of same type can be in a comma-separated list string name, address; Fields in a structure are all public by default

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7-5 Defining Structure Variables struct declaration does not allocate memory or create variables To define variables, use structure tag as type name Student s1; studentID name year gpa s1

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7-6 Accessing Structure Members Use the dot (.) operator to refer to members of struct variables getline(cin, s1.name); cin >> s1.studentID; s1.gpa = 3.75; Member variables can be used in any manner appropriate for their data type

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7-7 Displaying struct Members To display the contents of a struct variable, you must display each field separately, using the dot operator Wrong: cout << s1; // won’t work! Correct: cout << s1.studentID << endl; cout << s1.name << endl; cout << s1.year << endl; cout << s1.gpa;

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7-8 Comparing struct Members Similar to displaying a struct, you cannot compare two struct variables directly: if (s1 >= s2) // won’t work! Instead, compare member variables: if (s1.gpa >= s2.gpa) // better

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7-9 Initializing a Structure Cannot initialize members in the structure declaration, because no memory has been allocated yet struct Student // Illegal { // initialization int studentID = 1145; string name = "Alex"; short year = 1; float gpa = 2.95; };

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7-10 Initializing a Structure (continued) Structure members are initialized at the time a structure variable is created Can initialize a structure variable’s members with either –an initialization list –a constructor

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7-11 Using an Initialization List An initialization list is an ordered set of values, separated by commas and contained in { }, that provides initial values for a set of data members {12, 6, 3} // initialization list // with 3 values

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7-12 More on Initialization Lists Order of list elements matters: First value initializes first data member, second value initializes second data member, etc. Elements of an initialization list can be constants, variables, or expressions {12, W, L/W + 1} // initialization list // with 3 items

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7-13 Initialization List Example Structure Declaration Structure Variable struct Dimensions { int length, width, height; }; Dimensions box = {12,6,3}; box length12 width 6 height3

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7-14 Partial Initialization Can initialize just some members, but cannot skip over members Dimensions box1 = {12,6}; //OK Dimensions box2 = {12,,3}; //illegal

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7-15 Problems with Initialization List Can’t omit a value for a member without omitting values for all following members Does not work on most modern compilers if the structure contains any string objects –Will, however, work with C-string members

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7-16 Using a Constructor to Initialize Structure Members Similar to a constructor for a class: –name is the same as the name of the struct –no return type –used to initialize data members It is normally written inside the struct declaration

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7-17 A Structure with a Constructor struct Dimensions { int length, width, height; // Constructor Dimensions(int L, int W, int H) {length = L; width = W; height = H;} };

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7-18 Nested Structures A structure can have another structure as a member. struct PersonInfo { string name, address, city; }; struct Student { int studentID; PersonInfo pData; short year; double gpa; };

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7-19 Members of Nested Structures Use the dot operator multiple times to access fields of nested structures Student s5; s5.pData.name = "Joanne"; s5.pData.city = "Tulsa";

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7-20 Structures as Function Arguments May pass members of struct variables to functions computeGPA(s1.gpa); May pass entire struct variables to functions showData(s5); Can use reference parameter if function needs to modify contents of structure variable

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7-21 Notes on Passing Structures Using a value parameter for structure can slow down a program and waste space Using a reference parameter speeds up program, but allows the function to modify data in the structure To save space and time, while protecting structure data that should not be changed, use a const reference parameter void showData(const Student &s) // header

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7-22 Returning a Structure from a Function Function can return a struct Student getStuData(); // prototype s1 = getStuData(); // call Function must define a local structure variable –for internal use –to use with return statement

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7-23 Returning a Structure Example Student getStuData() { Student s; // local variable cin >> s.studentID; cin.ignore(); getline(cin, s.pData.name); getline(cin, s.pData.address); getline(cin, s.pData.city); cin >> s.year; cin >> s.gpa; return s; }

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7.12: Structures Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda