Chapter 7: Introduction to Classes and Objects

Slides:



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

1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Starting Out with C++: Early Objects 5th Edition
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.
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.
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 © 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.
Chapter 7 A Data Types – Structures Structures Structure: C++ construct that allows multiple variables to be grouped together Structure Declaration.
Structured Data and Classes
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
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.
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.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++
Chapter 12 Classes and Abstraction
Chapter 7: Introduction to Classes and Objects
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Chapter 2: Introduction to C++
11 Chapter Structured Data
Chapter 7: Introduction to Classes and Objects
Abstract Data Types Programmer-created data types that specify
Chapter 8: Arrays Starting Out with C++ Early Objects Ninth Edition
Chapter 10: Pointers Starting Out with C++ Early Objects
Chapter 3: Using Methods, Classes, and 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
MIS 120 Structures.
Records C++ Structs Chapter 10.1.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Chapter 10: Pointers Starting Out with C++ Early Objects
C++ Structs.
Data Types – Structures
Chapter 2: Introduction to C++
Chapter 8: Arrays Starting Out with C++ Early Objects Eighth Edition
Records C++ Structs Chapter 12.
Data Types – Structures
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Classes and Data Abstraction
Chapter 10: Records (structs)
Chapter 11: Structured Data.
Classes and Objects.
Chapter 3: Expressions and Interactivity
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Standard Version of Starting Out with C++, 4th Edition
Programming Fundamental
Presentation transcript:

Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early Objects Ninth Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda

Topics 7.12 Structures

Structures Recall that elements of arrays must all be of the same type In some situations, we wish to group elements of different types scores : 85 79 92 57 68 80 . . . 0 1 2 3 4 5 98 99 employee R. Jones 123 Elm 6/12/55 14.75

7.12 Structures Structure: A programmer-defined data type that allows multiple variables to be grouped together Structure declaration format: struct structure name { type1 field1; type2 field2; … typen fieldn; };

Example struct Declaration struct StudentStruct { int studentID; string name; short year; double gpa; }; structure name structure members Note the required ;

struct Declaration Notes struct names commonly begin with an uppercase letter, because they are a type Multiple fields of same type can be declared in a comma-separated list string name,address; Fields in a structure are all public by default

Defining Structure Variables A struct declaration does not allocate memory or create variables, because they are a type To define variables, use structure tag as the type name StudentStruct s1; studentID name year gpa s1

Accessing Structure Members Use the name of the struct variable the name of the member separated by a dot . getline(cin, s1.name); cin >> s1.studentID; s1.gpa = 3.75; The dot is called the member selector see pr7-13.cpp

Aggregate Operations with Structures Recall that arrays had no whole array operations (except passing reference to parameter) Structures DO have aggregate operators assignment statement = parameter (value or reference) return a structure as a function type

Aggregate Operations with Structures Limitations on aggregate operations no I/O no arithmetic operations no comparisons cout << old_part; cin >> new_part; old_part = new_part + old_part; if (old_part < new_part) cout << ...;

Aggregate Operations with Structures struct variables must be initialized, compared, written one member at a time.

Displaying struct Members To display the contents of a struct variable, you must display each field or member 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;

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

Initializing a Structure Structure members cannot be initialized 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; };

Initializing a Structure (continued) Structure members are initialized at the time a structure variable is created You can initialize a structure variable’s members with either an initialization list a constructor

Initialization List Example Structure Declaration Structure Variable struct Dimensions { int length, width, height; }; Dimensions box = {12,6,h}; box length 12 width 6 height 3

Using a Constructor to Initialize Structure Members This is similar to a constructor for a class: the name is the same as the name of the struct it has no return type it is used to initialize data members It is normally written inside the struct declaration

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

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;

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"; Reference the nested structure’s fields by the member variable name, not by the structure name s5.PersonInfo.name = "Joanne"; //no! See pr7-14.cpp

Structures as Function Arguments You may pass members of struct variables to functions computeGPA(s1.gpa); You may pass entire struct variables to functions by value or by reference void CopyPart (PartStruct partOrig, PartStruct & partNew); See pr7-15.cpp

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 execution, but it 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 StudentStruct &s) // header

Returning a Structure from a Function A function can return a struct StudentStruct getStuData(); // prototype s1 = getStuData(); // call The function must define a local structure variable for internal use to use with return statement

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; } See pr7-15B.cpp

Contrast Arrays and structs

typedef PartStruct PartsArray [50]; Arrays of structs First declare a struct (such as PartStruct) Then specify an array of that type Access elements of the array, elements of the struct typedef PartStruct PartsArray [50]; How do we print all the descrip fields? for (x = 0; x <50; x++) cout << _______________________; parts [x].descript;

structs with Arrays Example const ARRAYSIZE = 1000; struct LISTTYPE { int list [ARRAYSIZE]; //array containing the list int listLength; //length of the list }

Hierarchical structures Defn => structs where at least one of the components is, itself, a struct Example: struct InventoryStruct { PartStruct part; int qty_sold, re_order_qty; VendorStruct vendor; };

Choosing Data Structures Strive to group logical elements of a structure together calls for hierarchical structures Push details of entities down to lower levels of the structure Data Abstraction <=> separation of logical peoperties of a data type from its implementation

Testing and Debugging Hints Declaration of a struct type must end with a semicolon ; Be sure to specify the full member selector when referencing a component of a struct variable don’t leave out the struct name

Testing and Debugging When using an array in a struct, the index goes at the end studentRecord.scores[x] When using an array of struct, the index goes after the struct name parts[x].qty

Testing and Debugging Process struct members separately … the only aggregate operations will be Assignment = Parameter passing void do_it (PartStruct part); Function return PartStruct ProcessThings ( );

Testing and Debugging Be careful using same member names in different struct types Compiler keeps them separate OK Human readers can easily confuse them struct PartStruct { int qty; . . . } ; struct ScoresStruct { int qty; . . . } ;`

Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early Objects Ninth Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda