Chapter 7 A Data Types – Structures. 7-2 7.13 Structures Structure: C++ construct that allows multiple variables to be grouped together Structure Declaration.

Slides:



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

Lesson 11 Structured Data CS1 Lesson John Cole1.
Starting Out with C++: Early Objects 5th Edition
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 8 Arrays.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 7: Introduction to Classes and Objects
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Chapter 11 – Structured Data
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.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 10: Records ( struct s)
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 11. The Struct Data Type.
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.
Arrays Chapter 8.
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.
Structured Data Chapter 11. Combining Data Into Structures Structure: C++ construct that allows multiple variables to be grouped together Format: struct.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Chapter 7: Arrays. Outline Array Definition Access Array Array Initialization Array Processing 2D Array.
Structured Data and Classes
1 Introduction to C++ Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
Chapter 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
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.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
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++
Chapter 7: Introduction to Classes and Objects
11 Chapter Structured Data
Abstract Data Types Programmer-created data types that specify
Chapter 7: Introduction to Classes and Objects
Chapter 11: Structured Data.
Chapter 7: Introduction to Classes and Objects
Chapter 7: Introduction to Classes and Objects
Student Book An Introduction
Data Types – Structures
Data Types – Structures
Heterogeneous aggregate datatypes
Chapter 11: Structured Data.
7 Arrays.
Arrays Arrays A few types Structures of related data items
Standard Version of Starting Out with C++, 4th Edition
Structure (i.e. struct) An structure creates a user defined data type
Corresponds with Chapter 5
Programming Fundamental
Presentation transcript:

Chapter 7 A Data Types – Structures

Structures Structure: C++ construct that allows multiple variables to be grouped together Structure Declaration Format: struct structure name { type1 field1; type2 field2; … typen fieldn; };

7-3 Example struct Declaration struct Student { int studentID; string name; short year; double gpa; } ; structure tag structure members Notice the required ;

7-4 struct Declaration Notes struct names commonly begin with an uppercase letter The structure name is also called the tag struct name is a data type struct declaration does not allocate memory or create variables.

7-5 Defining Structured Variables To define variables, use structure name as the data type Student S1; Use field selector operator (. ) to reference a member variable studentID name year gpa S1 studentIDnameyeargpa.... S1. year

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

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;

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

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; };

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

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

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

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

7-14 Partial Initialization Can initialize just some members, but cannot skip over members Dimensions box1 = {12,6}; //OK Dimensions box2 = {12,,3}; //illegal

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

7-16 Using a Constructor to Initialize Structure Members A special function: – 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

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;} }; Applying Constructor  Dimensions box3(12, 6, 3); Dimensions Trunk(66,54,3);

7-18 Nested Structures A structure can have another structure as a member. struct PersonalInfo { string name, address, city; }; struct Student { int studentID; PersonalInfo persData; short year; double gpa; };

7-19 Nested Structures struct PersonalInf { string name, address, city; }; struct Student { int studentID; PersonalInfo pData; short year; double gpa; }; S1 studentIDpDatayeargpa.... name address City.... S1.pData.name S1.gpa

7-20 Members of Nested Structures Use the dot operator multiple times to access fields of nested structures. Student You; You.studentID = ; You.persData.name = "Joanne"; You.persData.city = "Tulsa";

7-21 Structures as Function Arguments Can pass members of struct variable to functions computeGPA(s1.gpa); Can pass entire struct variable in function call: Show_Data(You); Use reference parameter if function needs to modify contents of structure variable void Get_Data(Student & s);

7-22 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);

7-23 Returning a Structure from a Function Function can return a struct Student getStudData(); // prototype s1 = getStuData(); // call Function must define a local structure variable – for internal use – to use with return statement

7-24 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; }

7-25 Unions Similar to a struct, but – all members share a single memory location, which saves space – only 1 member of the union can be used at a time Declared using key word union Otherwise the same as struct Variables defined and accessed like struct variables

7-26 Example union Declaration union WageInfo { double hourlyRate; float annualSalary; }; union tag Notice the required ; union members