Learning Objectives Structures Structure types

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Copyright © 2002 Pearson Education, Inc. Slide 1.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
1 Programming Structures COMP102 Prog. Fundamentals, Structures / Slide 2 2 Structures l A Structure is a collection of related data items, possibly.
Structure.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide Overview 10.1 Structures 10.2 Classes 10.3 Abstract Data Types.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Structures.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
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.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
1 Using Structures and Classes COSC 1557 C++ Programming Lecture 4.
Learners Support Publications Classes and Objects.
Week 9 - Monday.  What did we talk about last time?  Time  GDB.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
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.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
CS 261 – Data Structures Introduction to C Programming.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 13: Data structures in C.
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
+ 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.
CPS120: Introduction to Computer Science Lecture 15A Structures.
CPS120: Introduction to Computer Science Data Structures.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
© Janice Regan, CMPT 128, Feb CMPT 128: Introduction to Computing Science for Engineering Students Structures.
STRUCTURES. C structures: aggregate, yet scalar  aggregate in that they hold multiple data items at one time  named members hold data items of various.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Structures  Structure types  Pointers and structure types  Structures.
Structures and Classes
Learning Objectives Pointers as dada members
Programmer Defined Types and Classes
What header file contains C++ file I/O instructions?
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 10-1: Structure.
Pointers, Enum, and Structures
Introduction to the C programming language
What’s New? Six homework programming assignments. New: five
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Structures Cont. Dr. Xiao Qin Auburn.
Introduction to Classes
Structures A Structure is a collection of related data items, possibly of different types. A structure type in C++ is called struct. A struct is heterogeneous.
Heterogeneous aggregate datatypes
Structures putting data together.
C Structures, Unions, Bit Manipulations and Enumerations
Chapter 9: Records (structs)
Chapter 9: Records (structs)
Chapter 10: Records (structs)
Chapter 9: Records (structs)
Arrays and Arrays as Parameters
Chapter 11: Structured Data.
Pointer to Structures Lesson xx
CS148 Introduction to Programming II
Structures putting data together.
Classes and Objects.
CS111 Computer Programming
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
Objectives In this chapter, you will: - Learn about records (structs) - Examine operations on a struct - Manipulate data using a struct - Learn about the.
Chapter 11: Records (structs)
Class and Objects In a class, all the functions that operate on the data structure are grouped together in one place along with the data Like a struct.
Java Programming Language
Standard Version of Starting Out with C++, 4th Edition
EECE.2160 ECE Application Programming
Chapter 9: Records (structs)
Structures, Unions, and Enumerations
Week 9 - Monday CS222.
Presentation transcript:

Learning Objectives Structures Structure types Pointers and structure types Structures as function arguments

Structures Aggregate data type: struct Aggregate meaning "grouping" Recall array: collection of values of same type Structure: collection of values of different types Treated as a single item, like arrays Major difference: Must first "define" struct Prior to declaring any variables

Structure Types Define struct globally (typically) No memory is allocated Just a "placeholder" for what our struct will "look like" Definition: struct CDAccount Name of new struct "type" { double balance;  member names double interestRate; int term; };

Declare Structure Variable With structure type defined, now declare variables of this new type: CDAccount account_1; Just like declaring simple types Variable account now of type CDAccount It contains "member values" Each of the struct "parts"

Accessing Structure Members Dot Operator to access members account_1.balance account_1.interestRate account_1.term Called "member variables" The "parts" of the structure variable Different structs can have same name member variables No conflicts

Structure Pitfall Semicolon after structure definition ; MUST exist: struct WeatherData { double temperature; double windVelocity; };  REQUIRED semicolon!

Structure Assignments Simple assignments are legal between instances of same structure: apples = oranges; Simply copies each member variable from apples into member variables from oranges Sometime confusing, not recommend to use Assign field by field, more accurate and clear apple.price = oragnge.price; apple.producer = orange.produce;

Initializing Structures Can initialize at declaration Example: struct Date { int month; int day; int year; }; Date dueDate = {12, 31, 2003}; Declaration provides initial data to all three member variables Not commonly used

Initializing Structures (continued) Initialize after declaration (more commenly used) struct Date { int month; int day; int year; }; Date dueDate; dueDate.month =12; dueDate.day = 31; Duedate.year = 2003;

Using Pointers with Struct The -> operator Shorthand notation Combines dereference operator, *, and dot operator Specifies member of class "pointed to" by given pointer

Using Pointers with Struct (continued) struct Date { int month; int day; int year; }; Date dueDate; Date * P = &dueDate P->month =12; P->day = 31; P->year = 2003;

Using Pointers with Struct (continued) struct Date { int month; int day; int year; }; Date * P = new Date; P->month =12; P->day = 31; P->year = 2003;

Structures as Function Arguments Passed like any simple data type Pass-by-value Pass-by-reference Pass-by-pointer Can also be returned by function Return-type is structure type Return statement in function definition sends structure variable back to caller