Lec17 Structs - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Lec17 Structs

Description:

ifstream, ofstream: classes representing pathways to files. 3 ... You can only do this on declaring. StudentRec t; NO: t={'Bert','Lance',3.5, 555}; ERROR 14 ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 16
Provided by: solo50
Category:
Tags: bert | lec17 | structs

less

Transcript and Presenter's Notes

Title: Lec17 Structs


1
Lec17 Structs
2
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

3
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.
  • Enterthe struct!!

4
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)

5
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.

6
Syntax of struct definition
  • struct TypeName
  • DataType MemberName
  • DataType MemberName

7
Example StudentRec
  • struct StudentRec
  • string firstName
  • string lastName
  • float GPA
  • int ID

This would go above main()
8
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
ID
firstName
GPA
lastName
GPA
lastName
9
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.ID333
  • Bill.GPA3.87
  • coutltltBill.ID
  • cingtgtBill.lastName

Bill
ID
firstName
William
333
GPA
lastName
3.87
Borroughs
10
Your turn
  • Declare a student record for a variable x
  • Make x store the data for Kalina Noori, whos 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
GPA
lastName
11
Aggregate Operations (performed on a struct as a
whole)
  • Aggregate Operation
  • I/O
  • Assignment
  • Arithmetic
  • Comparison
  • Passing Arguments
  • Return from a function
  • Allowed on Structs?
  • No
  • Yes
  • No
  • No
  • Yes, value and reference
  • Yes

Operations labelled no can still be performed
on a field by field basis (see next slide)
12
Example How to do the operationson StudentRec x,
y
  • Operation
  • I/O
  • Assignment
  • Arithmetic
  • Comparison
  • Passing to Fn
  • Function return
  • Correct Example
  • cingtgtx.firstNamegtgtx.lastName
  • gtgtx.IDgtgtx.GPA (Wrong cingtgtx)
  • yx (copies all fields) or x.IDy.ID
  • y.GPAx.GPAy.GPA (Wrong xxy)
  • if (x.ID lt y.ID) (Wrong if (xlty))
  • Display(x) OR y.GPAsqrt(x.GPA)
  • return x OR return x.GPA

13
Initializing Structs
  • When a struct is created you can initialize it
    with a list of values (initialization
    list)StudentRec sIsa, Corwin,3.5, 555
  • The sequence must match the order of declaration
    in the
  • struct (see slide 7)
  • NO StudentRec sIsa,Corwin,555,3.5?OOPS
  • You can only do this on declaring
  • StudentRec t
  • NO tBert,Lance,3.5, 555 ?ERROR

14
Data Abstraction
  • Data abstraction is the separation of a datas
    logical properties from its implementation, for
    exampleLogical 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!)

15
TRY IT NOW!!
  • Prepare for Project 2, Part 2
  • Practice basic concepts of structs by completing
    Assignment 11, problems 1-7
Write a Comment
User Comments (0)
About PowerShow.com