beginning c++ through game programming, second edition

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

Upload: argus

Post on 12-Jan-2016

93 views

Category:

Documents


2 download

DESCRIPTION

Beginning C++ Through Game Programming, Second Edition. by Michael Dawson. Chapter 9. Advanced Classes and Dynamic Memory: Game Lobby. Objectives. Combine objects Use friend functions Overload operators Dynamically allocate and free memory Avoid memory leaks - PowerPoint PPT Presentation

TRANSCRIPT

Page 10: Beginning C++ Through Game Programming, Second Edition

Using Critter and Farmint main(){ Critter crit("Poochie"); cout << "Critter's name:" << crit.GetName()

cout << "\nCreating critter farm.\n"; Farm myFarm(3); cout << "\nAdding three critters to farm.\n"; myFarm.Add(Critter("Moe")); myFarm.Add(Critter("Larry")); myFarm.Add(Critter("Curly"));

cout << "\nCalling Roll...\n"; myFarm.RollCall(); return 0;}

Page 11: Beginning C++ Through Game Programming, Second Edition

Friend Functions• Friend functions have complete access to any member of a class • Specify function is friend of a class with friend before function prototype inside class definition • In a Critter class: friend void Peek(const Critter& aCritter);

• Peek() can access any member of a Critter object • Outside of Critter class definition, define global function Peek():void Peek(const Critter& aCritter){ cout << aCritter.m_Name << endl;}• Peek() can access private member m_Name of a Critter object

Page 12: Beginning C++ Through Game Programming, Second Edition

Overloading Operators• Give meaning to built-in operators used with new types that you

define • Overload the << operator so it can work with Critter objects• Can send Critter object << to cout• Outside a Critter class, define a global function that

overloads << operatorostream& operator<<(ostream& os, const Critter&

aCritter){ os << "Critter Object - "; os << "m_Name: " << aCritter.m_Name; return os;}

• Function can directly access the private data member m_Name of a Critter object because function is a friend of the Critter class

friend ostream& operator<<(ostream& os, const Critter& aCritter);

Page 13: Beginning C++ Through Game Programming, Second Edition

Dynamically Allocating Memory

• Local variables live on the stack• Local variables don't persist beyond function in which declared• Heap memory persists until programmer frees it• Dynamic heap memory offers efficiency

– Use only memory needed, free when done– Access memory even after a function ends (without having to return a copy of the object)

Page 14: Beginning C++ Through Game Programming, Second Edition

The new Operator• Allocates memory on the heap and returns its

address • Use new followed by the type of value int* pHeap = new int;

• new int allocates memory on the heap for one int and returns its address

• int* pHeap, declares a local pointer, pHeap, which points to the newly allocated chunk of memory on the heap

• Can initialize memory on the heap at the same time you allocate it

int* pHeap = new int(10);

Page 25: Beginning C++ Through Game Programming, Second Edition

Copy Constructors• Sometimes an object is copied automatically• Copying is done by a special member function called the copy constructor• Occurs when an object is

– Passed by value to a function– Returned from a function– Initialized to another object through an initializer– Provided as a single argument to the object’s constructor

Page 26: Beginning C++ Through Game Programming, Second Edition

Default Copy Constructor

• Default copy constructor is supplied if you don’t write your own

• Copies the value of each data member to data members of the same name in the new object—shallow copy

• For simple classes, default copy constructor is usually fine

• For classes with a data member that points to value on the heap, usually write your own copy constructor

Page 32: Beginning C++ Through Game Programming, Second Edition

Overloading the Assignment Operator

• When both sides of an assignment statement are objects of the same class, the class’ assignment operator member function is called• A default assignment operator member function is supplied for you if you don’t write one of your own • Default assignment operator provides only member-wise duplication• For simple classes, the default assignment operator is usually fine• When you have a class with a data member that points to a value on the heap, you usually provide an overloaded assignment operator of your own; if you don’t, you’ll end

up with shallow copies of objects when assigning one object to another.

Page 34: Beginning C++ Through Game Programming, Second Edition

Summary• Aggregation is the combining of objects so that one is part of another • Friend functions have complete access to any member of a class • Operator overloading allows you to define new meanings for built-in operators as they relate to objects of your own classes• The stack is an area of memory that is automatically managed for you and is used for local variables• The heap is an area of memory that the programmer can use to allocate and free memory

Page 35: Beginning C++ Through Game Programming, Second Edition

Summary (cont.)• The new operator allocates memory on the heap and returns its address• The delete operator frees memory on the heap that was previously allocated• A dangling pointer points to an invalid memory location• Dereferencing or deleting a dangling pointer can cause your program to crash• A memory leak is an error in which memory that has been allocated becomes inaccessible and can no longer be freed

Page 36: Beginning C++ Through Game Programming, Second Edition

Summary (cont.)• A destructor is a member function that’s called just before an object is destroyed• If you don’t write a destructor of your own, the complier will supply a default destructor for you • The copy constructor is a member function that’s invoked when an automatic copy of an object is made• A default copy constructor is supplied for a class if you don’t write one of your own• The default copy constructor simply copies the value of each data member to data members with the same names in the copy, producing a shallow copy