Chapter 12
Object-Oriented Programming
Starting Out with
Games & Graphics in C++
Tony Gaddis
Addison Wesley
is an imprint of
© 2010 Pearson Addison-Wesley. All rights reserved.
12.1 Procedural and Object-Oriented
Programming
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.
Copyright © 2010 Pearson Addison-Wesley
1-2
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, objectoriented 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.
Copyright © 2010 Pearson Addison-Wesley
1-3
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.
Copyright © 2010 Pearson Addison-Wesley
Figure 12-2 Code outside the object interacts with
the object’s methods
1-4
12.1 Procedural and Object-Oriented
Programming
Object Reusability
• 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.
Copyright © 2010 Pearson Addison-Wesley
1-5
12.1 Procedural and Object-Oriented
Programming
An Everyday Example of an Object
• 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.
Copyright © 2010 Pearson Addison-Wesley
1-6
12.1 Procedural and Object-Oriented
Programming
An Everyday Example of an Object
•
•
•
•
•
•
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.
Copyright © 2010 Pearson Addison-Wesley
1-7
12.1 Procedural and Object-Oriented
Programming
OOP Terminology
• 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++.
Copyright © 2010 Pearson Addison-Wesley
1-8
12.2 Classes and Objects
Concept:
A class is code that specifies the
member variables and member
functions for a particular type of
object.
Copyright © 2010 Pearson Addison-Wesley
1-9
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.
Figure 12-3 A blueprint and
houses built from the blueprint
Copyright © 2010 Pearson Addison-Wesley
1-10
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.
Figure 12-4 The cookie cutter
metaphor
Copyright © 2010 Pearson Addison-Wesley
1-11
12.2 Classes and Objects
Class Declarations
Copyright © 2010 Pearson Addison-Wesley
1-12
12.2 Classes and Objects
Class Declarations
Copyright © 2010 Pearson Addison-Wesley
1-13
12.2 Classes and Objects
Mutators and Accessors
• 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
Copyright © 2010 Pearson Addison-Wesley
1-14
12.2 Classes and Objects
Defining Member Functions
Each of these
functions are
setting the
values of the
data members.
There is no
return value.
Copyright © 2010 Pearson Addison-Wesley
1-15
12.2 Classes and Objects
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
Copyright © 2010 Pearson Addison-Wesley
1-16
12.2 Classes and Objects
Defining Member Functions
Uses the private data members to draw a circle
Copyright © 2010 Pearson Addison-Wesley
1-17
12.2 Classes and Objects
Creating an Object
Create an
instance of the
Circle object
Copyright © 2010 Pearson Addison-Wesley
1-18
12.2 Classes and Objects
Constructors
Establishes initial
values for the data
members
Copyright © 2010 Pearson Addison-Wesley
1-19
12.2 Classes and Objects
Creating Multiple Objects from the Same Class
Creating
three
different
instances
of the
circle
class,
with three
different
values
Copyright © 2010 Pearson Addison-Wesley
1-20
12.2 Classes and Objects
Overloaded Member Functions
Two versions
of the draw
function, with
two different
parameter lists
(overloaded
functions)
Copyright © 2010 Pearson Addison-Wesley
1-21
12.2 Classes and Objects
Overloaded Constructors
Copyright © 2010 Pearson Addison-Wesley
1-22
12.2 Classes and Objects
Creating Arrays of Objects
Copyright © 2010 Pearson Addison-Wesley
1-23
12.2 Classes and Objects
Passing Objects as Arguments to Functions
Copyright © 2010 Pearson Addison-Wesley
1-24
12.2 Classes and Objects
Destructors
Copyright © 2010 Pearson Addison-Wesley
1-25
12.3 Inheritance
Concept:
Inheritance allows a new class to
extend an existing class. The new
class inherits the members of the
class it extends.
Copyright © 2010 Pearson Addison-Wesley
1-26
12.3 Inheritance
Generalization and Specialization
Figure 12-2 Bumblebees and
grasshoppers are specialized
versions of an insect
Copyright © 2010 Pearson Addison-Wesley
1-27
12.3 Inheritance
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
Copyright © 2010 Pearson Addison-Wesley
1-28
12.3 Inheritance
Inheritance and the “Is a” Relationship
Copyright © 2010 Pearson Addison-Wesley
1-29
12.3 Inheritance
Passing Arguments to the Base Class Constructor
Copyright © 2010 Pearson Addison-Wesley
1-30
12.4 An Object-Oriented Game:
Balloon Target
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
Copyright © 2010 Pearson Addison-Wesley
1-31
Chapter 12
Object-Oriented Programming
QUESTIONS
Addison Wesley
is an imprint of
?
© 2010 Pearson Addison-Wesley. All rights reserved.