CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.

Slides:



Advertisements
Similar presentations
Python Objects and Classes
Advertisements

OBJECT ORIENTED PROGRAMMING (OOP) IN PYTHON David Moodie.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
Computer Science 111 Fundamentals of Programming I Introduction to Programmer-Defined Classes.
Guide to Programming with Python
REFERENCES: CHAPTER 8 Object-Oriented Programming (OOP) in Python.
Python 3 Some material adapted from Upenn cis391 slides and other sources.
1 CSC 221: Introduction to Programming Fall 2012 course overview  What did you set out to learn?  What did you actually learn?  Where do you go from.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Centre for Computer Technology ICT115 Object Oriented Design and Programming Week 2 Intro to Classes Richard Salomon and Umesh Patel Centre for Information.
CIT 590 Intro to Programming Style Classes. Remember to finish up findAllCISCourses.py.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Chapter 12 Object Oriented Design.  Complements top-down design  Data-centered view of design  Reliable  Cost-effective.
1 Programming Paradigms Object Orientated Programming Paradigm (OOP)
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
Object-Oriented Programming (OOP) in Python References: Chapter 8.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Object Oriented Programming Some Interesting Genes.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
CMSC201 Computer Science I for Majors Lecture 18 – Classes and Modules (Continued, Part 3) Prof. Katherine Gibson Based on slides from the.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2012 Marek Janicki.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
CSC 108H: Introduction to Computer Programming
CMSC201 Computer Science I for Majors Lecture 25 – Classes
Object Oriented Programming
OOP: Encapsulation &Abstraction
Java Language Basics.
Python 3 Some material adapted from Upenn cis391 slides and other sources.
Object Oriented Programming in Python: Defining Classes
COMPSCI 107 Computer Science Fundamentals
Object-Oriented Programming (OOP) in Python
Objects as a programming concept
Introduction to Python
Behavioral Design Patterns
CSC 221: Computer Programming I Fall 2005
CS-104 Final Exam Review Victor Norman.
Copyright (c) 2017 by Dr. E. Horvath
Topic: Functions – Part 2
CSC 108H: Introduction to Computer Programming
Computer Science 111 Fundamentals of Programming I
CSC 108H: Introduction to Computer Programming
Functions CIS 40 – Introduction to Programming in Python
Functions.
CSC 108H: Introduction to Computer Programming
CSC 108H: Introduction to Computer Programming
CSC 108H: Introduction to Computer Programming
Engineering Computing
Creating and Deleting Instances Access to Attributes and Methods
Procedural Abstraction Object-Oriented Code
To Get Started Paper sheet
Teach A-Level Computer Science: Object-Oriented Programming in Python
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Loops CIS 40 – Introduction to Programming in Python
An Introduction to Object Orientated Programming
Java Inheritance.
Computer Science 111 Fundamentals of Programming I
Object-Oriented Programming
Inheritance and Polymorphism
A Level Computer Science Topic 6: Introducing OOP
Object-Oriented Design AND CLASS PROPERTIES
Classes and Methods 15-Aug-19.
Presentation transcript:

CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki

July Administration ● Class is in BA1140 Next week. ● Midterms will be given out in the second break. ● The mean was a ● Median was 31. ● Assignment 1 autotesting results have been mailed to your cdf s. ● I am aware that Assignment 2 was too difficult, and the grading will reflect that. ● Assignment 3 comes out tomorrow or on the weekend.

July The Structure of Programming. ● At the most basic level there is Machine Code. ● We don't do this. ● Then we have lines of code in a programming language. ● But often times we use the same code. ● Then we have functions on top of that. ● We can reuse code, but often code is very use specific. ● There are also built-in functions.

July The Structure of Programming. ● So we can divide our code into Modules. ● This means we don't need to evaluate massive amounts of code any time we need to get something done. ● Modules have their own functions. ● Helps with separating programs. ● In a similar way, we saw that types have methods.

July Classes ● Python allows us to build our own types. ● These are called classes. ● When we have a class, we can create instances/objects of that class. ● Recall the difference between a type (str, int) and a value ('this is a string', 10). ● This is analogous to the difference between a class and an object.

July Classes ● To make a class we just do: class Class_name(object): block ● Class is a keyword and object is a type. ● Class names start with Capital letters by convention. ● To create objects or instances of Class_name we use: x = Class_name()

July Class methods. ● So far our class objects can't do a whole lot. ● One way to make them more useful is to add methods to class objects. ● We do this by putting them in the block of code under the class name. ● But we want our methods to work on each individual class object. ● Ie. when we call 'aaa'.isUpper() we want it to only work on 'aaa' not on all strings. ● How can we get our method to refer to our class object rather than to all classes?

July Class methods. ● To solve this issue we use the keyword self ● Say we have a class Patient that is meant to store information about patients in a hospital. ● We may want to update the patient's age. ● To do this we need a method set_age. ● class Patient(object): def set_age(self, age): self.age = age

July Class methods. ● Note that these are methods, so that if we have a patient p1 to set his age we use p1.set_age(10) and not p1.set_age(p1,10) ● Self should always be the first parameter in a method but it is passed for free when we call the method.

July Class Constructors. ● There is also away to set class variables when you construct class instances. ● The special method __init__ is called whenever you try and construct an instance of an object. ● class Patient(object): def __init__(self, name, age): self.name = name self.age = age ● Called by x = Patient(“Joey”, 10)

July Class methods: Special Methods. ● __ indicates that the method is a special method. ● These are used to make our classes work more like Python's built-in types. ● For example: ● __str__ is used when printing ● __cmp__ is used to allow boolean operations. ● __add__ is used to allow the + operator. ● __iter__ is used to allow your type to be used in for loops.

July Classes - Encapsulation ● One of the big benefits of classes is that they hide implementation details from the user. ● We call this encapsulation. ● A well designed class has methods that allow the user to get out all the information they need out of it. ● This allows a user to concentrate on their code rather than on your code. ● This also frees you to change the internal implementation of the class.

July Class conventions. ● Class names start with upper case letters. ● Class methods and instances start with lower case letters. ● Method definitions should have docstrings just like function definitions. ● Classes should have docstrings just like modules have docstrings that describe what the class does.

July Break, the first.

July The Structure of Programming. ● We want our programs to be both reusable and extendable. ● Reusable means that other people can easily take our code and use it for their problems. ● Extendable means that it's easy to modify our code to handle new issues that come up. ● How do we resolve the tension between the two?

July Classes - Inheritance ● We want a way to allow modifications to existing code, that don't alter the ability of existing code to run. ● One way we could do this is to write a new class that copies the old class plus has some new functions. ● This is a lot of work, especially if you decide to change the old class down the road.

July Classes - Inheritance ● Instead we can use Inheritance. ● Classes are allowed to inherit methods and variables from other classes. ● If class A inherits from class B, then class B is called the superclass, and class A the subclass. ● Classes inherit all of the methods and variables in the superclass. ● One can overwrite or add new methods in the subclass as appropriate.

July Classes – Inheritance ● The syntax for creating subclasses is: ● class Class_name(Subclass_name): ● block ● Note that this means our previous class is a subclass of the class object. ● If you define a method with the same name as one in the superclass, you overwrite it.

July Classes - Inheritance ● Inheritance is a really powerful tool that is easy to abuse. ● Inheritance should be used to represent 'is-a' relations. ● So a Surgery Patient is a type of Patient. ● A mammal is a type of animal. ● A party is a type of event. ● When coming up on to a new problem, a common first step is to think about class structures and what objects you'll need.

July Break, the second

July Midterm Review