Unit 6-03

Learning Outcome

At the end of this lesson, you will be able to:

  • understand and use a Struct

Review

  • Enumeration

Lesson

  • go over Representing and Sorting Data, Chapter 6
    • Computer Based Problem Solving
    • "Abstract Data Types"
  • go over "Stack and Heap"
    • read this and this
    • this is why we use Structs and not Objects all the time
  • therefore:
  • - Local value types are allocated on the stack.
  • This includes integers such as "int i" for loops.
  • When you create an object from a class in a function, it is allocated on the heap.
  • The stack is normally much faster.
  • go over ADT example from below

Activity

# Created by: Mr. Coxall
# Created on: Nov 2017
# Created for: ICS3U
# This program is an example of a structure

class Student():
    def __init__(self, first_name, last_name, grade):
        self.first_name = first_name
        self.last_name = last_name
        self.grade = grade

a_single_student = Student('Patrick', 'Coxall', 13)
print (a_single_student.first_name + ' ' + str(a_single_student.grade))


Daily Assignment

  • create a program like the one below but creates a structure that will hold a mailing address
    • see Canada Post Civic Address, to figure out what elements are needed
    • make all text fields strings for now
    • No design, just a console program
    • you must create the address structure and the use it to hold 1 address
    • for now, just save the postal code in a string. We will fix this soon!

Extra

  • Do the program below in PHP
// Struct example

struct Student {
    var firstName: String
    var lastName: String
    var middleInitial: Character
    var grade: Int
}

let fName = ask("First name")
let lName = ask("Last name")
let mInitial = Character(ask("Middle initial"))
let grade : Int = Int(ask("Grade"))!

let aSingleStudent = Student(firstName: fName, lastName: lName, middleInitial: mInitial, grade: grade)

show(aSingleStudent.firstName)
show(aSingleStudent.grade)

Homework!

  • read over Representing and Sorting Data, Chapter 6
    • Computer Based Problem Solving
    • "Enumerated Types inside ADT"