360 likes | 800 Views
Chapter 12 Object-Oriented Programming. Starting Out with Games & Graphics in C++ Tony Gaddis. 12.1 Procedural and Object-Oriented Programming. Concept:
E N D
Chapter 12Object-Oriented Programming Starting Out with Games & Graphics in C++ Tony Gaddis
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.
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 objectis 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. • Encapsulationrefers to the combining of data and code into a single object. • Data hidingrefers to an object’s ability to hide its fields from code that is outside the object.
12.1 Procedural and Object-Oriented Programming Figure 12-2 Code outside the object interacts with the object’s methods 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.
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.
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 statethat the alarm clock is currently in.
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.
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 membervariables • Refer to methods as member functions • These are the terms that we will use, since they are commonly used in C++.
12.2 Classes and Objects Concept: A class is code that specifies the member variables and member functions for a particular type of object.
12.2 Classes and Objects Figure 12-3 A blueprint and houses built from the blueprint 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.
12.2 Classes and Objects Figure 12-4 The cookie cutter metaphor 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 instanceof the class.
12.2 Classes and Objects Class Declarations
12.2 Classes and Objects Class Declarations
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
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.
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
12.2 Classes and Objects Defining Member Functions Usesthe private data members to draw a circle
12.2 Classes and Objects Creating an Object Create an instance of the Circle object
12.2 Classes and Objects Constructors Establishes initial values for the data members
12.2 Classes and Objects Creating Multiple Objects from the Same Class Creating three different instances of the circle class, with three different values
12.2 Classes and Objects Overloaded Member Functions Twoversions of the draw function, with two different parameter lists (overloaded functions)
12.2 Classes and Objects Overloaded Constructors
12.2 Classes and Objects Creating Arrays of Objects
12.2 Classes and Objects Passing Objects as Arguments to Functions
12.2 Classes and Objects Destructors
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.
12.3 Inheritance Generalization and Specialization Figure 12-2 Bumblebees and grasshoppers are specialized versions of an insect
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
12.3 Inheritance Inheritance and the “Is a” Relationship
12.3 Inheritance Passing Arguments to the Base Class Constructor
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
Chapter 12Object-Oriented Programming QUESTIONS ?