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

Slides:



Advertisements
Similar presentations
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Advertisements

Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.
Chapter 13 – Introduction to Classes
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 9 Classes.
Chapter 6: A First Look at classes. Object-Oriented Programming  Object-oriented programming is centered on creating objects rather than procedures.
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 11 Classes and Object- Oriented Programming.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
ASP.NET Programming with C# and SQL Server First Edition
Chapter 13: Object-Oriented Programming
C++ fundamentals.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Object Oriented Software Development
Introduction to Classes, Objects, Methods and Attributes Lecture # 2.
Chapter 4 Objects and Classes.
Introduction to Object-oriented programming and software development Lecture 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter 8 More Object Concepts
An Object-Oriented Approach to Programming Logic and Design
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
Starting Out With Java 5 (Control Structures to Objects) Chapter 6 By Tony Gaddis Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: From Control Structures through Objects Third Edition.
Starting Out with Java: From Control Structures through Objects.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Chapter 4 Introduction to Classes, Objects, Methods and strings
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 OOP - An Introduction ISQS 6337 John R. Durrett.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
© 2010 Pearson Addison-Wesley. All rights reserved. 6-1 Chapter Topics Chapter 6 discusses the following main topics: –Classes and Objects –Instance Fields.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, 2005 Pearson Addison-Wesley. All rights reserved. Chapter 6 Slide.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
1 Introduction to Object Oriented Programming Chapter 10.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Chapter 6 A First Look at Classes. 2 Contents 1. Classes and objects 2. Instance Fields and Methods 3. Constructors 4. Overloading Methods and Constructors.
Structures and Classes
Programming Logic and Design Seventh Edition
Topics Procedural and Object-Oriented Programming Classes
A first Look at Classes.
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
More about OOP and ADTs Classes
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
IFS410: Advanced Analysis and Design
Classes and Objects housefly object Insect class mosquito object
More about OOP and ADTs Classes
Outline Anatomy of a Class Encapsulation Anatomy of a Method
2.1 Introduction to Object-Oriented Programming
Chapter 6: A First Look at classes
Presentation transcript:

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

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.

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

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

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

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

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

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 OOP Terminology

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.

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 Figure 12-3 A blueprint and houses built from the blueprint

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 Figure 12-4 The cookie cutter metaphor

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

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

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

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.

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

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

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

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

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

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)

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

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

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

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

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.

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

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

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

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

Copyright © 2010 Pearson Addison-Wesley 12.4 An Object-Oriented Game: Balloon Target 1-31 Figure 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

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