CHAPTER 13 Object Oriented Programming. Objectives  Design class definitions  Implement data hiding and encapsulation  Use accessor and mutator methods.

Slides:



Advertisements
Similar presentations
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Advertisements

L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)
Road Map Introduction to object oriented programming. Classes
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
CS 106 Introduction to Computer Science I 03 / 24 / 2008 Instructor: Michael Eckmann.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
ASP.NET Programming with C# and SQL Server First Edition
Abstract Data Types and Encapsulation Concepts
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Visual C++ Programming: Concepts and Projects Chapter 6A: Methods (Concepts)
Writing Classes (Chapter 4)
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
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.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own 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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
Chapter 10 Introduction to Classes
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
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.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
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.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 12 Object-Oriented Programming Starting Out with Games & Graphics.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
1 COMS 261 Computer Science I Title: Classes Date: November 4, 2005 Lecture Number: 27.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6A Methods (Concepts)
Chapter 11 An introduction to object-oriented design.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
CHAPTER 13B Object Oriented Programming (Tutorial)
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
INTRODUCTION TO CLASSES & OBJECTS CREATING CLASSES USING C#
OOP Details Constructors, copies, access. Initialize Fields are not initialized by default:
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 3: Using Methods, Classes, and Objects
Chapter 4: Writing classes
Classes & Objects: Examples
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
CIS16 Application Development and Programming using Visual Basic.net
NAME 436.
Lecture 8 Object Oriented Programming (OOP)
Classes and Objects Systems Programming.
Presentation transcript:

CHAPTER 13 Object Oriented Programming

Objectives  Design class definitions  Implement data hiding and encapsulation  Use accessor and mutator methods  Implement an initializing constructor  Use public, and private access modes  Create and reference a two-dimensional array

Introduction  An abstraction is an idea with varying amounts of definition  In computer science, abstractions are class definitions  Examples of system-defined class definitions: Button, TextBox, Label, Form  Objects are created from class definitions (abstractions)  Objects are implementations of the abstraction

Introduction (continued)

System namespace ( System ) Drawing class library ( System::Drawing ) Math class library ( System::Math ) Drawing::Graphics Drawing::Brush Drawing::Font Drawing::Pen. Math::Sin() Math::Cos() Math::Sqrt() Math::PI. methods constant object class defs

Introduction (continued)

OOP Example  Consider a program in which Frog objects are created and made to hop across the screen  The Frog class definition is an abstraction  The Frogs seen on the interface are objects (instances of the class)  Objects exhibit all the properties and methods defined by the class they are derived from

OOP Example (continued)

The Frog Class Definition  A Frog class definition needs  Class variables (the Frog icons)  Instance variables X and y coordinates of the Frog The assigned icon  Constructor

The Frog Class Definition (continued)  A Frog class definition needs  Instance methods Operations that Frog objects can perform Example: showIcon() Returns the Frog’s assigned icon to the client program so that it can be displayed

The Frog Class Definition (continued)

Instantiation and Use  Frog objects can be instantiated using gcnew

Instantiation and Use (continued)

Initializing Constructors  Constructors are used to instantiate objects  Initializing constructors use parameters to construct objects with specific properties that can be passed in as actual arguments

Initializing Constructors (continued)  The code for an initializing constructor (from Frog.h)  Parameters xcoord and ycoord are assigned to instance variables x and y

Initializing Constructors (continued)  The default constructor assigns every Frog object it makes the same x and y coordinates  Initializing constructors may be preferable to default constructors because the client can specify the location for each Frog object created  To prevent the client from using the default constructor, it should be placed in the private portion of the Frog class definition

Data Hiding  Data hiding is the practice of making data members inaccessible by designating them as private  Data members that are private can only be accessed by class methods and not by the client

Data Hiding (continued)  In this example, the client constructs a Frog with x and y locations -50, -75 and later assigns other negative coordinate values  This could lead to problems, since negative coordinates are off of the interface

Data Hiding (continued)  To make it impossible for the client to place invalid values into instance variables like x and y, these data members can be “hidden” by placing them in the private portion of the class definition  Invalid assignments by the client are no longer allowed  Unfortunately, valid assignments are not allowed either  public methods are provided to the client to allow it to change values stored in private data members

Data Hiding (continued)  Hidden data members are not visible to the client  The good news:  Invalid assignments are no longer possible  The bad news  Valid assignments are not allowed either  public methods are provided to the client to allow it to change values stored in private data members

Accessor and Mutator Methods (continued)  Accessor and mutator methods are public methods that provide access to private data members  An accessor method is a public method that returns the value stored in a private data member

Accessor and Mutator Methods (continued)  In this example, public methods getX() and getY() are available to the client, allowing it to retrieve the data stored in private data members x and y

Accessor and Mutator Methods (continued)  The definitions of public methods getX() and getY() are contained in the class definition

Accessor and Mutator Methods (continued)  A mutator method is a public method that changes the value stored in a private data member  In this example, setX() is a public mutator method used to change the private x coordinates of each of four Frog objects

Accessor and Mutator Methods (continued)  Mutator methods can be used to screen data before it is assigned to a private data member, as shown in the class definition of setX()

Utility Methods  Utility methods are instance methods that perform operations other than those performed by constructors, destructors, accessors, and mutators. (Example: verifyX() )

Utility Methods (continued)  A Frog class definition might want a public method that the client can call when they want a frog to hop

Complete Frog Class Definition  private data members  x, y, icon  private methods  Default constructor ( Frog() )  Utility methods verifyX() verifyY()

Complete Frog Class Definition (continued)  public methods  Initializing constructor ( Frog(int, int) )  Accessor methods getX() getY() showIcon()

Complete Frog Class Definition (continued)  public methods  Mutator methods setX() – used to assign a value to x setY() - used to assign a value to y setLeaping() used to assign the leaping icon setSitting() used to assign the sitting icon  Utility method hop() – used to add 25 to the x coordinate

Complete Frog Class Definition (continued)

Client Code  The client needs to instantiate four Frog objects and position them vertically along a starting line  Constant STARTX stores the starting x coordinate postion  Constants START1, START2, START3 and START4 store the starting y coordinate positions

Client Code (continued)  Using the initializing constructor, each new Frog can be positioned in a different location

Client Code (continued)  To draw a single Frog you first construct a Rectangle object  Upper left corner x coordinate is Frog->getX()  Upper left corner y coordinate is Frog->getY()  Width and height are both 25 pixels  The DrawIcon() method displays an icon in a Rectangle  The icon is Frog1->showIcon()

Client Code (continued)

 To reset the interface all frogs are repositioned back to their starting X location and all frog icons are set to the sitting frog icon

Client Code (continued)  A random number is used to select one of the four frogs. The chosen frog’s icon is set to the leaping icon

Summary  Class definitions are abstractions  Objects are instances of the class  Initializing constructors are useful if the objects they construct need to have unique values assigned to their attributes  public data members and methods are visible to the client  private data members and methods are not visible to the client

Summary (continued)  Data hiding is used to restrict client access to data members  Accessor methods are public methods that can be used to retrieve a value stored in a private data member  Mutator methods are public methods that can be used to assign new values to a private data member

Summary (continued)  Utility methods are carry out operations that constructors, destructors, accessors and mutators do not perform