Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 11 Classes and Object- Oriented Programming.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Looking inside classes Fields, Constructors & Methods Week 3.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Chapter 3 Introduction to Classes, Objects, Methods, and Strings
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 9 Classes.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Software Engineering Principles and C++ Classes
ASP.NET Programming with C# and SQL Server First Edition
Chapter 6: A First Look at Classes
Writing Classes (Chapter 4)
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
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.
Starting Out with Java: From Control Structures through Objects.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Functions.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
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 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Data Structures Using C++1 Chapter 1 -Software Engineering Principles -ADT and Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Classes and Multiform Projects.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
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 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 13 GUI Programming.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 12 Object-Oriented Programming Starting Out with Games & Graphics.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
© 2010 Pearson Addison-Wesley. All rights reserved. 6-1 Chapter Topics Chapter 6 discusses the following main topics: –Classes and Objects –Instance Fields.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
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.
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.
Chapter 11 An introduction to object-oriented design.
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide.
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.
Chapter 11 An introduction to object-oriented design.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 7: Introduction to Object- Oriented Programming in Python – Exercises Xiang Lian The University of.
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
Java Programming: Guided Learning with Early Objects
A first Look at Classes.
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Introduction to Classes
Object Oriented Programming
Introduction to Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Corresponds with Chapter 7
Defining Classes and Methods
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Topics Introduction to Functions Defining and Calling a Function
Defining Classes and Methods
Presentation transcript:

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 11 Classes and Object- Oriented Programming

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Topics Procedural and Object-Oriented Programming Classes Working with Instances Techniques for Designing Classes

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural Programming Procedural programming: writing programs made of functions that perform specific tasks –Procedures typically operate on data items that are separate from the procedures –Data items commonly passed from one procedure to another –Focus: to create procedures that operate on the program’s data

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Object-Oriented Programming Object-oriented programming: focused on creating objects Object: entity that contains data and procedures –Data is known as data attributes and procedures are known as methods Methods perform operations on the data attributes Encapsulation: combining data and code into a single object

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Object-Oriented Programming (cont’d.)

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Object-Oriented Programming (cont’d.) Data hiding: object’s data attributes are hidden from code outside the object –Access restricted to the object’s methods Protects from accidental corruption Outside code does not need to know internal structure of the object Object reusability: the same object can be used in different programs –Example: 3D image object can be used for architecture and game programming

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Object-Oriented Programming (cont’d.)

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley An Everyday Example of an Object Data attributes: define the state of an object –Example: clock object would have second, minute, and hour data attributes Public methods: allow external code to manipulate the object –Example: set_time, set_alarm_time Private methods: used for object’s inner workings

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Classes Class: code that specifies the data attributes and methods of a particular type of object –Similar to a blueprint of a house or a cookie cutter Instance: an object created from a class –Similar to a specific house built according to the blueprint or a specific cookie –There can be many instances of one class

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Classes (cont’d.)

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Classes (cont’d.)

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Class Definitions Class definition: set of statements that define a class’s methods and data attributes –Format: begin with class Class_name: Class names often start with uppercase letter –Method definition like any other python function definition self parameter: required in every method in the class – references the specific object that the method is working on

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Class Definitions (cont’d.) Initializer method: automatically executed when an instance of the class is created –Initializes object’s data attributes and assigns self parameter to the object that was just created –Format: def __init__ (self): –Usually the first method in a class definition

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Class Definitions (cont’d.)

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Class Definitions (cont’d.) To create a new instance of a class call the initializer method –Format: My_instance = Class_Name() To call any of the class methods using the created instance, use dot notation –Format: My_instance.method() –Because the self parameter references the specific instance of the object, the method will affect this instance Reference to self is passed automatically

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Hiding Attributes and Storing Classes in Modules An object’s data attributes should be private –To make sure of this, place two underscores ( __ ) in front of attribute name Example: __current_minute Classes can be stored in modules –Filename for module must end in.py –Module can be imported to programs that use the class

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The BankAccount Class – More About Classes Class methods can have multiple parameters in addition to self –For __init__, parameters needed to create an instance of the class Example: a BankAccount object is created with a balance –When called, the initializer method receives a value to be assigned to a __balance attribute –For other methods, parameters needed to perform required task Example: deposit method amount to be deposited

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The __str__ method Object’s state: the values of the object’s attribute at a given moment __str__ method : displays the object’s state –Automatically called when the object is passed as an argument to the print function –Automatically called when the object is passed as an argument to the str function

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Working With Instances Instance attribute: belongs to a specific instance of a class –Created when a method uses the self parameter to create an attribute If many instances of a class are created, each would have its own set of attributes

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Accessor and Mutator Methods Typically, all of a class’s data attributes are private and provide methods to access and change them Accessor methods: return a value from a class’s attribute without changing it –Safe way for code outside the class to retrieve the value of attributes Mutator methods: store or change the value of a data attribute

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Passing Objects as Arguments Methods and functions often need to accept objects as arguments When you pass an object as an argument, you are actually passing a reference to the object –The receiving method or function has access to the actual object Methods of the object can be called within the receiving function or method, and data attributes may be changed using mutator methods

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Techniques for Designing Classes UML diagram: standard diagrams for graphically depicting object-oriented systems –Stands for Unified Modeling Language General layout: box divided into three sections: –Top section: name of the class –Middle section: list of data attributes –Bottom section: list of class methods

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Finding the Classes in a Problem When developing object oriented program, first goal is to identify classes –Typically involves identifying the real-world objects that are in the problem –Technique for identifying classes: 1.Get written description of the problem domain 2.Identify all nouns in the description, each of which is a potential class 3.Refine the list to include only classes that are relevant to the problem

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Finding the Classes in a Problem (cont’d.) 1.Get written description of the problem domain –May be written by you or by an expert –Should include any or all of the following: Physical objects simulated by the program The role played by a person The result of a business event Recordkeeping items

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Finding the Classes in a Problem (cont’d.) 2.Identify all nouns in the description, each of which is a potential class –Should include noun phrases and pronouns –Some nouns may appear twice

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Finding the Classes in a Problem (cont’d.) 3.Refine the list to include only classes that are relevant to the problem –Remove nouns that mean the same thing –Remove nouns that represent items that the program does not need to be concerned with –Remove nouns that represent objects, not classes –Remove nouns that represent simple values that can be assigned to a variable

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Identifying a Class’s Responsibilities A classes responsibilities are: –The things the class is responsible for knowing Identifying these helps identify the class’s data attributes –The actions the class is responsible for doing Identifying these helps identify the class’s methods To find out a class’s responsibilities look at the problem domain –Deduce required information and actions

Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Summary This chapter covered: –Procedural vs. object-oriented programming –Classes and instances –Class definitions, including: The self parameter Data attributes and methods __init__ and __str__ functions Hiding attributes from code outside a class –Storing classes in modules –Designing classes