An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.

Slides:



Advertisements
Similar presentations
Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method.
Advertisements

1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 11 Classes and Object- Oriented Programming.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Road Map Introduction to object oriented programming. Classes
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes and Objects Systems Programming.
VBA Modules, Functions, Variables, and Constants
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 11: Classes and Data Abstraction
ASP.NET Programming with C# and SQL Server First Edition
1 Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Using Classes Object-Oriented Programming Using C++ Second Edition 5.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 8 More Object Concepts
Writing Classes (Chapter 4)
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter 8: User-Defined Classes and ADTs
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Chapter 4 Introduction to Classes, Objects, Methods and strings
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
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.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Introduction to Object-Oriented Programming Lesson 2.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 12: Classes and Data Abstraction.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 05: Classes and Data Abstraction.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
Chapter 11 An introduction to object-oriented design.
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
Programming Logic and Design Seventh Edition
3 Introduction to Classes and Objects.
Object-Oriented Programming: Classes and Objects
Topics Procedural and Object-Oriented Programming Classes
Chapter 3: Using Methods, Classes, and Objects
Object-Oriented Programming: Classes and Objects
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Defining Classes and Methods
Object Oriented Programming in java
SPL – PS3 C++ Classes.
Presentation transcript:

An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters

An Object-Oriented Approach to Programming Logic and Design 2 Objectives Create methods with and without arguments Create instance methods in a class Explore the rationale behind data hiding Organize classes

An Object-Oriented Approach to Programming Logic and Design 3 Objectives (continued) Understand the role of the this reference Begin to understand how to use constructors

An Object-Oriented Approach to Programming Logic and Design 4 Creating Methods With and Without Arguments Method – a program module containing statements that carry out a task Invoking, or calling, a method causes it to execute A method may require that data items (called arguments or parameters) be provided to it A method may or may not send back data (called a return value)

An Object-Oriented Approach to Programming Logic and Design 5 Creating Methods With and Without Arguments (continued) Example: a class with no arguments and no return value

An Object-Oriented Approach to Programming Logic and Design 6 Creating Methods With and Without Arguments (continued) Method declaration (header) contains: –Optional access modifiers (public, private, or protected) –Return type –Method name –Optional list of arguments, separated by commas

An Object-Oriented Approach to Programming Logic and Design 7 Creating Methods With No Arguments Creating methods with no arguments: –Place the method within the class that will use it, but not within any other method

An Object-Oriented Approach to Programming Logic and Design 8 Creating Methods With No Arguments (continued) Invoking a method with no arguments

An Object-Oriented Approach to Programming Logic and Design 9 Creating Methods With No Arguments (continued) Program flow in a method call

An Object-Oriented Approach to Programming Logic and Design 10 Creating Methods With No Arguments (continued)

An Object-Oriented Approach to Programming Logic and Design 11 Creating Methods With No Arguments (continued) Creating Methods With No Arguments (continued) Full name of a method:. Example: Hello.nameAndAddress()

An Object-Oriented Approach to Programming Logic and Design 12 Creating Methods With One Argument Requires the following in the method header: –The type of the argument –A local name for the argument Argument receives a value that is passed in when the method is invoked Any method that does not return a value is declared with type void

An Object-Oriented Approach to Programming Logic and Design 13 Creating Methods With One Argument (continued)

An Object-Oriented Approach to Programming Logic and Design 14 Creating Methods With One Argument (continued) When calling a method with arguments, you can pass –Values –Variables –Constants Each time a method is called, it is reinitialized to use the new values for arguments that are passed in the call

An Object-Oriented Approach to Programming Logic and Design 15 Creating Methods With One Argument (continued)

An Object-Oriented Approach to Programming Logic and Design 16 Creating Methods With One Argument (continued)

An Object-Oriented Approach to Programming Logic and Design 17 Creating Methods With Multiple Arguments Creating methods with multiple arguments: –Argument list is separated by commas –Each argument’s type is specified with the argument name, even if two or more arguments are the same type –Argument type is placed before the argument name

An Object-Oriented Approach to Programming Logic and Design 18 Creating Methods With Multiple Arguments (continued) Method with two arguments

An Object-Oriented Approach to Programming Logic and Design 19 Creating Methods With Multiple Arguments (continued)

An Object-Oriented Approach to Programming Logic and Design 20 Creating Methods With Multiple Arguments (continued) Arguments passed to a method must match the method’s declaration in: –Number of arguments –Type of each argument –Order of each argument in the list

An Object-Oriented Approach to Programming Logic and Design 21 Creating Methods that Return Values Return type can be any type If no value is to be returned, the type is void Return type is placed in the header before the method name Example : public static numeric predictRaise(numeric money)

An Object-Oriented Approach to Programming Logic and Design 22 Creating Methods that Return Values (continued) Method can only return at most one value Return value can be assigned to a variable by using the method call as a variable Example: myNewSalary = predictRaise(mySalary)

An Object-Oriented Approach to Programming Logic and Design 23 Creating Methods that Return Values (continued) Return value of a method can be used as a variable Example: print “New salary is”, predictRaise(mySalary)

An Object-Oriented Approach to Programming Logic and Design 24 Creating Instance Methods in a Class Every object that is an instance of a class is assumed to possess the same methods as the class Static methods: methods for which no object needs to exist Non-static methods: methods that exist to be used with an object created from a class

An Object-Oriented Approach to Programming Logic and Design 25 Creating Instance Methods in a Class (continued) Mutator methods: methods which set values Accessor methods: methods which retrieve values

An Object-Oriented Approach to Programming Logic and Design 26 Creating Instance Methods in a Class (continued)

An Object-Oriented Approach to Programming Logic and Design 27 Creating Instance Methods in a Class (continued) A class does not create an object; an object must be instantiated from the class Once instantiated, a class’s methods can be invoked by name Syntax:.

An Object-Oriented Approach to Programming Logic and Design 28 Creating Instance Methods in a Class (continued)

An Object-Oriented Approach to Programming Logic and Design 29 Creating Instance Methods in a Class (continued)

An Object-Oriented Approach to Programming Logic and Design 30 Exploring the Rationale Behind Data Hiding Accessing data only through public methods of a class guarantees that the data will only be manipulated in an approved way Methods can be written to ensure that the data conforms to required standards Example: ensuring that all phone numbers have area codes

An Object-Oriented Approach to Programming Logic and Design 31 Organizing Classes Data fields: –No special order required, but key data items (used as database look-up values) placed first –Common practice to declare all data items first, before any methods Methods: –Common practice to order them alphabetically by name, or to put them in get/set pairs

An Object-Oriented Approach to Programming Logic and Design 32 Organizing Classes (continued)

An Object-Oriented Approach to Programming Logic and Design 33 Understanding the Role of the this Reference Just one copy of a class’s methods are stored in memory, and all objects instantiated from that class use the same stored method Each object has its own set of data The this reference (or pointer variable) allows the programmer to specify the use of the particular object instance The this reference is implicitly used when the object’s code is running

An Object-Oriented Approach to Programming Logic and Design 34 Understanding the Role of the this Reference (continued)

An Object-Oriented Approach to Programming Logic and Design 35 An Introduction to Using Constructors Constructor method establishes the object when it is instantiated Default constructor requires no arguments, and is created automatically by the compiler for every class you write Constructor class has the same name as the class name in some languages Constructors are usually declared as public so that other classes can instantiate objects of this class

An Object-Oriented Approach to Programming Logic and Design 36 An Introduction to Using Constructors (continued) Constructor is a method; it can contain any valid program statements Constructor is usually placed as the first method in the class Constructor may have arguments that have to be supplied when creating a new object from the class

An Object-Oriented Approach to Programming Logic and Design 37 An Introduction to Using Constructors (continued)

An Object-Oriented Approach to Programming Logic and Design 38 Summary Method is a procedure that contains a series of statements to accomplish a task Methods may contain one or more arguments defining data that must be supplied when calling the method Method may return a value of a specific type, or may be void An instantiated object possesses the methods and attributes of the class from which it was created

An Object-Oriented Approach to Programming Logic and Design 39 Summary (continued) Data hiding using encapsulation guarantees that the data may only be manipulated by the methods of a class Most data items are declared as private to support data hiding this reference is a pointer to the instantiated object whose code is currently running Constructor methods are created automatically for each class, and are executed when the object is created from the class