Lec17 Structs.

Slides:



Advertisements
Similar presentations
1 Programming Structures COMP102 Prog. Fundamentals, Structures / Slide 2 2 Structures l A Structure is a collection of related data items, possibly.
Advertisements

C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Structures Spring 2013Programming and Data Structure1.
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we.
1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
1 Lecture 24 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 11: Records (structs)
More Storage Structures A Data Type Defined by You Characteristics of a variable of a specific ‘data type’ has specific values or range of values that.
S-1 University of Washington Computer Programming I Lecture 18: Structures © 2000 UW CSE.
CIS Computer Programming Logic
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
Structured Data Types array array union union struct struct class class.
Edited from Powerpoint Slides provided by Thomson Learning
Week 9 - Monday.  What did we talk about last time?  Time  GDB.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Agenda Object Oriented Programming Reading: Chapter 14.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
11/5/2016CS150 Introduction to Computer Science 1 Announcements  Assignment 6 due on Wednesday, December 3, 2003  Final Exam on Tuesday, December 9,
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Arrays.
1 CS161 Introduction to Computer Science Topic #15.
Multiple Items in one Data Object Arrays are a way to store more than one piece of data in a data object, provided that all the data is of the same type.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
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.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
Chapter 6. Character String Types It is one in which the values consists of sequences of characters. How to Define a variable contain a string? In a programming.
LESSON 06.
Programming Logic and Design Seventh Edition
Exercise 1 – Datentypen & Variablen
chapter 11 Structured Types, Data Abstraction, and Classes
Structures and Unions.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Structures Cont. Dr. Xiao Qin Auburn.
Records C++ Structs Chapter 10.1.
CS150 Introduction to Computer Science 1
C++ Structs.
Records C++ Structs Chapter 12.
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Heterogeneous aggregate datatypes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Structs And Arrays.
Introduction to Abstract Data Types
Chapter 10: Records (structs)
Strings A collection of characters taken as a set:
C++ Data Types Data Type
CS148 Introduction to Programming II
Structured Data Types array union struct class.
ICT Programming Lesson 4:
EGR 2261 Unit 12 structs Read Malik, Chapter 9.
Structures and Union.
Objectives In this chapter, you will: - Learn about records (structs) - Examine operations on a struct - Manipulate data using a struct - Learn about the.
Instructor: Dr. Michael Geiger Spring 2019 Lecture 13: Exam 1 Preview
Programming Languages and Paradigms
Structure (i.e. struct) An structure creates a user defined data type
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
Structures Structured Data types Data abstraction structs ---
Structures, Unions, and Enumerations
Week 9 - Monday CS222.
Presentation transcript:

Lec17 Structs

Review of Data Types float: floating point numbers int : integer numbers char: single ASCII character string: a class representing a sequence of characters ifstream, ofstream: classes representing pathways to files

These are great but somewhat limited How would you represent an automobile in a computer? What about an ATM bank account? An employee entry in a database? And so on We need a type that goes beyond simple one type data. Enter…the struct!!

Structured Data Types A structured data type is one in which each value is a collection of components and whose organization is characterized by the method used to access individual components (i.e. the dot ‘.’ operator)

Structs A record or a struct in C++, is a structured data type with a fixed number of components that are accessed by name. The components may be heterogeneous (mixed types). A field or component is a piece of the record.

Syntax of struct definition struct TypeName { DataType MemberName; … };

This would go above main() Example: StudentRec struct StudentRec { string firstName; string lastName; float GPA; int ID; }; This would go above main()

Creating StudentRec variables Once you declare a struct StudentRec, it becomes a new data type you can use StudentRec Bill, Tamara; Bill Tamara firstName ID firstName ID lastName GPA lastName GPA

Accessing a field To assign a value to one of the fields, you use the member selector operator ‘.’ StudentRec Bill; // create a variable of type StudentRec Bill.firstName = “William”; Bill.lastName = “Borroughs”; Bill.ID=333; Bill.GPA=3.87; cout<<Bill.ID; cin>>Bill.lastName; Bill firstName ID William 333 GPA lastName 3.87 Borroughs

Your turn Declare a student record for a variable x Make x store the data for Kalina Noori, who’s ID is 456 and GPA is 3.97 Display the first and last names of x on the same line Input the ID and GPA of x x ID firstName lastName GPA

Aggregate Operations (performed on a struct as a whole) Assignment Arithmetic Comparison Passing Arguments Return from a function Allowed on Structs? No Yes Yes, value and reference Operations labelled ‘no’ can still be performed on a field by field basis (see next slide)

Example How to do the operations on StudentRec x, y; Assignment Arithmetic Comparison Passing to Fn Function return Correct Example cin>>x.firstName>>x.lastName >>x.ID>>x.GPA; (Wrong: cin>>x) y=x; (copies all fields) or x.ID=y.ID y.GPA=x.GPA+y.GPA; (Wrong: x=x+y) if (x.ID < y.ID) (Wrong: if (x<y)) Display(x); OR y.GPA=sqrt(x.GPA) return x; OR return x.GPA

Initializing Structs When a struct is created you can initialize it with a list of values (initialization list): StudentRec s={“Isa”, “Corwin”,3.5, 555}; The sequence must match the order of declaration in the struct (see slide 7): NO: StudentRec s={“Isa”,“Corwin”,555,3.5};OOPS You can only do this on declaring StudentRec t; NO: t={“Bert”,“Lance”,3.5, 555}; ERROR

Data Abstraction Data abstraction is the separation of a data’s logical properties from its implementation, for example: Logical Properties of a real number: addition, multiplication, positive, negative Implementation of a real number: Hardware, limited bits, mantissa, exponent how these can be used to provide adding, multiplication An Abstract Data Type is a data type whose properties are specified independently of any particular implementation. How we represent real things on a computer This leads us to Classes (2 weeks!)

TRY IT NOW!! Prepare for Project 2, Part 2 Practice basic concepts of structs by completing Assignment 11, problems 1-7