Beginning C++ Through Game Programming, Second Edition by Michael Dawson.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Web Application Development Slides Credit Umair Javed LUMS.
C++ Classes & Data Abstraction
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. S. Kozaitis Summer Summary of Topics Related to Classes Class definition Defining member.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
Guide to Programming with Python
Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Learners Support Publications Classes and Objects.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 10 Introduction to Classes
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Chapter 8 Operator Overloading, Friends, and References.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Object-Oriented Programming. Procedural Programming All algorithms in a program are performed with functions and data can be viewed and changed directly.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
Slide 1 Chapter 8 Operator Overloading, Friends, and References.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
Introduction to Object-Oriented Programming Lesson 2.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Classes: Defining Your Own Data Types Basic principles in OOP Define a new data type as a class and use objects of a class Member Functions –Constructors.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Encapsulation, Data Hiding and Static Data Members
Review What is an object? What is a class?
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Chapter 5 Classes.
group work #hifiTeam
Classes and Objects.
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
Submitted By : Veenu Saini Lecturer (IT)
Constructors & Destructors
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

Beginning C++ Through Game Programming, Second Edition by Michael Dawson

Chapter 8 Classes: Critter Caretaker

Objectives Create new types by defining classes Declare class data members and member functions Instantiate objects from classes Set member access levels Declare static data members and member functions

Classes Define new types Like a blueprint Code that groups data members and member functions From a class, create individual objects that have their own copies of each data member and access to all of the member functions Classes for game objects: space ship, sword, mutant chicken, etc.

Critter Class class Critter // class definition // defines a new type Critter { public: int m_Hunger; // data member void Greet(); // member func prototype };

Critter Class Explained For a virtual pet Begin with class followed by name Surround the class body with curly braces {} and end it with a semicolon ( ; ) Declare data members to represent object qualities Can begin name with m_ for clarity Declare member functions to represent object abilities

Defining Member Functions Can define member functions outside of a class definition // member function definition void Critter::Greet() { cout << "Hi. My hunger level is " << m_Hunger << ".\n"; } Qualify with the class name Can access object members Sending m_Hunger to cout means that Greet() displays the value of m_Hunger for the specific object through which the function is called

Instantiating Objects When you create an object, you instantiate it from a class Critter crit1; Critter crit2; Two Critter objects in existence, crit1 and crit2

Accessing Data Members Can access an available data member of an object with member selection operator crit1.m_Hunger = 9; Assigns 9 to crit1 ’s data member m_Hunger crit2.m_Hunger = 3; Assigns 3 to crit2 ’s data member m_Hunger Each object has its own m_Hunger data member with its own value

Calling Member Functions crit1.Greet(); Calls crit1 ’s Greet() member function Accesses crit1 ’s m_Hunger data member

Constructor Special member function automatically called every time a new object instantiated Useful for performing initialization of a new object

Critter Constructor class Critter { public: int m_Hunger; // constructor prototype Critter(int hunger = 0); void Greet(); }; // constructor definition Critter::Critter(int hunger) { cout << "A new critter has been born!" << endl; m_Hunger = hunger; }

Declaring and Defining a Constructor As with any member function, can declare the constructor in the class definition and can define constructor outside of class Constructor has no return type Must have same name as the class itself

Default Constructor A constructor that requires no arguments If you don’t define a default constructor, the compiler defines default If you write a constructor, compiler won’t provide a default constructor Usually a good idea to have a default constructor; supply your own when necessary.

Member Initializers Shorthand to assign values to data members in a constructor Critter::Critter(int hunger = 0, int boredom = 0): m_Hunger(hunger), m_Boredom(boredom) {} // empty constructor body

Calling a Constructor Automatically Don’t explicitly call a constructor Whenever you instantiate a new object, its constructor is automatically called Critter crit(7); Message A new critter has been born! is displayed Constructor assigns 7 to the object’s m_Hunger data member

Setting Member Access Levels Treat objects as encapsulated entities Avoid directly altering or accessing an object’s data members Call an object’s member functions instead Can enforce member restrictions

Private Critter //Private Critter //Demonstrates setting member access levels #include using namespace std; class Critter { public: // begin public section Critter(int hunger = 0); int GetHunger() const; void SetHunger(int hunger); private: // begin private section int m_Hunger; };

Private Critter (cont.) Critter::Critter(int hunger): m_Hunger(hunger) { cout << "A new critter has been born!\n"; } int Critter::GetHunger() const { return m_Hunger; } void Critter::SetHunger(int hunger) { if (hunger < 0) cout << "Can't set negative.\n"; else m_Hunger = hunger; }

Specifying Public and Private Access Levels Every data member and member function has an access level Access level determines from where in your program you can access member Any public member can be accessed in any part of the program private members can only be directly accessed by code in the class So in a main() function, the following would be illegal if uncommented: //cout << crit.m_Hunger;

Accessor Member Functions Allow indirect access to a data member GetHunger() is an accessor member function In a main() function, following line is legal: cout << crit.GetHunger(); SetHunger() limits access; can't set to negative number.

Constant Member Functions Can’t modify a data member of its class or call a non-constant member function of its class Protects from accidentally altering a data member in the member function Makes your intentions clear to other programmers Get accessor member functions can generally be defined as constant Constant member function can alter a static data member

Static Data Members and Member Functions Static data member –Data member that exists for the entire class –Can exist before any object of class does Static member function –Exists for the entire class –Doesn't need an object to call it through

Static Critter #include using namespace std; class Critter { public: //static member variable declaration static int s_Total; Critter(int hunger = 0); //static member function prototype static int GetTotal(); private: int m_Hunger; };

Static Critter (cont.) //static member variable initialization int Critter::s_Total = 0; Critter::Critter(int hunger): m_Hunger(hunger) { cout << "A critter has been born!"; ++s_Total; } //static member function definition int Critter::GetTotal() { return s_Total; }

Declaring and Initializing Static Data Members Declares a static data member s_Total : static int s_Total; Can prefix variable name with s_ for clarity Initialize static data member outside class definition Qualify static data member with its class name int Critter::s_Total = 0;

Accessing Static Data Members Can access a public static data member anywhere in program cout << Critter::s_Total << "\n\n"; Can also access a static member through any object of the class cout << crit1.s_Total << "\n\n"; Inside class definition, don't need to use class name ++s_Total; Can make a static data member private

Static Member Functions Can access a public static member function anywhere in program Begin defintion with static static int GetTotal() Call static member functions outside a class using class name cout << Critter::GetTotal(); Can access through any object of the class cout << crit1.GetTotal(); Static member functions don’t have direct access to any class data members or member functions

Summary In object-oriented programming (OOP), you define different types of objects with relationships that interact with each other You can create a new type by defining a class A class is a blueprint for an object You can access data members and member functions of objects through the member selection operator (. ) Every class has a constructor—a special member function that’s automatically called every time a new object is instantiated

Summary (cont.) A default constructor requires no arguments Member initializers provide shorthand to assign values to data members in a constructor You can set member access levels in a class by using the keywords public, private, and protected A public member can be accessed by any part of your code through an object A private member can only be accessed by a member function of that class

Summary (cont.) An accessor member function allows indirect access to a data member A static data member exists for the entire class A static member function exists for the entire class A constant member function can’t modify non-static data members or call non- constant member functions of its class