Difference between revisions of "Intermediate C++ Game Programming Tutorial 17"

From Chilipedia
Jump to: navigation, search
(Video Timestamp Index)
 
(86 intermediate revisions by 2 users not shown)
Line 1: Line 1:
I haven't actually written this part yet. Hopefully I won't forget and just publish it like this ;)
+
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 <code>mutable</code>, for some reason.
  
 
== Topics Covered ==
 
== Topics Covered ==
* Class hierarchy
+
=== Part 1 ===
* Mutable modifier for member data
+
* 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 <code>protected</code>
 +
=== Part 2 ===
 +
* Hierarchical inheritance
 +
* Abstract vs. concrete classes
 +
* <code>mutable</code> data members
  
 
== Video Timestamp Index ==
 
== Video Timestamp Index ==
[https://youtu.be/3J1Pz30IE4Q Tutorial 16]
+
[https://youtu.be/O2-jdGGCFkE Tutorial 17.1]
 +
<div class="mw-collapsible mw-collapsed"><br />
 +
* What is inheritance? Introducing a simple example <code>class ChildClass : public ParentClass {...};</code>[https://youtu.be/O2-jdGGCFkE?t=15 0:15]
 +
* Different language used for the same concept [https://youtu.be/O2-jdGGCFkE?t=1m45s 1:45]:
 +
<div class="mw-collapsible-content">
 +
** to "inherit" a class, from "parent" to "child",
 +
** to "extend" a class, from "base" to "extended",
 +
** to "derive" a class, from "base" to "derived",
 +
** to "sub-class" a class, from "superclass" to "subclass" (used in Java)
 +
</div>
 +
* Why is inheritance important? no clutter, no repetition of code, no tampering inside deployed classes [https://youtu.be/O2-jdGGCFkE?t=2m44s 2:44]
 +
* Fictitious but instructive <code>Smasher</code> and <code>EliteSmasher</code> class example [https://youtu.be/O2-jdGGCFkE?t=5m08s 5:08]
 +
* Inheritance creates an "Is-a relationship" [https://youtu.be/O2-jdGGCFkE?t=8m15s 8:15]
 +
<div class="mw-collapsible-content">
 +
** A child class "is-a [kind of]" parent class, so you can pass a child object to functions that expect a reference to a parent object
 +
** The ability to refer to a child type with a parent reference (or pointer), allows us to create functions that can work over many types, as long as they all inherit from a common parent
 +
</div>
 +
* Inheritance and constructors: you can't inherit (default) constructors [https://youtu.be/O2-jdGGCFkE?t=9m49s 9:49]
 +
<div class="mw-collapsible-content">
 +
** The base class of a class behaves like the first data member of the extended class: it is initialized (constructed) first
 +
** The default constructor of a child class has to call a constructor of the parent class
 +
</div>
 +
* Overriding parent functions in the child class [https://youtu.be/O2-jdGGCFkE?t=13m22s 13:22]
 +
<div class="mw-collapsible-content">
 +
** In addition to extending functionality of a parent class in a child class, overriding allows you to alter functionality, if you have to
 +
** You can invoke the parent version of an overridden function by prefixing the parent class name <code>ParentClass::OverriddenParentFunction();</code>
 +
** The private members of the parent class are not accessible to the child class
 +
</div>
 +
* Introducting the <code>protected:</code> access specifier in classes [https://youtu.be/O2-jdGGCFkE?t=16m34s 16:34]
 +
<div class="mw-collapsible-content">
 +
** Form of access control that enables children to access members (while keeping members private to all other scopes)
 +
** Allows you to apply proper encapsulation of the parent class by setting a separate protected interface only for its children
 +
** ROI "Return on invested time" should guide your choice to either i) protect your members, ii) keep your members private but build a proteced interface
 +
</div>
 +
* Recap of key techniques learned about inheritance [https://youtu.be/O2-jdGGCFkE?t=21m05s 21:05]
 +
* Cool it down with applying inheritance, risk of mis/overusage of the concept [https://youtu.be/O2-jdGGCFkE?t=22m08s 22:08]
 +
</div>
 +
 
 +
[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]
 +
* 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)
 +
** Concrete (child) classes are supposed to be instantiated
 +
</div>
 +
* 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]
 +
<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]
 +
** 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]
 +
* Giving the implemented code a test run [https://youtu.be/21uaUOeOof8?t=16m49s 16:49]
 +
* Recap of topics covered [https://youtu.be/21uaUOeOof8?t=18m40s 18:40]
 +
* 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]
 +
<div class="mw-collapsible-content">
 +
** Never break the "is-a" rule
 +
** Don't go crazy with applying inheritance
 +
</div>
 +
</div>
  
 
== Part 2 Class Specification ==
 
== Part 2 Class Specification ==
All of the MemeFighter entities of the system will have the following shared functionality:
+
All of the <code>MemeFighter</code> entities of the system will have the following shared functionality:
* Member data for name (string), hp, speed, and power (int)
+
* Member data for <code>name</code> (string), <code>hp</code>, <code>speed</code>, and <code>power</code> (int)
 
* Getters for name, initiative (speed + 2d6), and alive state
 
* Getters for name, initiative (speed + 2d6), and alive state
* Punch function that takes a different entity and applies damage (str + 2d6) to it
+
* <code>Punch</code> function that takes a different entity and applies damage (power + 2d6) to it
* Tick function where the entity recovers 1d6 hp
+
* <code>Tick</code> 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.)
 
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
+
There are two concrete <code>MemeFighter</code> entites: <code>MemeFrog</code> and <code>MemeStoner</code>
  
MemeFrog:
+
<code>MemeFrog</code>:
 
* Has 69 hp, 7 speed, and 14 power
 
* 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
+
* Has a <code>SpecialMove</code> function that takes an entity and has a 1/3 chance of dealing 3d6 + 20 damage
 
* Takes 1d6 of damage every turn
 
* Takes 1d6 of damage every turn
  
MemeStoner:
+
<code>MemeStoner</code>:
 
* Has 80 hp, 4 speed, and 10 power
 
* 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 to the entity's name
+
* Has a <code>SpecialMove</code> 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 ==
 +
[https://github.com/planetchili/Inheritance Inheritance Github Repository]<br />
  
 
== Links ==
 
== Links ==
[http://en.cppreference.com/w/cpp/algorithm Algorithms Library]<br />
+
[https://isocpp.org/wiki/faq/basics-of-inheritance#protected-data-not-evil C++ FAQ on protected]<br />
 +
[https://softwareengineering.stackexchange.com/questions/260343/why-is-inheritance-generally-viewed-as-a-bad-thing-by-oop-proponents Stack Overflow question about inheritance]<br />
  
 
== See also ==
 
== See also ==
 
* [[Intermediate C++ Game Programming Tutorial 18|Next in series (Tutorial 18)]]
 
* [[Intermediate C++ Game Programming Tutorial 18|Next in series (Tutorial 18)]]
 
* [[Intermediate C++ Game Programming Series]]
 
* [[Intermediate C++ Game Programming Series]]

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.

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

Tutorial 17.1


  • What is inheritance? Introducing a simple example class ChildClass : public ParentClass {...};0:15
  • Different language used for the same concept 1:45:
    • to "inherit" a class, from "parent" to "child",
    • to "extend" a class, from "base" to "extended",
    • to "derive" a class, from "base" to "derived",
    • to "sub-class" a class, from "superclass" to "subclass" (used in Java)
  • Why is inheritance important? no clutter, no repetition of code, no tampering inside deployed classes 2:44
  • Fictitious but instructive Smasher and EliteSmasher class example 5:08
  • Inheritance creates an "Is-a relationship" 8:15
    • A child class "is-a [kind of]" parent class, so you can pass a child object to functions that expect a reference to a parent object
    • The ability to refer to a child type with a parent reference (or pointer), allows us to create functions that can work over many types, as long as they all inherit from a common parent
  • Inheritance and constructors: you can't inherit (default) constructors 9:49
    • The base class of a class behaves like the first data member of the extended class: it is initialized (constructed) first
    • The default constructor of a child class has to call a constructor of the parent class
  • Overriding parent functions in the child class 13:22
    • In addition to extending functionality of a parent class in a child class, overriding allows you to alter functionality, if you have to
    • You can invoke the parent version of an overridden function by prefixing the parent class name ParentClass::OverriddenParentFunction();
    • The private members of the parent class are not accessible to the child class
  • Introducting the protected: access specifier in classes 16:34
    • Form of access control that enables children to access members (while keeping members private to all other scopes)
    • Allows you to apply proper encapsulation of the parent class by setting a separate protected interface only for its children
    • ROI "Return on invested time" should guide your choice to either i) protect your members, ii) keep your members private but build a proteced interface
  • Recap of key techniques learned about inheritance 21:05
  • Cool it down with applying inheritance, risk of mis/overusage of the concept 22:08

Tutorial 17.2


  • Revisiting the "is-a" principle of Inheritance. Applying it to build a class hierarchy 0:51
  • Abstract vs. Concrete classes 3:20
    • Abstract (parent) classes are not supposed to be instantiated (constructors are protected)
    • Concrete (child) classes are supposed to be instantiated
  • Application of abtract classes on multiple levels 4:09
  • Building the Meme Fighter class hierarchy (Abstract class) 5:28
    • Introducing the modifier mutable (an access specifier for data members) 10:06
    • When writing a const member function (i.e. a member function that does not change data members in the class), a mutable data member may be changed in that function
  • Building the Meme Fighter class hierarchy (Concrete classes) 13:03
  • Giving the implemented code a test run 16:49
  • Recap of topics covered 18:40
  • Getting a class diagram in Visual Studio 19:26
  • Closing two points of advice 19:55
    • Never break the "is-a" rule
    • Don't go crazy with applying inheritance

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, and power (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

Inheritance Github Repository

Links

C++ FAQ on protected
Stack Overflow question about inheritance

See also