Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 9 Classes.

Slides:



Advertisements
Similar presentations
When is Orientated Programming NOT? Mike Fitzpatrick.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 11 Classes and Object- Oriented Programming.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 9- 1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18 Indexing Structures for Files.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11 Object, Object- Relational, and XML: Concepts, Models, Languages,
Basic OOP Concepts and Terms
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Software Engineering Principles and C++ 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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Part 1 Conditionals and Loops.
C++ fundamentals.
Chapter 6: A First Look at Classes
Introduction to Classes, Objects, Methods and Attributes Lecture # 2.
An Object-Oriented Approach to Programming Logic and Design
Starting Out With Java 5 (Control Structures to Objects) Chapter 6 By Tony Gaddis Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: From Control Structures through Objects Third Edition.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Limits.
Starting Out with Java: From Control Structures through Objects.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 3 Simple.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural and Object-Oriented Programming 13.1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Classes and Multiform Projects.
Basic OOP Concepts and Terms. In this class, we will cover: Objects and examples of different object types Classes and how they relate to objects Object.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Learners Support Publications Object Oriented Programming.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 12 Object-Oriented Programming Starting Out with Games & Graphics.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
© 2010 Pearson Addison-Wesley. All rights reserved. 6-1 Chapter Topics Chapter 6 discusses the following main topics: –Classes and Objects –Instance Fields.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Introduction to Classes, Objects, Methods and Attributes Lecture # 5.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 3 A First Look at Classes and Objects.
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide.
Chapter 2 Principles of Programming and Software Engineering.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
CS16: UML’s Unified Modeling Language. UML Class Diagram A UML class diagram is a graphical tool that can aid in the design of a class. The diagram has.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 3: A First Look at Classes and Objects.
Chapter 6 A First Look at Classes. 2 Contents 1. Classes and objects 2. Instance Fields and Methods 3. Constructors 4. Overloading Methods and Constructors.
Programming Logic and Design Seventh Edition
Topics Procedural and Object-Oriented Programming Classes
Chapter 3 Classes & Objects
A first Look at Classes.
About the Presentations
Chapter 3 Introduction to Classes, Objects Methods and Strings
Classes and Objects housefly object Insect class mosquito object
Basic OOP Concepts and Terms
Object Oriented Programming in java
2.1 Introduction to Object-Oriented Programming
Presentation transcript:

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 9 Classes and Object Oriented Programming

1-2 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural and Object-Oriented Programming Concept: Procedural programming is a method of writing software. It is a programming practice centered on the procedures or actions that take place in a program. Object-oriented programming is centered on objects. Objects are created from abstract data types that encapsulate data and function together.

1-3 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural and Object-Oriented Programming Two methods of programming: Procedural Object-oriented

1-4 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural and Object-Oriented Programming Procedural programming is made up of one or more procedures Procedures operate on data items that are separate from the procedure Data items are passed from one procedure to another Focus is on the creation of procedures that operate on the program’s data

1-5 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural and Object-Oriented Programming Object-oriented programming is a self-contained unit that consists of data attributes and methods that operate on data attributes An object is a software entry that contains both data and procedures Data contained in an object is known as the object’s data attributes Data attributes are variables that reference data Procedures that an object performs are known as methods

1-6 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural and Object-Oriented Programming Object-oriented programming ( OOP ) OOP separates code and data through encapsulation and data hiding Encapsulation refers to the combining of data and code into a single object Data hiding refers to an object’s ability to hide its data attributes from code that is outside the object Protects the data attributes from accidentally being corrupted Object reusability – it can be used by any program that needs its services

1-7 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Classes Concept: A class is code that specifies data attributes and methods for a particular type of data.

1-8 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Classes Class – code that specifies the data attributes and methods of a particular type of object. It is the “blueprint” that objects may be created from Each object that is created from a class is called an instance of the class

1-9 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Classes Figure 9-3 A blueprint and houses built from the blueprint Figure 9-5 The housefly and mosquito objects are instances of the Insect class

1-10 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Classes Class Definition It is a set of statements that define a class’s methods and data attributes Starts with the keyword class, followed by the class name The self parameter is required in every method of the class It is a way of knowing which object’s data attributes the method is supposed to operate on Most python classes have the __init__ method Known as the initializer method because it initializes the object’s data attributes

1-11 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Classes Class Definition Program 9-1 (Coin class, not a complete program)

1-12 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Classes Class Definition Program 9-2 (coin_demo1.py)

1-13 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Classes Class Definition Program 9-2 (cont.) (coin_demo1.py)

1-14 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Classes Class Definition Figure 9-7 The my_coin variable references a Coin object

1-15 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Classes Hiding Attributes … __attributeName Program 9-4 (coin_demo3.py)

1-16 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Classes Storing Classes in Modules Create a module with the class definitions; save the file using the.py extension Import the module to any program that needs access to the class definition

1-17 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Classes Storing Classes in Modules Program 9-7 (account.py)Program 9-8 (account_test.py)

1-18 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Classes The __str__ method Returns a string containing the object’s state Object’s state is the value of the object’s attribute at any given moment For example: def __str__(self): state_string = ‘The account balance is $%.2f.’ % self.__balance return state_string

1-19 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Working with Instances Concept: Each instance of a class has its own set of data attributes.

1-20 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Working with Instances When the self parameter is used to create an attribute, the attribute belongs to the specific object that self references – instance attribute(s) Figure 9-8 The coin1, coin2, and coin3 variables reference three Coin objects

1-21 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Working with Instances Accessor and Mutator Methods The accessor method returns a value from a class’s attribute but does not change it. The mutator method stores a value in a data attribute or changes the value of a data attribute in some other way.

1-22 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Techniques for Designing Classes The Unified Modeling Language (UML) Provides a set of standard diagrams for graphically depicting object-oriented systems

1-23 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Techniques for Designing Classes The Unified Modeling Language (UML) Figure 9-10 General layout of a UML diagram for a class

1-24 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Techniques for Designing Classes The Unified Modeling Language (UML) Figure 9-12 UML diagram for the CellPhone class

1-25 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Techniques for Designing Classes Finding the Classes in a Problem Get a written description of the problem domain. Identify all the nouns in the description. Each of these is a potential class. Refine the list to include only the classes that are relevant to the problem.

1-26 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Techniques for Designing Classes Identifying a Class’s Responsibility The things that the class is responsible for knowing The actions that the class is responsible for doing Figure 9-13 UML diagram for the Customer class

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley QUESTIONS ? Chapter 9 Classes and Object Oriented Programming