< Previous | Contents | Next >

Inheritance and Polymorphism: Blackjack


Classes give you the perfect way to represent game entities that have attributes and behaviors. But game entities are often related. In this chapter, youll learn about inheritance and polymorphism, which give you ways to express those connections and can make defining and using classes even simpler and more intuitive. Specifically, youll learn to:

n Derive one class from another

n Use inherited data members and member functions

n Override base class member functions

n Define virtual functions to enable polymorphism

n Declare pure virtual functions to define abstract classes


 

Introducing InheritanceIntroducing the Simple Boss ProgramDeriving from a Base ClassInstantiating Objects from a Derived ClassUsing Inherited MembersControlling Access under InheritanceIntroducing the Simple Boss 2.0 ProgramUsing Access Modifiers with Class MembersUsing Access Modifiers When Deriving ClassesCalling and Overriding Base Class Member FunctionsIntroducing the Overriding Boss ProgramCalling Base Class ConstructorsDeclaring Virtual Base Class Member FunctionsOverriding Virtual Base Class Member FunctionsCalling Base Class Member FunctionsUsing Overloaded Assignment Operators and Copy Constructors in Derived ClassesIntroducing PolymorphismIntroducing the Polymorphic Bad Guy ProgramUsing Base Class Pointers to Derived Class ObjectsDefining Virtual DestructorsUsing Abstract ClassesIntroducing the Abstract Creature ProgramDeclaring Pure Virtual FunctionsDeriving a Class from an Abstract ClassIntroducing the Blackjack GameDesigning the ClassesPlanning the Game LogicThe Card ClassThe Hand ClassThe GenericPlayer ClassThe Player ClassThe House ClassThe Deck ClassThe Game ClassThe main() FunctionOverloading the operator<<() FunctionSummaryQuestions and AnswersDiscussion QuestionsExercises