1 Programming for Engineers in Python Autumn 2011-12 Lecture 5: Object Oriented Programming.

Slides:



Advertisements
Similar presentations
Python Objects and Classes
Advertisements

Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
6-1 Chapter Goals Determine whether a problem is suitable for a computer solution Describe the computer problem-solving process and relate it to Polya’s.
Problem Solving and Algorithm Design
Chapter 6 Problem Solving and Algorithm Design. 6-2 Chapter Goals Determine whether a problem is suitable for a computer solution Describe the computer.
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
1 Basic Object Oriented Concepts Overview l What is Object-Orientation about? l What is an Object? l What is a Class? l Constructing Objects from Classes.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
1 Gentle Introduction to Programming Session 5: Memory Model, Object Oriented Programming.
Object-oriented Programming Concepts
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
ASP.NET Programming with C# and SQL Server First Edition
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 7 Object-Oriented Programming.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
TCU CoSc Introduction to Programming (with Java) Getting to Know Java.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin IE 423 Design of Decision Support Systems (304)
CSC 212 Object-Oriented Programming and Java Part 1.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Classes 1 COMPSCI 105 S Principles of Computer Science.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
1 Programming for Engineers in Python Autumn Lecture 6: More Object Oriented Programming.
Programming Paradigms Lecturer Hamza Azeem. What is PP ? Revision of Programming concepts learned in CPLB Learning how to perform “Object-Oriented Programming”
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Process-Concept.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Chapter 7 Object-Oriented Programming
Classes and OOP.
Objects as a programming concept
About the Presentations
Road Map Introduction to object oriented programming. Classes
Procedural Abstraction Object-Oriented Code
Object-Oriented Programming
Object-Oriented Programming
Algorithms and Problem Solving
Object-Oriented Programming
Creating and Using Classes
Presentation transcript:

1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming

2 Lecture 4 Highlights Tuples, Dictionaries Sorting Lists Modular programming Data analysis: text categorization Collect data Most frequent words How it is really done

3 Today Quick review on functions Object Oriented Programming (partially based on chapters in the book Think Python, available in the site)

4 Functions (Quick Overview)

5 How to Calculate 5! + 3! + 6!?

6 Problems Same code duplicated 3 times More elements  more code duplicates The program becomes longer and more complicated May cause bugs, cut & paste errors Hard to understand what was the writer’s intension What would we like? Write the code once! Use it several times with different arguments

7 Solution - Functions

8 Functions A group of declarations and statements that is assigned a name Effectively, a named statement block Usually has a value A sub-program Inside a fucntion we can call other functions Which can themselves use other functions, and so on… Function output: Return value to the calling function If no value is to be returned, ‘None’ is returned

9 What are They Good For? Generalize a repeated set of instructions We don’t have to keep writing the same thing over and over Solve bugs once… They can break your problem down into smaller sub-tasks Easier to solve complex problems They make a program much easier to read and maintain Abstraction – we don’t have to know how a function is implemented to use it

10 In Short Why do we need functions? Code reusability Modularity Abstraction

11 Examples

12 Passing Arguments to Functions When a function is called, arguments’ values are attached to function’s formal parameters by order, and an assignment occurs before execution Values are copied to formal parameters

13 Passing Arguments to Functions A reference is passed by reference Example: lists This explains why we can change an array’s content within a function a

14 Example

15 Pyhton Memory Model Stack: local variables and arguments, every function uses a certain part of the stack Stack variables “disappear” when scope ends Heap: global variables and objects, scope independent Garbage Collector Partial description

16 How to Change a Variable via Functions? So how can a method change an outer variable? By its return value By accessing heap-based memory (e.g., lists)

17 A Car How would you represent a car? Parts / features: 4 wheels, steering wheel, horn, color,… Functionality: drive, turn left, honk, repaint,… In Python???

18 Lets Start Simpler How would you represent a library? Container of books Each book: Title Author ISBN number Number of pages Publisher

19 Printing a Book

20 What Would We Want? To group the definition of several variables under a single name – a new type Somewhat similar to functions… Using the new type as if it is part of the language: Define variables, perform operation

21 Object-Oriented Programming (OOP) Represent problem-domain entities using a computer language When building a software in a specific domain, describe the different components of the domain as types and variables Thus we can take another step up in abstraction

22 Class as a Blueprint A class is a blueprint of objects

23 Class as a Blueprint A class is a blueprint of objects

24 Classes as Data Types Classes define types that are a composition of other types and have unique functionality An instance of a class is named an object Every instance may contain: Constructors Attributes (data members / fields) Methods

25 Car Example Members: 4 wheels, steering wheel, horn, color,… Every car instance has its own Methods: drive, turn left, honk, repaint,… Constructors: by color (only), by 4 wheels, engine,…

26 How to Represent a Point in 2D? Alternatives: Two variables x,y Elements in a list / tuple A new data type Creating a new type is a (little) more complicated, but has its advantages (to be apparent soon) class – a user defined type How to represent a point?

27 Creating a new Point (instantiation) blank is an instance of class Point

28 But Where is the Point? x does not exist “in” blank (nor y) We want to be able to access x,y via Point instance

29 Constructors A special function, defined in the class’s code that “produces” instances Invoked automatically when an object is instantiated Given as input whatever is required to produce an instance What would a Point’s constructor accept as inputs?

30 Constructors – More Technically Defined within the class’s scope Always named __init__ Short for initialization 2 underscores init 2 underscores The first parameter would be self To be explained later on Note that when invoking (calling) the constructor, self is not passed as an argument It is common for the parameters of __init__ to have the same names as the attributes

31 Attributes The variable p1 refers to a Point object p1.x means “Go to object p1 refers to and get the value of x” There is no conflict between a variable x and the attribute x p1 x  3.0 y  4.0 Point

32 Instances and Functions Objects can be passed as arguments to functions Objects are mutable

33 Copying Objects is operator indicates that p1 and p2 are not the same object, the default behavior of the == operator is the same as the is operator

34 Instances as Return Values

35 A Circle How would you represent a circle object? Attributes? Constructor?

36 Shallow / Deep Copy

37 Shallow / Deep Copy (Cont.)

38 Object Oriented Programming Programs are made of object definitions and function definitions, and most of the computation is expected in terms of operations on objects Each object definition corresponds to some object or concept in the real world, and the functions that operate on that object correspond to the ways real-world objects interact

39 Methods Method is a function that is associated with a particular class Examples in strings, lists, dictionaries, tuples Difference between methods and functions: Methods are defined inside a class definition The syntax for invoking a method A method is called through an instance

40 Example: Distance Between Two Points (function)

41 Example: Distance Between Two Points (method)

42 Example: In Circle

43 Next week: defining a new type that behaves as part of the language More OOP Implementation of Rational Numbers