/
Inheritance and Polymorphism Inheritance and Polymorphism

Inheritance and Polymorphism - PowerPoint Presentation

tawny-fly
tawny-fly . @tawny-fly
Follow
345 views
Uploaded On 2019-11-21

Inheritance and Polymorphism - PPT Presentation

Inheritance and Polymorphism Data Structures and Algorithms CS 244 Brent M Dingle PhD Department of Mathematics Statistics and Computer Science University of Wisconsin Stout Based on the book Data Structures and Algorithms in C Goodrich ID: 766176

enemy boss damage class boss enemy class damage cout attack const int void virtual damagemultiplier public base pointer destructor

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Inheritance and Polymorphism" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Inheritanceand Polymorphism Data Structures and AlgorithmsCS 244 Brent M. Dingle , Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin – Stout Based on the book: Data Structures and Algorithms in C++ (Goodrich, Tamassia , Mount) Some content derived/taken from: http://www.stroustrup.com/Programming / and some from C++ Through Game Programming (Dawson)

Things to Note Homework 4 is Due SoonHomework 5 is Posted on D2LDo NOT delay in starting itDo not forget to look at the Meta-Info files

From Last Time Making friends and overloading functions in your classes (FriendCritter)Dynamically Allocating MemoryMemory Leaks Using new and delete [] with arrays Classes and Dynamic Memory Allocation Summary More on Classes Constructors and Destructors with Dynamic Memory Overloading Constructors (copy constructor) Overloading the Assignment Operator Graded In-Class Activity: HeapDataMember

For Today ReviewUML HierarchyInheritance and PolymorphismSome Review and Some New stuffDerive one class from anotherUse inherited data members and member functionsOverride base class member functions Define virtual functions to enable Polymorphism Glance at pure virtual functions to define abstract classes

Marker Slide Any General Questions ?Next upUML Hierarchy (review)C++ Inheritance (review) Inheritance (new)

Recall: Class Activity – Vehicles Looked like:Pick-Up Truck Car Vehicle Van

Recall: UML Hierarchy And we extrapolated to:UML allows for one class to be a generalization of anotherThis is called an “is a” relationshipFor example, in the below a Student “is a” SimplePerson SimplePerson - m_nameStr : string - m_idStr string + SimplePerson ( const string& nameStr , const string& idStr); + ~SimplePerson() + print() : void + getName() : stringFriend functions + bool operator==(const SimplePerson& lhs, const SimplePerson& rhs) Student - m_major: string - m_gradYear: int + Student(const string& sname, const string& sid, const string& smajor, int year); + ~Student() + print(): void + changeMajor(string newMajor) :void Aside: This does not mean students are simple

Recall: C++ Hierarchy From UML Hierarchy we went to:The Object-Oriented paradigm, which C++ followsallows for hierarchy in its classes via inheritance In general you will develop a base class (or parent class) such as: SimplePerson From that base class you will derive a subclass (or child class) such as: Student A base class may have multiple subclasses such as: Student, Instructor, Administrator

Marker Slide Any questions on:UML Hierarchy (review)Next upC++ Inheritance (review) Inheritance (new)

Recall: C++ Inheritance Subclasses are said to specialize or extend a base classSubclasses do NOT need to re-implement functions defined in the base class as the subclass inherits themSubclasses can re-implement base class functions but should not need to do so Subclasses should define and declare functions that make it a specialization of the base class

Recall: Class Activity – class Student Create a C++ class Student derived from SimplePersonUse the code in the above slides as appropriatemay look on D2L also for some starter code Be sure to create a main() routine in a file named PersonTest.cpp to test out your new classes Extra Challenge Instead of using g ++ PersonTest.cpp SimplePerson.cpp Student.cpp Create a makefile to compile and build this program Example on D2L likely (may be in general section or examples)SimplePerson - m_nameStr: string - m_idStr string + SimplePerson(const string& nameStr, const string& idStr); + ~SimplePerson() + print() : void + getName() : stringStudent - m_major: string - m_gradYear: int + Student(const string& sname, const string& sid, const string& smajor, int year); + ~ Student () + print(): void + changeMajor(const string& newMajor): void

Marker Slide Any questions on:UML Hierarchy (review)C++ Inheritance (review)Next upInheritance (new ) Simple Boss Overriding Boss

Definition: Inheritance Inheritance allows you to derive a new class from an existing oneThe new class is a subclass, or child, of the existing classThe new class automatically inherits the data members and the member functions of an existing class

Example Use Inheritance is useful when there is a need for specialized versions of an existing classExample:class Enemy hasmember variable: m_Damagemember function: Attack() Need a “Boss” type enemy that is tougher Can extend class Enemy and add: a m_DamageMultiplier member variable a SpecialAttack () member function m_DamageMultiplier

In-Class Activity: SimpleBossTake a couple minutes to download the example from D2LFile: EX065_SimpleBoss.cpp Sample run should look like: Code Discussion/walkthrough follows

Looking at Simple Boss // ---------------------------------------------------------------------// Simple Boss // Demonstrates inheritance // --------------------------------------------------------------------- #include < iostream > using namespace std ; // --------------------------------------------------------------------- // Declaration of class Enemy // This would normally be in the file Enemy.h // --------------------------------------------------------------------- class Enemy { public: int m_Damage; Enemy(); void Attack() const;};Enemy::Enemy(): m_Damage(10){ // nothing to do, the above instantiation list // already initialized m_Damage = 10}// ---------------------------------------------------------------------void Enemy::Attack() const { cout << "Attack inflicts " << m_Damage << " damage points!\n";}Our typical includes and using namespace

Looking at Simple Boss // ---------------------------------------------------------------------// Simple Boss // Demonstrates inheritance // --------------------------------------------------------------------- #include < iostream > using namespace std ; // --------------------------------------------------------------------- // Declaration of class Enemy // This would normally be in the file Enemy.h // --------------------------------------------------------------------- class Enemy { public: int m_Damage; Enemy(); void Attack() const;};Enemy::Enemy(): m_Damage(10){ // nothing to do, the above instantiation list // already initialized m_Damage = 10}// ---------------------------------------------------------------------void Enemy::Attack() const { cout << "Attack inflicts " << m_Damage << " damage points!\n";}Declarationof class Enemy1 public member variableConstructor1 public member function

Looking at Simple Boss // ---------------------------------------------------------------------// Simple Boss // Demonstrates inheritance // --------------------------------------------------------------------- #include < iostream > using namespace std ; // --------------------------------------------------------------------- // Declaration of class Enemy // This would normally be in the file Enemy.h // --------------------------------------------------------------------- class Enemy { public: int m_Damage; Enemy(); void Attack() const;};Enemy::Enemy(): m_Damage(10){ // nothing to do, the above instantiation list // already initialized m_Damage = 10}// ---------------------------------------------------------------------void Enemy::Attack() const { cout << "Attack inflicts " << m_Damage << " damage points!\n";}Constructor function of class EnemyNotice the instantiation list : m_Damage(10)This initializes m_Damage = 10Same Effect As: Enemy::Enemy() { m_Damage = 10; }

Looking at Simple Boss // ---------------------------------------------------------------------// Simple Boss // Demonstrates inheritance // --------------------------------------------------------------------- #include < iostream > using namespace std ; // --------------------------------------------------------------------- // Declaration of class Enemy // This would normally be in the file Enemy.h // --------------------------------------------------------------------- class Enemy { public: int m_Damage; Enemy(); void Attack() const;};Enemy::Enemy(): m_Damage(10){ // nothing to do, the above instantiation list // already initialized m_Damage = 10}// ---------------------------------------------------------------------void Enemy::Attack() const { cout << "Attack inflicts " << m_Damage << " damage points!\n";}Enemy’s Attack functionThe const implies no values are changed in this function (i.e. constant)

Looking at Simple Boss // ---------------------------------------------------------------------// Declaration of class Boss // Normally this would be done in file: Boss.h // Notice it is derived from the class Enemy // --------------------------------------------------------------------- class Boss : public Enemy { public: int m_DamageMultiplier ; Boss(); void SpecialAttack () const ;};// ---------------------------------------------------------------------// Implementation of the class Boss// Normally this would be done in the file: Boss.cpp// ---------------------------------------------------------------------Boss::Boss(): m_DamageMultiplier(3){ // nothing to do here. The above instantiation list // already initialized m_DamageMultiplier = 3}// ---------------------------------------------------------------------// ---------------------------------------------------------------------void Boss::SpecialAttack() const{ cout << "Special Attack inflicts " << (m_DamageMultiplier * m_Damage); cout << " damage points!\n";}Declaration of class Boss derived from class Enemy and public things stay publicclass Boss inherits m_Damage and Attack() from class enemy

Looking at Simple Boss // ---------------------------------------------------------------------// Declaration of class Boss // Normally this would be done in file: Boss.h // Notice it is derived from the class Enemy // --------------------------------------------------------------------- class Boss : public Enemy { public: int m_DamageMultiplier ; Boss(); void SpecialAttack () const ;};// ---------------------------------------------------------------------// Implementation of the class Boss// Normally this would be done in the file: Boss.cpp// ---------------------------------------------------------------------Boss::Boss(): m_DamageMultiplier(3){ // nothing to do here. The above instantiation list // already initialized m_DamageMultiplier = 3}// ---------------------------------------------------------------------// ---------------------------------------------------------------------void Boss::SpecialAttack() const{ cout << "Special Attack inflicts " << (m_DamageMultiplier * m_Damage); cout << " damage points!\n";}Declaration of class Boss derived from class Enemy and public things stay publicclass Boss inherits m_Damage and Attack() from class enemyIt extends the class by adding the membersm_DamageMultiplier and SpecialAttack()

Looking at Simple Boss // ---------------------------------------------------------------------// Declaration of class Boss // Normally this would be done in file: Boss.h // Notice it is derived from the class Enemy // --------------------------------------------------------------------- class Boss : public Enemy { public: int m_DamageMultiplier ; Boss(); void SpecialAttack () const ;};// ---------------------------------------------------------------------// Implementation of the class Boss// Normally this would be done in the file: Boss.cpp// ---------------------------------------------------------------------Boss::Boss(): m_DamageMultiplier(3){ // nothing to do here. The above instantiation list // already initialized m_DamageMultiplier = 3}// ---------------------------------------------------------------------// ---------------------------------------------------------------------void Boss::SpecialAttack() const{ cout << "Special Attack inflicts " << (m_DamageMultiplier * m_Damage); cout << " damage points!\n";}Declaration of class Boss derived from class Enemy and public things stay publicclass Boss inherits m_Damage and Attack() from class enemyIt extends the class by adding the membersm_DamageMultiplier and SpecialAttack()Public Service MessageConstructorsCopy Constructors Destructors Overloaded assignment operators are NOT inherited from the base class

Looking at Simple Boss // ---------------------------------------------------------------------// Declaration of class Boss // Normally this would be done in file: Boss.h // Notice it is derived from the class Enemy // --------------------------------------------------------------------- class Boss : public Enemy { public: int m_DamageMultiplier ; Boss(); void SpecialAttack () const ;};// ---------------------------------------------------------------------// Implementation of the class Boss// Normally this would be done in the file: Boss.cpp// ---------------------------------------------------------------------Boss::Boss(): m_DamageMultiplier(3){ // nothing to do here. The above instantiation list // already initialized m_DamageMultiplier = 3}// ---------------------------------------------------------------------// ---------------------------------------------------------------------void Boss::SpecialAttack() const{ cout << "Special Attack inflicts " << (m_DamageMultiplier * m_Damage); cout << " damage points!\n";}Constructor of the Boss class.It too has an instantiation listWhich initializes m_DamageMultiplier = 3But what is the value of m_Damage?How?

Looking at Simple Boss // ---------------------------------------------------------------------// Declaration of class Boss // Normally this would be done in file: Boss.h // Notice it is derived from the class Enemy // --------------------------------------------------------------------- class Boss : public Enemy { public: int m_DamageMultiplier ; Boss(); void SpecialAttack () const ;};// ---------------------------------------------------------------------// Implementation of the class Boss// Normally this would be done in the file: Boss.cpp// ---------------------------------------------------------------------Boss::Boss(): m_DamageMultiplier(3){ // nothing to do here. The above instantiation list // already initialized m_DamageMultiplier = 3}// ---------------------------------------------------------------------// ---------------------------------------------------------------------void Boss::SpecialAttack() const{ cout << "Special Attack inflicts " << (m_DamageMultiplier * m_Damage); cout << " damage points!\n";}Constructor of the Boss class.It too has an instantiation listWhich initializes m_DamageMultiplier = 3But what is the value of m_Damage?How?Side Point ExerciseTry the following code/program out and see what happens:struct A{ A() {cout << "A() C-tor" << endl;} ~ A(){ cout << "~A() D-tor" << endl ;} }; struct B : public A { B (){ cout << "B() C-tor" << endl ;} ~ B(){ cout << "~B() D-tor" << endl ;} }; int main () { B b ; }

Looking at Simple Boss // ---------------------------------------------------------------------// Declaration of class Boss // Normally this would be done in file: Boss.h // Notice it is derived from the class Enemy // --------------------------------------------------------------------- class Boss : public Enemy { public: int m_DamageMultiplier ; Boss(); void SpecialAttack () const ;};// ---------------------------------------------------------------------// Implementation of the class Boss// Normally this would be done in the file: Boss.cpp// ---------------------------------------------------------------------Boss::Boss(): m_DamageMultiplier(3){ // nothing to do here. The above instantiation list // already initialized m_DamageMultiplier = 3}// ---------------------------------------------------------------------// ---------------------------------------------------------------------void Boss::SpecialAttack() const{ cout << "Special Attack inflicts " << (m_DamageMultiplier * m_Damage); cout << " damage points!\n";}Side Point ExerciseTry the following code/program out and see what happens:struct A{ A() {cout << "A() C-tor" << endl;} ~A(){cout << "~A() D-tor" << endl;} }; struct B : public A{ B(){cout << "B() C-tor" << endl;} ~B(){cout << "~B() D-tor" << endl;} };int main(){ B b; } struct A has a “default” constructor i.e. one with no parameters struct B’s constructor will automatically call the default constructor for A prior to running its own constructor.

Looking at Simple Boss // ---------------------------------------------------------------------// Declaration of class Boss // Normally this would be done in file: Boss.h // Notice it is derived from the class Enemy // --------------------------------------------------------------------- class Boss : public Enemy { public: int m_DamageMultiplier ; Boss(); void SpecialAttack () const ;};// ---------------------------------------------------------------------// Implementation of the class Boss// Normally this would be done in the file: Boss.cpp// ---------------------------------------------------------------------Boss::Boss(): m_DamageMultiplier(3){ // nothing to do here. The above instantiation list // already initialized m_DamageMultiplier = 3}// ---------------------------------------------------------------------// ---------------------------------------------------------------------void Boss::SpecialAttack() const{ cout << "Special Attack inflicts " << (m_DamageMultiplier * m_Damage); cout << " damage points!\n";}Side Point ExerciseTry the following code/program out and see what happens:struct A{ A() {cout << "A() C-tor" << endl;} ~A(){cout << "~A() D-tor" << endl;} }; struct B : public A{ B(){cout << "B() C-tor" << endl;} ~B(){cout << "~B() D-tor" << endl;} };int main(){ B b; } struct A has a “default” constructor i.e. one with 0 parameters struct B’s constructor will automatically call the default constructor for A prior to running its own constructor. And we see that in the output below

Looking at Simple Boss // ---------------------------------------------------------------------// Declaration of class Boss // Normally this would be done in file: Boss.h // Notice it is derived from the class Enemy // --------------------------------------------------------------------- class Boss : public Enemy { public: int m_DamageMultiplier ; Boss(); void SpecialAttack () const ;};// ---------------------------------------------------------------------// Implementation of the class Boss// Normally this would be done in the file: Boss.cpp// ---------------------------------------------------------------------Boss::Boss(): m_DamageMultiplier(3){ // nothing to do here. The above instantiation list // already initialized m_DamageMultiplier = 3}// ---------------------------------------------------------------------// ---------------------------------------------------------------------void Boss::SpecialAttack() const{ cout << "Special Attack inflicts " << (m_DamageMultiplier * m_Damage); cout << " damage points!\n";}Side Point ExerciseTry the following code/program out and see what happens:struct A{ A() {cout << "A() C-tor" << endl;} ~A(){cout << "~A() D-tor" << endl;} }; struct B : public A{ B(){cout << "B() C-tor" << endl;} ~B(){cout << "~B() D-tor" << endl;} };int main(){ B b; } The destructors are called in reverse order. When B’s destructor completes then A’s is called automatically. Think house construction: A = foundation, B = first floor Create house = build foundation, then 1 st floor Destroy house = dest 1 st floor, then foundation

Looking at Simple Boss // ---------------------------------------------------------------------// Declaration of class Boss // Normally this would be done in file: Boss.h // Notice it is derived from the class Enemy // --------------------------------------------------------------------- class Boss : public Enemy { public: int m_DamageMultiplier ; Boss(); void SpecialAttack () const ;};// ---------------------------------------------------------------------// Implementation of the class Boss// Normally this would be done in the file: Boss.cpp// ---------------------------------------------------------------------Boss::Boss(): m_DamageMultiplier(3){ // nothing to do here. The above instantiation list // already initialized m_DamageMultiplier = 3}// ---------------------------------------------------------------------// ---------------------------------------------------------------------void Boss::SpecialAttack() const{ cout << "Special Attack inflicts " << (m_DamageMultiplier * m_Damage); cout << " damage points!\n";}But what is the value of m_Damage?How?So value of m_Damage is set to 10because Enemy’s default constructor is automatically called by Boss’s constructor before it executes its own body of code.

Looking at Simple Boss // ---------------------------------------------------------------------// Declaration of class Boss // Normally this would be done in file: Boss.h // Notice it is derived from the class Enemy // --------------------------------------------------------------------- class Boss : public Enemy { public: int m_DamageMultiplier ; Boss(); void SpecialAttack () const ;};// ---------------------------------------------------------------------// Implementation of the class Boss// Normally this would be done in the file: Boss.cpp// ---------------------------------------------------------------------Boss::Boss(): m_DamageMultiplier(3){ // nothing to do here. The above instantiation list // already initialized m_DamageMultiplier = 3}// ---------------------------------------------------------------------// ---------------------------------------------------------------------void Boss::SpecialAttack() const{ cout << "Special Attack inflicts " << (m_DamageMultiplier * m_Damage); cout << " damage points!\n";}SpecialAttack functionagain const implies no values will change in this function (i.e constant)

Looking at Simple Boss // ---------------------------------------------------------------------// main function // Normally this would be found in a separate . cpp file // perhaps named BossTest.cpp // --------------------------------------------------------------------- int main() { cout << "Creating an enemy.\n"; Enemy enemy1; enemy1.Attack (); cout << "\ nCreating a boss.\n"; Boss boss1; boss1.Attack(); boss1.SpecialAttack(); return 0;}The main() functionCreates a variable of type EnemyAnd calls its Atttack() function

Looking at Simple Boss // ---------------------------------------------------------------------// main function // Normally this would be found in a separate . cpp file // perhaps named BossTest.cpp // --------------------------------------------------------------------- int main() { cout << "Creating an enemy.\n"; Enemy enemy1; enemy1.Attack (); cout << "\ nCreating a boss.\n"; Boss boss1; boss1.Attack(); boss1.SpecialAttack(); return 0;}The main() functionIt then creates a variable of type Bossand calls its Attack and SpecialAttack functions

Marker Slide Any questions on:UML Hierarchy (review)C++ Inheritance (review)Inheritance (new)Simple Boss Next up Inheritance (new ) Overriding Boss Polymorphism (new) Polymorphic Bad Guy

In-Class Activity: Overriding Boss Take a couple minutes to download the example from D2LFile: EX069_OverridingBoss.cppSample run should look like: Code Discussion/walkthrough follows

Looking at Overriding Boss //Overriding Boss//Demonstrates calling and overriding base member functions #include < iostream > using namespace std ; // --------------------------------------------------------------------- // Enemy Interface Declaration // Typically found in Enemy.h , // everything is grouped together here for ease of presentation // --------------------------------------------------------------------- class Enemy { public: Enemy(int damage = 10); void virtual Taunt() const; //made virtual to be overridden void virtual Attack() const; //made virtual to be overridden private: int m_Damage;}; Standard include for IOUsing standard namespace

Looking at Overriding Boss //Overriding Boss//Demonstrates calling and overriding base member functions #include < iostream > using namespace std ; // --------------------------------------------------------------------- // Enemy Interface Declaration // Typically found in Enemy.h , // everything is grouped together here for ease of presentation // --------------------------------------------------------------------- class Enemy { public: Enemy(int damage = 10); void virtual Taunt() const; //made virtual to be overridden void virtual Attack() const; //made virtual to be overridden private: int m_Damage;}; Class EnemyConstructor sets default value for damage to 10

Looking at Overriding Boss //Overriding Boss//Demonstrates calling and overriding base member functions #include < iostream > using namespace std ; // --------------------------------------------------------------------- // Enemy Interface Declaration // Typically found in Enemy.h , // everything is grouped together here for ease of presentation // --------------------------------------------------------------------- class Enemy { public: Enemy(int damage = 10); void virtual Taunt() const; //made virtual to be overridden void virtual Attack() const; //made virtual to be overridden private: int m_Damage;}; Enemy(int damage = 10)Works like TWO constructors The first has zero parameters Enemy someGuy;The compiler will automatically change that to be the same as: Enemy someGuy(10);

Looking at Overriding Boss //Overriding Boss//Demonstrates calling and overriding base member functions #include < iostream > using namespace std ; // --------------------------------------------------------------------- // Enemy Interface Declaration // Typically found in Enemy.h , // everything is grouped together here for ease of presentation // --------------------------------------------------------------------- class Enemy { public: Enemy(int damage = 10); void virtual Taunt() const; //made virtual to be overridden void virtual Attack() const; //made virtual to be overridden private: int m_Damage;}; Enemy(int damage = 10)Works like TWO constructors The second has one parameters Enemy someGuy(88);with results as would be expected.

Looking at Overriding Boss //Overriding Boss//Demonstrates calling and overriding base member functions #include < iostream > using namespace std ; // --------------------------------------------------------------------- // Enemy Interface Declaration // Typically found in Enemy.h , // everything is grouped together here for ease of presentation // --------------------------------------------------------------------- class Enemy { public: Enemy(int damage = 10); void virtual Taunt() const; //made virtual to be overridden void virtual Attack() const; //made virtual to be overridden private: int m_Damage;}; Virtual FunctionsThe keyword virtual means these functions are intended to be overridden by subclasses.This supports polymorphism.

Looking at Overriding Boss // ---------------------------------------------------------------------// Enemy Implementation Definition // Typically found in Enemy.cpp, // everything is grouped together here for ease of presentation // --------------------------------------------------------------------- // --------------------------------------------------------------------- // Constructor // --------------------------------------------------------------------- Enemy::Enemy( int damage): m_Damage (damage) { // nothing left to do } // --------------------------------------------------------------------- // --------------------------------------------------------------------- void Enemy::Taunt() const{ cout << "The enemy says he will fight you.\n";}// ---------------------------------------------------------------------// ---------------------------------------------------------------------void Enemy::Attack() const{ cout << "Attack! Inflicts " << m_Damage << " damage points.";}Note the instantiation list :m_Damage(damage) This is the same as saying m_Damage = damage;in the body of the constructor

Looking at Overriding Boss // ---------------------------------------------------------------------// Enemy Implementation Definition // Typically found in Enemy.cpp, // everything is grouped together here for ease of presentation // --------------------------------------------------------------------- // --------------------------------------------------------------------- // Constructor // --------------------------------------------------------------------- Enemy::Enemy( int damage): m_Damage (damage) { // nothing left to do } // --------------------------------------------------------------------- // --------------------------------------------------------------------- void Enemy::Taunt() const{ cout << "The enemy says he will fight you.\n";}// ---------------------------------------------------------------------// ---------------------------------------------------------------------void Enemy::Attack() const{ cout << "Attack! Inflicts " << m_Damage << " damage points.";}Member functions with basic output lines to the screen. Recall these were declared as virtualNote the words used as they will not be the same as the soon to be revealed “Boss” class

Looking at Overriding Boss // ---------------------------------------------------------------------// --------------------------------------------------------------------- // Interface Declaration for // Boss class derived from Enemy class // Again typically this would be in Boss.h // --------------------------------------------------------------------- class Boss : public Enemy { public: Boss( int damage = 30); void virtual Taunt() const ; // optional use of keyword virtual void virtual Attack() const; //optional use of keyword virtual};class Enemy{ public: Enemy(int damage = 10); void virtual Taunt() const; //made virtual to be overridden void virtual Attack() const; // made virtual to be overridden private: int m_Damage;};Interface for Boss class derived from EnemyNotice almost same as Enemy class.Constructor initializes damage to 30virtual keyword keptNo re-declaration of member variable: m_Damage

Looking at Overriding Boss // ---------------------------------------------------------------------// ---------------------------------------------------------------------// Implementation Definition of Boss class// Again typically this would be in Boss.cpp // --------------------------------------------------------------------- // --------------------------------------------------------------------- // Boss's Constructor // --------------------------------------------------------------------- Boss::Boss( int damage): Enemy(damage) // call base class constructor with argument { } For the Boss’s constructor the instantiation list does something special: It calls the base class constructor sending it the damage parameter This is one of the few ways you can ever directly “call” a class’s constructor In thought this is same as: Boss:Boss ( int damage) { Enemy::Enemy(damage);  you should not actually do this }

Looking at Overriding Boss // ---------------------------------------------------------------------// --------------------------------------------------------------------- void Boss::Taunt() const // override base class member function { cout << "The boss says he will end your pitiful existence.\n"; } // --------------------------------------------------------------------- // --------------------------------------------------------------------- void Boss::Attack() const //override base class member function { Enemy::Attack(); //call base class member function cout << " And laughs heartily at you.\n";}Here are the overridden virtual functionsNotice the output is different than that of the Enemy functions of the same namevoid Enemy::Taunt() const{ cout << "The enemy says he will fight you.\n"; }void Enemy::Attack() const { cout << "Attack! Inflicts " << m_Damage << " damage points.";}

Looking at Overriding Boss // ---------------------------------------------------------------------// --------------------------------------------------------------------- void Boss::Taunt() const // override base class member function { cout << "The boss says he will end your pitiful existence.\n"; } // --------------------------------------------------------------------- // --------------------------------------------------------------------- void Boss::Attack() const //override base class member function { Enemy::Attack(); //call base class member function cout << " And laughs heartily at you.\n";}Other point of noteThe Boss::Attack functionfirst calls the base class’s Attack functionand then adds an extra message after itvoid Enemy::Taunt() const{ cout << "The enemy says he will fight you.\n";} void Enemy::Attack() const { cout << "Attack! Inflicts " << m_Damage << " damage points.";}

Looking at Overriding Boss // ---------------------------------------------------------------------// main - it all begins here // This would also typically be in a separate file, e.g. BossTester.cpp // --------------------------------------------------------------------- int main() { cout << "Enemy object:\n"; Enemy anEnemy ; anEnemy.Taunt (); anEnemy.Attack (); cout << "\n\nBoss object:\n"; Boss aBoss; aBoss.Taunt(); aBoss.Attack(); return 0;}The main() functionCreates an “Enemy”Creates a “Boss”Compare the output of the Taunt and Attack Functions

Marker Slide Any questions on:UML Hierarchy (review)C++ Inheritance (review)Inheritance (new)Simple Boss Overriding Boss Next up Polymorphism (new) Polymorphic Bad Guy Blackjack Game

Polymorphism Conversational definition of Polymorphism:A member function will produce different results depending on the type of object for which it is being calledExample10 bad guys face the playerEach of a different class typeCall the attack() function for each and get 10 different (class specific attacks)requires use of keyword virtual

In-Class Activity: Poly Bad Guy Take a couple minutes to download the example from D2LFile: EX072_PolymorphBadGuy.cppSample run should look like: Code Discussion/walkthrough follows

Looking at Poly Bad Guy // --------------------------------------------------------------------// Polymorphic Bad Guy// Demonstrates calling member functions dynamically // -------------------------------------------------------------------- #include < iostream > using namespace std ; // -------------------------------------------------------------------- // -------------------------------------------------------------------- class Enemy { public: Enemy(int damage = 10); virtual ~Enemy(); void virtual Attack() const; protected: int* mp_Damage;};Code setup is similar to previous exampleNote a destructor is declaredand the use of virtualand the m_Damage is now a pointernamed mp_Damage

Looking at Poly Bad Guy // --------------------------------------------------------------------// --------------------------------------------------------------------Enemy::Enemy( int damage) { mp_Damage = new int (damage); } // -------------------------------------------------------------------- // -------------------------------------------------------------------- Enemy::~Enemy() { cout << "In Enemy destructor, deleting mp_Damage.\n"; delete mp_Damage; mp_Damage = 0;}Constructor is dynamically allocating memory

Looking at Poly Bad Guy // --------------------------------------------------------------------// --------------------------------------------------------------------Enemy::Enemy( int damage) { mp_Damage = new int (damage); } // -------------------------------------------------------------------- // -------------------------------------------------------------------- Enemy::~Enemy() { cout << "In Enemy destructor, deleting mp_Damage.\n"; delete mp_Damage; mp_Damage = NULL;}Destructoris freeing/deleting the dynamically allocated memoryand setting the pointer to NULL

Looking at Poly Bad Guy // --------------------------------------------------------------------// --------------------------------------------------------------------void Enemy::Attack() const { cout << "An enemy attacks and inflicts " << * mp_Damage << " damage points." ; } Outputs the damage of the attack. Note the * dereferences the mp_Damage pointer variable

Looking at Poly Bad Guy // --------------------------------------------------------------------// --------------------------------------------------------------------class Boss : public Enemy { public : Boss( int multiplier = 3); virtual ~Boss(); void virtual Attack() const; protected: int* mp_Multiplier;};Interface Declaration for Boss classAgain has a destructorand uses the keyword virtualThe damage multiplier member variable is a pointer to type int

Looking at Poly Bad Guy // --------------------------------------------------------------------// --------------------------------------------------------------------Boss::Boss( int multiplier) { mp_Multiplier = new int (multiplier); } // -------------------------------------------------------------------- // -------------------------------------------------------------------- Boss::~Boss() { cout << "In Boss destructor, deleting mp_Multiplier.\n"; delete mp_Multiplier; mp_Multiplier = NULL;}The Boss’s constructor allocates memory for the mp_MultiplierRecall the Enemy’s constructor is automatically called prior to the running of the Boss’s constructor

Looking at Poly Bad Guy // --------------------------------------------------------------------// --------------------------------------------------------------------Boss::Boss( int multiplier) { mp_Multiplier = new int (multiplier); } // -------------------------------------------------------------------- // -------------------------------------------------------------------- Boss::~Boss() { cout << "In Boss destructor, deleting mp_Multiplier.\n"; delete mp_Multiplier; mp_Multiplier = NULL;}The Boss’s destructor frees the memory used by the mp_Multiplier pointer variable and sets it to NULL.Recall the Enemy’s destructor will be called automatically after the Boss’s destructor executes

Looking at Poly Bad Guy void Boss::Attack() const { cout << "A boss attacks and inflicts " << (* mp_Damage ) * (* mp_Multiplier ) << " damage points." ; } The Boss’s attack function Notice the output is significantly different than that of a basic Enemy Note also the dereferencing of the pointer variables using *

Looking at Poly Bad Guy // --------------------------------------------------------------------// main - it all begins here// -------------------------------------------------------------------- int main() { cout << "Calling Attack() on Boss object through pointer to Enemy:\n" ; Enemy* pBadGuy = new Boss(); pBadGuy ->Attack(); cout << "\n\nDeleting pointer to Enemy:\n"; delete pBadGuy; pBadGuy = NULL; return 0;}We declare a pointer to class Enemy

Looking at Poly Bad Guy // --------------------------------------------------------------------// main - it all begins here// -------------------------------------------------------------------- int main() { cout << "Calling Attack() on Boss object through pointer to Enemy:\n" ; Enemy* pBadGuy = new Boss(); pBadGuy ->Attack(); cout << "\n\nDeleting pointer to Enemy:\n"; delete pBadGuy; pBadGuy = NULL; return 0;}We declare a pointer to class EnemyBut we allocate a class Boss

Looking at Poly Bad Guy // --------------------------------------------------------------------// main - it all begins here// -------------------------------------------------------------------- int main() { cout << "Calling Attack() on Boss object through pointer to Enemy:\n" ; Enemy* pBadGuy = new Boss(); pBadGuy ->Attack(); cout << "\n\nDeleting pointer to Enemy:\n"; delete pBadGuy; pBadGuy = NULL; return 0;}We declare a pointer to class EnemyBut we allocate a class BossSo which Attack() function executes here?

Looking at Poly Bad Guy // --------------------------------------------------------------------// main - it all begins here// -------------------------------------------------------------------- int main() { cout << "Calling Attack() on Boss object through pointer to Enemy:\n" ; Enemy* pBadGuy = new Boss(); pBadGuy ->Attack(); cout << "\n\nDeleting pointer to Enemy:\n"; delete pBadGuy; pBadGuy = NULL; return 0;}We declare a pointer to class EnemyBut we allocate a class BossSo which Attack() function executes here?By Polymorphism and the use of the keyword virtualBoss’s Attack function will “magically” be executed

Looking at Poly Bad Guy // --------------------------------------------------------------------// main - it all begins here// -------------------------------------------------------------------- int main() { cout << "Calling Attack() on Boss object through pointer to Enemy:\n" ; Enemy* pBadGuy = new Boss(); pBadGuy ->Attack(); cout << "\n\nDeleting pointer to Enemy:\n"; delete pBadGuy; pBadGuy = NULL; return 0;} A potential problem exists when you use a pointer to a base class to point to an object of a derived class.When you delete the pointer, only the base class’ destructor will be called for the object. This could lead to disastrous resultsbecause the derived class’ destructor might need to free memory (as the destructor for Boss does). The solution is to make the base class’ destructor virtual. That way, the derived class’ destructor is called, which (as always) leads to the calling the base class’ destructor, giving every class the chance to clean up after itself.

Looking at Poly Bad Guy // --------------------------------------------------------------------// main - it all begins here// -------------------------------------------------------------------- int main() { cout << "Calling Attack() on Boss object through pointer to Enemy:\n" ; Enemy* pBadGuy = new Boss(); pBadGuy ->Attack(); cout << "\n\nDeleting pointer to Enemy:\n"; delete pBadGuy; pBadGuy = NULL; return 0;} A potential problem exists when you use a pointer to a base class to point to an object of a derived class.When you delete the pointer, only the base class’ destructor will be called for the object. (in this case Enemy is the base class) This could lead to disastrous resultsbecause the derived class’ destructor might need to free memory (as the destructor for Boss does). The solution is to make the base class’ destructor virtual. That way, the derived class’ destructor is called, which (as always) leads to the calling the base class’ destructor, giving every class the chance to clean up after itself.

Looking at Poly Bad Guy // --------------------------------------------------------------------// main - it all begins here// -------------------------------------------------------------------- int main() { cout << "Calling Attack() on Boss object through pointer to Enemy:\n" ; Enemy* pBadGuy = new Boss(); pBadGuy ->Attack(); cout << "\n\nDeleting pointer to Enemy:\n"; delete pBadGuy; pBadGuy = NULL; return 0;} A potential problem exists when you use a pointer to a base class to point to an object of a derived class.When you delete the pointer, only the base class’ destructor will be called for the object. This could lead to disastrous resultsbecause the derived class’ destructor might need to free memory (as the destructor for Boss does). The solution is to make the base class’ destructor virtual. That way, the derived class’ destructor is called, which (as always) leads to the calling the base class’ destructor, giving every class the chance to clean up after itself.class Enemy{ public : Enemy( int damage = 10); virtual ~Enemy(); void virtual Attack() const ; protected : int * mp_Damage ; };

Looking at Poly Bad Guy // --------------------------------------------------------------------// main - it all begins here// -------------------------------------------------------------------- int main() { cout << "Calling Attack() on Boss object through pointer to Enemy:\n" ; Enemy* pBadGuy = new Boss(); pBadGuy ->Attack(); cout << "\n\nDeleting pointer to Enemy:\n"; delete pBadGuy; pBadGuy = NULL; return 0;} A potential problem exists when you use a pointer to a base class to point to an object of a derived class. When you delete the pointer, only the base class’ destructor will be called for the object. This could lead to disastrous resultsbecause the derived class’ destructor might need to free memory (as the destructor for Boss does). The solution is to make the base class’ destructor virtual. That way, the derived class’ destructor is called, which leads to the calling of the base class’ destructor, giving every class the chance to clean up after itself.

Independent Activity: Poly Bad Guy On your ownTo see what the keyword virtual ‘really’ does:Take the previous programRemove the keyword virtual from everywhere it appears See what changes

Independent Activity: Pure Virtual On your ownAbstract ClassPure Virtual FunctionsDownload and play with EX074_AbstractCreature.cpp

Marker Slide Any questions on:UML Hierarchy (review)C++ Inheritance (review)Inheritance (new)Simple Boss Overriding Boss Polymorphism (new) Polymorphic Bad Guy Next up Program Design Blackjack Game

Blackjack Game Standard game of Blackjackalso called 21ObjectObtain the set of cards closest to 21 without going over

Blackjack Game Classes Useful C++ classes to have around for the game

Classes: Inheritance Relations Generic player is shaded to indicate it is an abstract classi.e. all virtual functions, base class only, not intended to be instantiated

Classes: More Details Cards:real-life cardsdon’t copy when you deal it from he deck, move it to a handallocate them on the heap (free store)Handvector of pointers to Card objectscards moving from hand to hand are really pointers that are being copied and destroyed

Classes: More Details PlayersHumans and ComputerEach really is a “Hand”Computer will be the dealer (aka House)which has unique rules to determine “hit” or “stay”Humans will be playersrequires human input for “hit” or “stay” Deck Similar to a hand Starts as all 52 cards (randomly ordered) Needs to be able to “deal” cards to player and the house objects

Card Class

Hand Class

GenericPlayer (abstract) Class

Player Class

House Class

Deck Class

Game Class

Game Flow Deal players and the house 2 initial cardsHide the house’s first card (facedown)Display players’ and house’s handsDeal additional cards to playersReveal House’s first cardDeal additional cards to houseIf House busted Everyone who is not busted wins Else Each non-busted player above house’s total wins Players with total over 21, or less than equal to house lose

main() function of Blackjack Game int main(){ cout << "\t\ tWelcome to Blackjack!\n\n" ; int numPlayers = 0; while ( numPlayers < 1 || numPlayers > 7) { cout << "How many players? (1 - 7): "; cin >> numPlayers; } vector<string> names; string name; for (int i = 0; i < numPlayers; ++i) { cout << "Enter player name: "; cin >> name; names.push_back(name); } cout << endl; // the game loop Game aGame(names); char again = 'y'; while (again != 'n' && again != 'N') { aGame.Play(); cout << "\nDo you want to play again? (Y/N): "; cin >> again; } return 0;}

Marker Slide Any Questions on:Any questions on:UML Hierarchy (review)C++ Inheritance (review) Inheritance (new) Simple Boss Overriding Boss Polymorphism (new) Polymorphic Bad Guy Program Design Blackjack Game Next up Free Play --- and an in-class assignment

Graded In-Class Activity: Blackjack Implement and run a Blackjack program based on the design just discussedStarter code (and one possible solution) is on D2LDownload the file:ICA020_broken_Blackjack.cpp Rename it: Blackjack.cpp Fix the program as indicated Put your name and today’s date in the lead comments Compile it Run it When satisfied with it (or class ends) upload the resulting FIXED code to the appropriate D2L dropbox You can work in groups BUT each individual must submit something to D2L You may create a set of source files and possibly a makefile to solve this problem, but it is not required

Free Play – Things to Work OnGraded In-Class Activity:Homework 4Homework 5 Various In-Class Activities to revisit

The End Or is it?