Difference between revisions of "Intermediate C++ Game Programming Tutorial 17"
From Chilipedia
(→Video Timestamp Index) |
(→Video Timestamp Index) |
||
Line 54: | Line 54: | ||
[https://youtu.be/21uaUOeOof8 Tutorial 17.2] | [https://youtu.be/21uaUOeOof8 Tutorial 17.2] | ||
+ | <div class="mw-collapsible mw-collapsed"><br /> | ||
* Revisiting the "is-a" principle of Inheritance. Applying it to build a class hierarchy [https://youtu.be/21uaUOeOof8?t=51 0:51] | * Revisiting the "is-a" principle of Inheritance. Applying it to build a class hierarchy [https://youtu.be/21uaUOeOof8?t=51 0:51] | ||
* Abstract vs. Concrete classes [https://youtu.be/21uaUOeOof8?t=3m20s 3:20] | * Abstract vs. Concrete classes [https://youtu.be/21uaUOeOof8?t=3m20s 3:20] | ||
+ | <div class="mw-collapsible-content"> | ||
** Abstract (parent) classes are not supposed to be instantiated (constructors are protected) | ** Abstract (parent) classes are not supposed to be instantiated (constructors are protected) | ||
** Concrete (child) classes are supposed to be instantiated | ** Concrete (child) classes are supposed to be instantiated | ||
+ | </div> | ||
* Application of abtract classes on multiple levels [https://youtu.be/21uaUOeOof8?t=4m09s 4:09] | * Application of abtract classes on multiple levels [https://youtu.be/21uaUOeOof8?t=4m09s 4:09] | ||
* Building the Meme Fighter class hierarchy (Abstract class) [https://youtu.be/21uaUOeOof8?t=5m28s 5:28] | * Building the Meme Fighter class hierarchy (Abstract class) [https://youtu.be/21uaUOeOof8?t=5m28s 5:28] | ||
+ | <div class="mw-collapsible-content"> | ||
** Introducing the modifier <code>mutable</code> (an access specifier for data members) [https://youtu.be/21uaUOeOof8?t=10m06s 10:06] | ** Introducing the modifier <code>mutable</code> (an access specifier for data members) [https://youtu.be/21uaUOeOof8?t=10m06s 10:06] | ||
** When writing a <code>const</code> member function (i.e. a member function that does not change data members in the class), a <code>mutable</code> data member may be changed in that function | ** When writing a <code>const</code> member function (i.e. a member function that does not change data members in the class), a <code>mutable</code> data member may be changed in that function | ||
+ | </div> | ||
* Building the Meme Fighter class hierarchy (Concrete classes) [https://youtu.be/21uaUOeOof8?t=13m03s 13:03] | * Building the Meme Fighter class hierarchy (Concrete classes) [https://youtu.be/21uaUOeOof8?t=13m03s 13:03] | ||
* Giving the implemented code a test run [https://youtu.be/21uaUOeOof8?t=16m49s 16:49] | * Giving the implemented code a test run [https://youtu.be/21uaUOeOof8?t=16m49s 16:49] | ||
Line 67: | Line 72: | ||
* Getting a class diagram in Visual Studio [https://youtu.be/21uaUOeOof8?t=19m26s 19:26] | * Getting a class diagram in Visual Studio [https://youtu.be/21uaUOeOof8?t=19m26s 19:26] | ||
* Closing two points of advice [https://youtu.be/21uaUOeOof8?t=19m55s 19:55] | * Closing two points of advice [https://youtu.be/21uaUOeOof8?t=19m55s 19:55] | ||
+ | <div class="mw-collapsible-content"> | ||
** Never break the "is-a" rule | ** Never break the "is-a" rule | ||
** Don't go crazy with applying inheritance | ** Don't go crazy with applying inheritance | ||
+ | </div> | ||
+ | </div> | ||
== Part 2 Class Specification == | == Part 2 Class Specification == |
Latest revision as of 22:46, 13 October 2019
This two-part lesson teaches the basics of inheritance, how it works and what motivates its use, with ample concrete coding examples. Also taught here is the keyword mutable
, for some reason.
Contents
[hide]Topics Covered
Part 1
- How to inherit from a class
- Referencing a child type with a parent reference/ptr
- Inheritance and constructors
- Overriding parent functions
- Invoking overridden versions of functions
- Access specifier
protected
Part 2
- Hierarchical inheritance
- Abstract vs. concrete classes
-
mutable
data members
Video Timestamp Index
[Expand]
- What is inheritance? Introducing a simple example
class ChildClass : public ParentClass {...};
0:15 - Different language used for the same concept 1:45:
- Why is inheritance important? no clutter, no repetition of code, no tampering inside deployed classes 2:44
- Fictitious but instructive
Smasher
andEliteSmasher
class example 5:08 - Inheritance creates an "Is-a relationship" 8:15
- Inheritance and constructors: you can't inherit (default) constructors 9:49
- Overriding parent functions in the child class 13:22
- Introducting the
protected:
access specifier in classes 16:34
[Expand]
- Revisiting the "is-a" principle of Inheritance. Applying it to build a class hierarchy 0:51
- Abstract vs. Concrete classes 3:20
Part 2 Class Specification
All of the MemeFighter
entities of the system will have the following shared functionality:
- Member data for
name
(string),hp
,speed
, andpower
(int) - Getters for name, initiative (speed + 2d6), and alive state
-
Punch
function that takes a different entity and applies damage (power + 2d6) to it -
Tick
function where the entity recovers 1d6 hp
Functions should narrate actions / effects when it makes sense. Tests should be made to check whether an entity is alive before an action is performed (dead characters can't punch, recover, etc.)
There are two concrete MemeFighter
entites: MemeFrog
and MemeStoner
MemeFrog
:
- Has 69 hp, 7 speed, and 14 power
- Has a
SpecialMove
function that takes an entity and has a 1/3 chance of dealing 3d6 + 20 damage - Takes 1d6 of damage every turn
MemeStoner
:
- Has 80 hp, 4 speed, and 10 power
- Has a
SpecialMove
function that takes no parameters and has a 1/2 chance of adding 3 speed, increasing power by 69/42, adding 10 hp, and adding "Super" to the entity's name
Source Code
Links
C++ FAQ on protected
Stack Overflow question about inheritance