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

From Chilipedia
Jump to: navigation, search
(Video Timestamp Index)
(Video Timestamp Index)
Line 37: Line 37:
 
** Allows you to apply proper encapsulation of the parent class by setting a separate protected interface only for its children
 
** 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
 
** 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 take-aways on inheritance [https://youtu.be/O2-jdGGCFkE?t=21m05s 21:05]
 
* WORK-IN-PROGRESS
 
* WORK-IN-PROGRESS
  

Revision as of 05:39, 7 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 B : public A {...};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
    • The private members of the parent class are not accessible to the child class
  • Introducting the protected 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 take-aways on inheritance 21:05
  • WORK-IN-PROGRESS

Tutorial 17.2

  • WORK-IN-PROGRESS

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