1 / 32

Chapter 12 Object-Oriented Programming

Chapter 12 Object-Oriented Programming. Starting Out with Games & Graphics in C++ Tony Gaddis. 12.1 Procedural and Object-Oriented Programming. Concept:

cleave
Télécharger la présentation

Chapter 12 Object-Oriented Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 12Object-Oriented Programming Starting Out with Games & Graphics in C++ Tony Gaddis

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. 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++.

  9. 12.2 Classes and Objects Concept: A class is code that specifies the member variables and member functions for a particular type of object.

  10. 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.

  11. 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. 12.2 Classes and Objects Class Declarations

  13. 12.2 Classes and Objects Class Declarations

  14. 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

  15. 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.

  16. 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

  17. 12.2 Classes and Objects Defining Member Functions Usesthe private data members to draw a circle

  18. 12.2 Classes and Objects Creating an Object Create an instance of the Circle object

  19. 12.2 Classes and Objects Constructors Establishes initial values for the data members

  20. 12.2 Classes and Objects Creating Multiple Objects from the Same Class Creating three different instances of the circle class, with three different values

  21. 12.2 Classes and Objects Overloaded Member Functions Twoversions of the draw function, with two different parameter lists (overloaded functions)

  22. 12.2 Classes and Objects Overloaded Constructors

  23. 12.2 Classes and Objects Creating Arrays of Objects

  24. 12.2 Classes and Objects Passing Objects as Arguments to Functions

  25. 12.2 Classes and Objects Destructors

  26. 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.

  27. 12.3 Inheritance Generalization and Specialization Figure 12-2 Bumblebees and grasshoppers are specialized versions of an insect

  28. 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

  29. 12.3 Inheritance Inheritance and the “Is a” Relationship

  30. 12.3 Inheritance Passing Arguments to the Base Class Constructor

  31. 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

  32. Chapter 12Object-Oriented Programming QUESTIONS ?

More Related