COMP_SCI 211 (Fall 2022) Lecture 16: C++ inheritance + Building a game SRUTI BHAGAVATULA 11/10/2022 1 2 Administrivia u u HW 5 due Tuesday u Reminder that extra time is meant to be used u Not recommended to delay – haven’t seen much on Piazza u Need to get started on your projects immediately after Proposals are due tomorrow!! u u Only seen a 14 proposals so far Projects meant to be started immediately after HW5 u Wed-Fri of next week (can start earlier once approved) u Mon-Wed of Thanksgiving week u All of last week of classes 3 Today’s Goals u Introduce concept of inheritance for C++ classes u Develop Tic-Tac-Toe game plan and start implementing from scratch 4 Inheritance in C++ 5 Consider the game Super Mario u Many characters: Mario, Luigi, Peach, Bowser u Each has special powers and behavior 6 How they might be defined class Mario { class Bowser { private: Posn<int> position; double height; double weight; bool flower_power; bool invincible; int lives_left; } private: Posn<int> position; double height; double weight; bool spitting_fire; bool defeated_mario; double poison_gas_left; } 7 What if we took out the common fields? u We can do this through inheritance! Parent class (has all common fields) Child class (has extra fields specific to child) Child class (has extra fields specific to child) class Character { Base class! public: Character(); void display(); Parent members are private • children can’t access them class Mario: public Character { private: bool flower_power; bool invincible; int lives_left; } 8 private: Posn<int> position; double height; double weight; }; class Bowser: public Character { private: bool spitting_fire; bool defeated_mario double poison_gas_left } class Character { Base class! public: Character(); void display(); Change to protected • children can now access them • equivalent to private outside of children class Mario: public Character { private: bool flower_power; bool invincible; int lives_left; } 9 protected: Posn<int> position; double height; double weight; }; class Bowser: public Character { private: bool spitting_fire; bool defeated_mario double poison_gas_left } Derived class needs its own unique constructor class Mario: public Character { public: Mario(bool f, bool i, int l); private: bool flower_power; bool invincible; int lives_left; } 10 11 Constructor for our derived class Mario::Mario(bool f, bool i, int l) : Character(), flower_power(f), invincible(i), lives(l) { } u Base class constructors are called first in the initializer list u C++ will automatically call the default constructor if one exists and you don’t call any 12 Class derivation list class Name : public BaseClass1, public BaseClass2 { }; u It is possible to inherit from any number of classes u u Can add some difficulties outside the scope of this class (Diamond problem) public is an access specifier u Always want to use public u Private would make everything inherited private u Which would mean other things wouldn’t know you had them u Which really defeats the whole purpose Slide adopted from Branden Ghena 13 Extending base class functionality class Mario: public Character { public: Mario(bool f, bool i, int l); void throw(); private: bool flower_power; bool invincible; int lives_left; } Extended functionality Provides features that the original class does not 14 Overriding base class functionality class Mario: public Character { public: Mario(bool f, bool i, int l); void throw(); void display(); private: bool flower_power; bool invincible; int lives_left; } Overridden functionality Redefines existing functionality to do something different How does a function know which function to call? 15 Character c; Mario m(true, false, 2); c.display(); m.display(); Compiler decides based on types at compile type. Will call the correct function based on the object. This is static dispatch! 16 What if we had a function like this? void print_char(Character& c) { c.display(); } Static dispatch is happening by calling the Character print based on the type Character c; Mario m(true, false, 2); print_char(c); print_char(m); //Function call casts m to base class 17 We want dynamic dispatch u Decide at runtime which implementation is the best fit u This is dynamic polymorphism! class Character { class Mario: public Character { public: Character(); virtual void display(); public: void display() override; protected: Posn<int> position; double height; double weight; bool flower_power; bool invincible; int lives_left; } private: bool flower_power; bool invincible; int lives_left; } Making a pure virtual function (base function must be overridden) class Character { public: virtual void display() const = 0; } class Mario: public Character { public: void display() const override; 18 19 Planning and building Tic-Tac-Toe 20 What you’ll need u Downloaded project starter code from Canvas u A place to take notes where you can take notes to plan out the program