+ All Categories
Home > Documents > Beginning C++ Through Game Programming, Second Edition

Beginning C++ Through Game Programming, Second Edition

Date post: 19-Jan-2016
Category:
Upload: shubha
View: 64 times
Download: 8 times
Share this document with a friend
Description:
Beginning C++ Through Game Programming, Second Edition. by Michael Dawson. Chapter 10. Inheritance and Polymorphism: Blackjack. Objectives. Derive one class from another Use inherited data members and member functions Override base class member functions - PowerPoint PPT Presentation
Popular Tags:
40
Beginning C++ Through Game Programming, Second Edition by Michael Dawson
Transcript
Page 11: Beginning C++ Through Game Programming, Second Edition

Controlling Access under Inheritance

• When you derive one class from another, you can control how much access the derived class has to the base class’ members

• Generally want to provide only as much access as is necessary to a base class’ members in a derived class• Use access modifiers public, protected, and private

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

Access Modifiers with Class Members

• public members are accessible to all code in a program• protected members are accessible only in their own class and certain derived classes, depending upon the access level used in

inheritance• private members are only accessible in their own class, which means they are not directly accessible in any kind of derived class

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

Access Modifiers When Deriving Classes

• Use an access modifier when deriving a classclass Boss : public Enemy

• public derivation – public members in the base class become public members in the derived class – protected members in the base class become protected members in the derived class – private members are inaccessible

• Can derive a new class with the protected and private keywords, but they’re rarely used

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

Calling Base Class Constructors

• Can also explicitly call a base class constructor from a derived class constructor• Do this in the Boss constructor, which says to explicitly call the Enemy constructor and pass it damage Boss(int damage = 30):

Enemy(damage) • Pass the Enemy constructor the value that gets assigned to m_Damage

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

Overloaded Assignment Operator in a Derived Class:

Example• Boss derived from Enemy, overloaded assignment operator member function defined in Boss could start as follows:

Boss& operator=(const Boss& b){ //handles data members inherited from Enemy Enemy::operator=(b); //now take care of data members in Boss

• Explicit call to Enemy assignment operator member function handles the data members inherited from Enemy • Rest of the member function would take care of the data members defined in Boss

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

Copy Constructor in a Derived Class: Example

• If Boss is derived from Enemy, the copy constructor defined in Boss could start as follows:

//handles data members inherited from EnemyBoss (const Boss& b): Enemy(b){ //take care of data members defined in Boss

• Calling Enemy copy constructor with Enemy(b) copies Enemy object's data members into the new Boss object • In remainder of Boss copy constructor, take care of copying the data members declared in Boss for the new object

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

Polymorphism

• One of the pillars of OOP• Member function will produce different results depending on the type of object for which it is being called • Example:

– A group of bad guys is made of objects of different types that are related through inheritance, such as enemies and bosses– You call the same member function for each bad guy in the group,like Attack(), and each object would determine the exact effects– Effect of the function calls is dynamic and is determined at runtime, depending on the object type

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

Virtual Member Functions

• Virtual member functions allow for polymorphic behavior

• If VTaunt() is defined as virtual in Enemy and overridden in Boss, then pBadGuy->VTaunt();

• Executes Boss VTaunt() member function• If VTaunt() had not been virtual in Enemy,

then even if Boss had overridden the member function, Enemy VTaunt() would have been called through the Boss object

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

Virtual Member Function: Tips

• Once a member function is defined as virtual, it’s virtual in any derived class• Don’t have to use the keyword virtual when you override a virtual member function in a derived class, but you should use it anyway because it will remind you that the function is indeed virtual• Virtual functions produce polymorphic behavior through references as well pointers• Benefits of virtual functions aren't free; there is a performance cost associated with the overhead.• Use virtual functions only when you need them • Once you’ve defined one virtual member function in a class, defining another in that class doesn’t cost you much more

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

Slicing Objects

• Can use a pointer or reference to the base class to point to an object of the derived class• Assigning an object of a derived class to a variable of a base class slices the object, losing the data

members declared in the derived class and losing access to member functions of the derived class • Avoid slicing objects

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

Virtual Destructors• When you use a pointer to a base class to point to an object of a derived class, you have a potential problem• When you delete the pointer, only the base class’ destructor will be called for the object • Solution is to make the base class’ destructor virtual • Then derived class’ destructor is called, which leads to base class’ destructor being called• Make a destructor virtual with virtual keyword virtual ~Enemy()

• Good rule of thumb: if you have any virtual member functions in a class, make the destructor virtual, too.

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

Abstract Classes

• A class that can’t be used to instantiate an object

• A generic class upon which to base other classes

• Example: a generic Creature class upon which classes Pixie, Dragon, and Orc are based

• Doesn't make sense to instantiate Creature objects, but pixies, dragons, and orcs have common elements

• Creature would be an abstract class

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

Pure Virtual Functions• A pure virtual function is one to which you don’t need to give a definition• Specify a pure virtual function by placing an equal sign and a zero at the end of the function header of a virtual function • In a Creature class, Greet() is a pure virtual function: virtual void Greet() const = 0;

• When a class contains at least one pure virtual function, it’s an abstract class• An abstract class can have data members and can have virtual functions that are not pure virtual

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

Deriving a Class from an Abstract Class

• When you derive a new class from an abstract class, you can override its pure virtual functions• If you override all of its pure virtual functions, then the new class is not abstract; you can instantiate objects from it. • In an Orc class derived from Creature: virtual void Greet() const { cout << "The orc grunts hello.\n"; }

• Orc is not abstract, and objects can be instantiated from it

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

Summary• Inheritance allows you to derive a new class from an existing one • A derived class automatically inherits data members and member functions from base class• A derived class does not inherit constructors, copy constructors, destructors, or an overloaded assignment operator• Base class constructors are automatically called before the derived class constructor when a derived class object is instantiated• Base class destructors are automatically called after the derived class destructor when a derived class object is destroyed

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

Summary (cont.)• protected members are accessible only in their own class and certain derived classes, depending upon the derivation access level• Using public derivation means that public members in the base class become public members in the derived class, protected members in the base class become

protected members in the derived class, and private members are inaccessible• Can override base class member functions by giving them new definitions in a derived class• Can explicitly call a base class member function from a derived class• Can explicitly call the base class constructor from a derived class instructor

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

Summary (cont.)• Polymorphism is the quality whereby a member function will produce different results depending on the type of object for which it is called • Virtual functions allow for polymorphic behavior • A pure virtual function is a function to which you don’t need to give a definition • An abstract class has at least one pure virtual member function• An abstract class can’t be used to instantiate an object


Recommended