Presentation is loading. Please wait.

Presentation is loading. Please wait.

Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 12 Object-Oriented Programming Starting Out with Games & Graphics.

Similar presentations


Presentation on theme: "Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 12 Object-Oriented Programming Starting Out with Games & Graphics."— Presentation transcript:

1 Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 12 Object-Oriented Programming Starting Out with Games & Graphics in C++ Tony Gaddis

2 Copyright © 2010 Pearson Addison-Wesley 12.1 Procedural and Object-Oriented Programming 1-2 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 functions together.

3 Copyright © 2010 Pearson Addison-Wesley 12.1 Procedural and Object-Oriented Programming There are primarily two methods of programming in use today: –Procedural –Object oriented The programs that you have written so far have been procedural in nature. Whereas procedural programming is centered on creating functions, object- oriented programming (OOP) is centered on creating objects. An object is a software entity that contains fields and methods. Fields are simply variables, arrays, or other data structures that are stored in the object. Methods are functions that perform operations on the object's data. OOP addresses the problem of code/data separation 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 fields from code that is outside the object. 1-3

4 Copyright © 2010 Pearson Addison-Wesley 12.1 Procedural and Object-Oriented Programming An object typically hides its fields, but allows outside code to access its methods. Only the object’s methods may then directly access and make changes to the object’s fields. 1-4 Figure 12-2 Code outside the object interacts with the object’s methods

5 Copyright © 2010 Pearson Addison-Wesley 12.1 Procedural and Object-Oriented Programming In addition to solving the problems of code/data separation, the use of OOP has also been encouraged by the trend of object reusability. An object is not a stand-alone program, but is used by programs that need its service. 1-5 Object Reusability

6 Copyright © 2010 Pearson Addison-Wesley 12.1 Procedural and Object-Oriented Programming Think of your alarm clock as an object It has the following fields: –The current second (a value in the range of 0–59) –The current minute (a value in the range of 0–59) –The current hour (a value in the range of 1–12) –The time the alarm is set for (a valid hour and minute) –Whether the alarm is on or off (“on” or “off ”) As you can see, the fields are merely data values that define the state that the alarm clock is currently in. 1-6 An Everyday Example of an Object

7 Copyright © 2010 Pearson Addison-Wesley 12.1 Procedural and Object-Oriented Programming You, the user of the alarm clock object, cannot directly manipulate these fields because they are private. To change a field’s value, you must use one of the object’s methods Here are some of the alarm clock object’s methods: –Set time –Set alarm time –Turn alarm on –Turn alarm off Each method manipulates one or more of the fields. Notice that all of these methods can be activated by you, who are outside of the alarm clock. Methods that can be accessed by entities outside the object are known as public methods. 1-7 An Everyday Example of an Object

8 Copyright © 2010 Pearson Addison-Wesley 12.1 Procedural and Object-Oriented Programming OOP programmers commonly use the term "fields" to describe the items of data that are stored in an object, and the term "methods" to describe the procedures that operate on an object's data. C++ programmers often refer to fields as member variables Refer to methods as member functions These are the terms that we will use, since they are commonly used in C++. 1-8 OOP Terminology

9 Copyright © 2010 Pearson Addison-Wesley 12.2 Classes and Objects 1-9 Concept: A class is code that specifies the member variables and member functions for a particular type of object.

10 Copyright © 2010 Pearson Addison-Wesley 12.2 Classes and Objects A class is code that specifies the member variables and member functions that a particular type of object has. Think of a class as a “blueprint” that objects may be created from. It serves a similar purpose as the blueprint for a house. 1-10 Figure 12-3 A blueprint and houses built from the blueprint

11 Copyright © 2010 Pearson Addison-Wesley 12.2 Classes and Objects A class is not an object, but a description of an object. When the program is running, it can use the class to create, in memory, as many objects of a specific type as needed. Each object that is created from a class is called an instance of the class. 1-11 Figure 12-4 The cookie cutter metaphor

12 Copyright © 2010 Pearson Addison-Wesley 12.2 Classes and Objects 1-12 Class Declarations

13 Copyright © 2010 Pearson Addison-Wesley 12.2 Classes and Objects 1-13 Class Declarations

14 Copyright © 2010 Pearson Addison-Wesley 12.2 Classes and Objects Member variables are private Member functions are public A mutator function is a member function that stores or changes the member variable in some way An accessor function is a member function that gets a value from a member variable, but doesn’t change it 1-14 Mutators and Accessors

15 Copyright © 2010 Pearson Addison-Wesley 12.2 Classes and Objects 1-15 Defining Member Functions Each of these functions are setting the values of the data members. There is no return value.

16 Copyright © 2010 Pearson Addison-Wesley 12.2 Classes and Objects 1-16 Defining Member Functions Each of these functions are getting the values of the data members and returning them to the function in which it was called

17 Copyright © 2010 Pearson Addison-Wesley 12.2 Classes and Objects 1-17 Defining Member Functions Uses the private data members to draw a circle

18 Copyright © 2010 Pearson Addison-Wesley 12.2 Classes and Objects 1-18 Creating an Object Create an instance of the Circle object

19 Copyright © 2010 Pearson Addison-Wesley 12.2 Classes and Objects 1-19 Constructors Establishes initial values for the data members

20 Copyright © 2010 Pearson Addison-Wesley 12.2 Classes and Objects 1-20 Creating Multiple Objects from the Same Class Creating three different instances of the circle class, with three different values

21 Copyright © 2010 Pearson Addison-Wesley 12.2 Classes and Objects 1-21 Overloaded Member Functions Two versions of the draw function, with two different parameter lists (overloaded functions)

22 Copyright © 2010 Pearson Addison-Wesley 12.2 Classes and Objects 1-22 Overloaded Constructors

23 Copyright © 2010 Pearson Addison-Wesley 12.2 Classes and Objects 1-23 Creating Arrays of Objects

24 Copyright © 2010 Pearson Addison-Wesley 12.2 Classes and Objects 1-24 Passing Objects as Arguments to Functions

25 Copyright © 2010 Pearson Addison-Wesley 12.2 Classes and Objects 1-25 Destructors

26 Copyright © 2010 Pearson Addison-Wesley 12.3 Inheritance 1-26 Concept: Inheritance allows a new class to extend an existing class. The new class inherits the members of the class it extends.

27 Copyright © 2010 Pearson Addison-Wesley 12.3 Inheritance 1-27 Figure 12-2 Bumblebees and grasshoppers are specialized versions of an insect Generalization and Specialization

28 Copyright © 2010 Pearson Addison-Wesley 12.3 Inheritance 1-28 Inheritance and the “Is a” Relationship Inheritance involves a base class and a derived class The base class is the general class The derived class is the specialized class

29 Copyright © 2010 Pearson Addison-Wesley 12.3 Inheritance 1-29 Inheritance and the “Is a” Relationship

30 Copyright © 2010 Pearson Addison-Wesley 12.3 Inheritance 1-30 Passing Arguments to the Base Class Constructor

31 Copyright © 2010 Pearson Addison-Wesley 12.4 An Object-Oriented Game: Balloon Target 1-31 Figure 12-21 The Balloon Target Game Balloon moves repeatedly across the screen from left to right Speed randomly changes Dart positioned at the bottom of screen Player launches dart with space bar Hit the balloon with the dart as many times as possible

32 Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 12 Object-Oriented Programming QUESTIONS ?


Download ppt "Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 12 Object-Oriented Programming Starting Out with Games & Graphics."

Similar presentations


Ads by Google