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

From Chilipedia
Jump to: navigation, search
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 protected access specifier, virtual functions, the keywords override and final, and the keyword mutable.
  
 
== Topics Covered ==
 
== Topics Covered ==
* Class hierarchy
+
* How to inherit from a class
* Mutable modifier for member data
+
* Referencing a child type with a parent reference/ptr
 +
* Inheritance and constructors
 +
* Overriding parent functions
 +
* Invoking overridden versions of functions
 +
* Access specifier protected
  
 
== Video Timestamp Index ==
 
== Video Timestamp Index ==
[https://youtu.be/3J1Pz30IE4Q Tutorial 16]
+
[https://youtu.be/3J1Pz30IE4Q Tutorial 17.1]
  
 
== Part 2 Class Specification ==
 
== Part 2 Class Specification ==
Line 27: Line 31:
 
* 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 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
 +
 +
== Source Code ==
 +
[http://en.cppreference.com/w/cpp/algorithm Inheritance Github Repository]<br />
  
 
== Links ==
 
== Links ==
[http://en.cppreference.com/w/cpp/algorithm Algorithms Library]<br />
+
[http://en.cppreference.com/w/cpp/algorithm C++ FAQ on protected]<br />
 +
[http://en.cppreference.com/w/cpp/algorithm 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]]

Revision as of 19:24, 18 November 2017

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 protected access specifier, virtual functions, the keywords override and final, and the keyword mutable.

Topics Covered

  • 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

Video Timestamp Index

Tutorial 17.1

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 (str + 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 to the entity's name

Source Code

Inheritance Github Repository

Links

C++ FAQ on protected
Stack Overflow question about inheritance

See also