Objects and Classes. Chapter 8

Similar documents
CS1210 Lecture 28 Mar. 27, 2019

MITOCW watch?v=flgjisf3l78

Object Oriented Programming in Python

Programming for Engineers in Python

OBJECT ORIENTED PROGRAMMING 7

OBJECT ORIENTED PROGRAMMING 6

Linked lists. A linked list is considered a recursive data structure because it has a recursive definition.

Object Oriented Programming

OBJECT ORIENTED PROGRAMMING 7

Classes and Objects 1

Data Structures (list, dictionary, tuples, sets, strings)

& Object-Oriented Programming (POOP) Modified from CS61A Summer 2012 Lecture at UC Berkeley

Professor Hugh C. Lauer CS-1004 Introduction to Programming for Non-Majors

CS1004: Intro to CS in Java, Spring 2005

OBJECT ORIENTED PROGRAMMING 6

(Refer Slide Time: 01:40)

A Crash Course in Python Part II. Presented by Cuauhtémoc Carbajal ITESM CEM

Abstract Data Types Chapter 1

Introduction to Object-Oriented Programming

CSE : Python Programming. Packages (Tutorial, Section 6.4) Announcements. Today. Packages: Concretely. Packages: Overview

6.001 Notes: Section 6.1

Book keeping. Will post HW5 tonight. OK to work in pairs. Midterm review next Wednesday

At full speed with Python

Functions, Scope & Arguments. HORT Lecture 12 Instructor: Kranthi Varala

CMSC201 Computer Science I for Majors

Lab 3: Decisions and Boolean Logic This lab accompanies Chapter 4 of Starting Out with Programming Logic & Design.

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

THE IF STATEMENT. The if statement is used to check a condition: if the condition is true, we run a block

6.001 Notes: Section 31.1

Lecture 12 ADTs and Stacks

What is a class? Responding to messages. Short answer 7/19/2017. Code Listing 11.1 First Class. chapter 11. Introduction to Classes

COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 14 Object Oriented Programming TODAY 7/11/2012 WHERE WE WERE: FUNCTIONAL PROGRAMMING

6.001 Notes: Section 8.1

User Defined Types. Babes-Bolyai University Lecture 06. Lect Phd. Arthur Molnar. User defined types. Python scope and namespace

What is a function? "The Practice of Computing Using Python", Punch & Enbody, Copyright 2013 Pearson Education, Inc.

Implementing an Algorithm for Boomerang Fraction Sequences in Python

CMSC201 Computer Science I for Majors

A lot of people make repeated mistakes of not calling their functions and getting errors. Make sure you're calling your functions.

Unit E Step-by-Step: Programming with Python

Introduction to Oral Presentations

Homework notes. Homework 2 grades posted on canvas. Homework 3 due tomorrow. Homework 4 posted on canvas. Due Tuesday, Oct. 3

Variables, Functions and String Formatting

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

CS2304: Python for Java Programmers. CS2304: Sequences and Collections

GIS 4653/5653: Spatial Programming and GIS. More Python: Statements, Types, Functions, Modules, Classes

2. The object-oriented paradigm!

CS61A Lecture 20 Object Oriented Programming: Implementation. Jom Magrotker UC Berkeley EECS July 23, 2012

PREPARING FOR PRELIM 1

Programming Paradigms in Python

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

Object-Oriented Programming Paradigm

Object Orientation. A Crash Course Intro

Objects and Classes APRIL 23 RD 2012

Testing and Debugging

Lecturer: Sebastian Coope Ashton Building, Room G.18 COMP 201 web-page:

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

MEMOIZATION, RECURSIVE DATA, AND SETS

Object Oriented Programming

The Dynamic Typing Interlude

PREPARING FOR PRELIM 2

CS1 Lecture 27 Mar. 26, 2018

Data Structures I: Linked Lists

Develop Mobile Front Ends Using Mobile Application Framework A - 2

CSE 341, Autumn 2015, Ruby Introduction Summary

Abstract Data Types. CS 234, Fall Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples

Design Phase. Create a class Person. Determine the superclass. NSObject (in this case)

Objects. say something to express one's disapproval of or disagreement with something.

Chapter 12 Object-Oriented Programming. Starting Out with Games & Graphics in C++ Tony Gaddis

c++ and python modern programming techniques

Lastly, in case you don t already know this, and don t have Excel on your computers, you can get it for free through IT s website under software.

CSE : Python Programming

CSE : Python Programming

Software Design and Analysis for Engineers

4.2 Function definitions the basics

Homework Assignment #2

A set, collection, group, or configuration containing members regarded as having certain attributes or traits in common.

Week 8: Operator overloading

Week 3 Classes and Objects

Lecture 04 FUNCTIONS AND ARRAYS

Pointers in C/C++ 1 Memory Addresses 2

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

Textbook. Topic 6: Functions. Motivation. What is a Function? What s a function? How can we use functions to write better software?

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

CS61A Notes Week 13: Interpreters

CS115 - Module 4 - Compound data: structures

Guidelines for Writing Mathematical Proofs

Structure and Interpretation of Computer Programs Summer 2015 Midterm 2

Genome 559 Intro to Statistical and Computational Genomics Lecture 15b: Classes and Objects, II Larry Ruzzo

22. Two-Dimensional Arrays. Topics Motivation The numpy Module Subscripting functions and 2d Arrays GooglePage Rank

Week - 01 Lecture - 04 Downloading and installing Python

ECE 3574: Applied Software Design: Unit Testing using Catch. Chris Wyatt

UNDERSTANDING CLASS DEFINITIONS CITS1001

6.001 Notes: Section 15.1

CSCI-1200 Data Structures Fall 2018 Lecture 3 Classes I

CS 161 Computer Security

Functional Programming in Java. CSE 219 Department of Computer Science, Stony Brook University

Functions and Decomposition

Exercise: Inventing Language

Transcription:

200

Chapter 8 Objects and Classes The style of programming we have seen so far is called procedural programming. This was the first programming paradigm, developed in the 1950 s and 1960 s alongside the growth of hardware technology. In this chapter we look at objectoriented programming, a more modern style of coding that was developed in the 1970 s and 1980 s. The motivation for the development of object-oriented programming was the repeated failure of programmers to eliminate bugs from large programs written in the procedural style. Object-oriented programming is not more powerful than procedural programming any program that can be written in one style can be written in the other. However, in many situations it is easier to write correct programs in the object-oriented style. In Chapter 8 we will look at programs that use graphics, animation and simulations, and you will see that all of these lend themselves naturally to the object-oriented style. 201

202 CHAPTER 8. OBJECTS AND CLASSES 8.1 Concepts There is some terminology that you must understand to get started on writing object-oriented programs. The terminology is not difficult, but it is uses the building blocks we have already seen in a new way. Here are the basic pieces. A class is a structure that holds both data and functions for manipulating that data. We have seen some classes before without calling them classes. For example, Python has a class called List. A list holds sequences of whatever data you give it, and it has functions, or methods, such as append( ) for manipulating that data. The class itself is a pattern. It says what kinds of data and what kinds of functionality instances of the class will have. You can construct specific instances of a class, which are called objects. For example, we might have a class Person. This would describe the data and functionality needed to represent people. Each object of this class would represent one individual person. Like all data, the data in an object is stored in variables. There are two types of variables in classes: instance variables andclass variables. Each object of a class has its own copy of each of the instance variables, so each object can have different data stored in the instance variables. For example, in our Person class there might be an instance variable to hold a persons name. Each object of the class will represent a different person, and so each will have its own name. The class variables are shared by all objects of the class; there is only one copy of the class variables, and it is visible to all objects of the class. Our Person class might have a class variable to hold the population size. The number of people in existence is the same regardless of which person we ask for this number. We refer to the instance variables of the class through objects of the class using a dot-notation: <object name>.<variable name>. For example, if x is an object of class Person we would refer to the name instance variable as x.name. We refer to the class variables through the class itself. Person.population might be the class variable that represent the size of the Person population. Objects have their own functions for manipulating their data. We call the functions contained in an object its methods. If class Person has a GetOlder( ) method that adds one to the age of a person, and if x is an object of this class, we would tell object x to get older with the code x. GetOlder ( ) You might think of x.getolder() as a command to object x to increase its age variable. As we said, each object of a class has its own copies of the instance variables and methods of the class. The class definition needs some way to refer to an individual object s data and methods. The word self is used for this. This word only appears in class definitions; it always refers to the current object. Every method of every class has self as its first argument. For example, the GetOlder( ) method has header def GetOlder ( s e l f ) :

8.1. CONCEPTS 203 When we call this method on a specific object, as in x.getolder( ), the object x is passed as the argument for self. We might have a method SetAge( ) that would set the persons age to a specific value. This would have header def SetAge ( s e l f, myage ) : and would be called as in x.setage(19). All methods have self as their first argument; you never explicitly pass a value for self, but the system substitutes the object whose method you are invoking for this argument. The word self is used for similar reasons in references to instance variables within the methods of a class. self.age is the age variable for whatever object is being referenced. For example, here is the complete code for the SetAge( ) and GetOlder( ) methods: def SetAge ( s e l f, myage ) : s e l f. age = myage def GetOlder ( s e l f ) : s e l f. age = s e l f. age + 1 If we call x.setage(19), then the object stored in variable x is passed for the self argument in method SetAge( ). Then self.age, which is the value of the age instance variable of object x, is set to 19. This notation probably seems cumbersome at first, but with a little practice it will begin to seem natural: x. foobar() runs method foobar() on object x. Every class has a specific method called a constructor that is run when a new object of that class is created. The purpose of a constructor is to give initial values to the instance variables of the class. If the constructor has arguments, values for those arguments must be provided when a new object is constructed. Constructors in Python all have the name init ( ). Of course, since constructors are methods they all have self as their first argument. For example, we might want the constructor for the Person class to give a name to the new person. The constructor that does this is def i n i t ( s e l f, myname ) : s e l f. name = myname s e l f. age = 0 Remember that the constructor should initialize all of the instance variables of the class, even those that are not mentioned in the constructor s arguments. If we fail to initialize the variable then it doesn t exist. For example, if the constructor for class Person omits the line s e l f. age = 0 then new persons will not have an age variable. If other methods, such as a Print( ) method, refer to the age of the person and the age variable doesn t exist, our program will crash when these methods are called. We construct objects of a class by using the class name as a function. The function that is actually called is the init ( ) method of this class, so Python

204 CHAPTER 8. OBJECTS AND CLASSES expects us to provide all of the arguments (except self ) of this method. For example, the init ( ) method above for class Person needs a string for the name, so we could construct a new person with x = Person ( bob ) Here, finally, is the complete code for a simple program that uses classes. This defines the class Person that we have been discussing. The main( ) function of the program makes use of this class definition to construct several persons, set the age variables, and print out their information. Note that the main( ) function is simple and intuitive. This is typically the case with object-oriented programs the hard work in such programs is implementing the classes; once that is accomplished they are usually easy to use. Note also that classes can be used in more than one program. One of the goals of object-oriented programming is to make reusable classes that can be written carefully and then used in a wide range of applications. A programmer doesn t need to know the details of how a class is implemented in order to use it. All the programmer needs to know is what data the class holds and what methods it offers. The List and Dictionary classes in Python are illustrations of this. We have used these classes to write many programs without any knowledge of how the classes are implemented. These classes are very powerful. It is the object-oriented paradigm that allows us to separate the implementation of the classes from their use.

8.1. CONCEPTS 205 class Person : def i n i t ( s e l f, myname ) : s e l f. name = myname s e l f. age = 0 def SetAge ( s e l f, myage ) : s e l f. age = myage def GetOlder ( s e l f ) : s e l f. age = s e l f. age + 1 def P r i n t ( s e l f ) : print ( %s i s %d y e a r s o l d. %( s e l f. name, s e l f. age ) ) def main ( ) : x = Person ( bob ) y = Person ( s u z i e ) z = Person ( j o e ) main ( ) x. SetAge (57) x. GetOlder ( ) x. GetOlder ( ) x. P r i n t ( ) y. P r i n t ( ) Program 8.1.1: A complete object-oriented program