visual c++ programming: concepts and projects chapter 13a: object-oriented programming (concepts)

45
Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

Upload: percival-stafford

Post on 18-Dec-2015

233 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

Visual C++ Programming: Concepts and Projects

Chapter 13A:Object-Oriented Programming

(Concepts)

Page 2: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

Objectives

In this chapter, you will:• 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

2Programming with Visual C++

Page 3: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

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

3Programming with Visual C++

Page 4: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

Introduction (continued)

4Programming with Visual C++

Page 5: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

Introduction (continued)

5Programming with Visual C++

Page 6: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

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

6Programming with Visual C++

Page 7: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

OOP Example (continued)

7Programming with Visual C++

Page 8: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

OOP Example (continued)

8Programming with Visual C++

Page 9: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

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

9Programming with Visual C++

Page 10: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

The Frog Class Definition (continued)

• A Frog class definition needs: (continued)– 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

10Programming with Visual C++

Page 11: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

The Frog Class Definition (continued)

11Programming with Visual C++

Page 12: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

Instantiation and Use

• Frog objects can be instantiated using gcnew

12Programming with Visual C++

Page 13: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

Instantiation and Use (continued)

13Programming with Visual C++

Page 14: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

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

14Programming with Visual C++

Page 15: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

Initializing Constructors (continued)

• The code for an initializing constructor (from Frog.h)

• Parameters xcoord and ycoord are assigned to instance variables x and y

15Programming with Visual C++

Page 16: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

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

16Programming with Visual C++

Page 17: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

17Programming with Visual C++

Page 18: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

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

18Programming with Visual C++

Page 19: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

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

19Programming with Visual C++

Page 20: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

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

20Programming with Visual C++

Page 21: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

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

21Programming with Visual C++

Page 22: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

Accessor and Mutator Methods

• 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

22Programming with Visual C++

Page 23: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

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

23Programming with Visual C++

Page 24: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

Accessor and Mutator Methods (continued)

• The definitions of public methods getX() and getY() are contained in the class definition

24Programming with Visual C++

Page 25: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

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

25Programming with Visual C++

Page 26: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

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()

26Programming with Visual C++

Page 27: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

Accessor and Mutator Methods (continued)

27Programming with Visual C++

Page 28: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

Utility Methods

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

28Programming with Visual C++

Page 29: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

Utility Methods (continued)

• A Frog class definition might want a public method that the client can call when he or she wants a frog to hop

29Programming with Visual C++

Page 30: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

Complete Frog Class Definition

• private data members– x, y, icon

• private methods– Default constructor (Frog())– Utility methods

• verifyX()• verifyY()

30Programming with Visual C++

Page 31: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

Complete Frog Class Definition (continued)

• public methods– Initializing constructor (Frog(int, int))– Accessor methods

• getX()• getY()• showIcon()

31Programming with Visual C++

Page 32: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

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

32Programming with Visual C++

Page 33: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

Complete Frog Class Definition (continued)

33Programming with Visual C++

Page 34: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

Complete Frog Class Definition (continued)

34Programming with Visual C++

Page 35: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

35Programming with Visual C++

Page 36: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

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 position

• Constants START1, START2, START3 , and START4 store the starting y coordinate positions

36Programming with Visual C++

Page 37: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

Client Code (continued)

37Programming with Visual C++

Page 38: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

Client Code (continued)

• Using the initializing constructor, each new Frog can be positioned in a different location

38Programming with Visual C++

Page 39: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

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()

39Programming with Visual C++

Page 40: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

Client Code (continued)

40Programming with Visual C++

Page 41: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

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

41Programming with Visual C++

Page 42: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

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

42Programming with Visual C++

Page 43: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

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

43Programming with Visual C++

Page 44: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

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

44Programming with Visual C++

Page 45: Visual C++ Programming: Concepts and Projects Chapter 13A: Object-Oriented Programming (Concepts)

Summary (continued)

• Utility methods carry out operations that constructors, destructors, accessors, and mutators do not perform

45Programming with Visual C++