Advertisement

C++ For Absolute Beginner

Started by August 02, 2017 09:12 PM
12 comments, last by RobertCunningham 7 years, 3 months ago
1 hour ago, Lucas_Cage said:

...

I have a small question, let me hijack the topic for just one second, sorry :P

I remember watching a video on youtube where a similar example where made (with items, ice and fire sword which inherit from sword and what would happen if you then need an ice/fire sword, which is, it inherit twice from sword and either won't work or is not efficient, not sure which one is...), so what if you now want a Rogue/Fighter character? Inheritance would be bad cause you inherit twice from hero, right? Would the "Component design pattern" be commonly used to allow this "mixed hero stuff" in games?

 

The example is trivial and only exists to represent simple inheritance.  The structure given may fall apart at a certain level of complexity as all analogies do. 

To answer your question directly the inheritance is generally used when the design lends itself to a hierarchy of classes that are defined by what they are.  This is simply defined as a "is a" relationship.  The Sword is a wood sword.  Composition becomes more appropriate when the relationship is better defined by a "has a" relationship.  The Sword has an enchantment.  When making the decision between inheritance and composition we decide in simple terms if each instance of a Fighter is a Hero or if some instances of  Hero simply have the attributes of a Fighter.  In many cases both answers will be affirmative, but when we give a more practical look at our classes and how they will function in the program one solution generally emerges as superior.  Remember that this is about ownership.

To further illustrate I provide a more specific application.  We consider the hybrid class exists in a leveling system where the Rogue/Fighter begins as a fighter for the first 8 levels selecting only those abilities appropriate to his class.  Then at some certain level we will say 8 he can now choose to increase his level to 9 as a fighter or can take abilities as a level 1 thief.  Inheritance now seem obviously inappropriate.  It is clear that the hero has the attributes of the class (fighter, thief, mage).  The fighter is not truly a type of hero and certainly does not exist autonomously among the other proposed children.  At anytime he may need to be bound to another child class of the same parent.  Not only does this reduce the usefulness of the child classes it makes them an obstacle.  When we consider that the fighter now has certain limitations imposed on the thief class via the rules of the RPG system. We can see the ownership relationship is more accurately expressed by saying The Hero has attributes of a class(RPG class), rather than by saying a particular class(RPG class) is a hero. 

In a simpler relationship where we have a class sword.  The sword has attributes defining its damage and also another defining its enchantments.  We naturally picture the ice sword as a sword that has an ice enchantment rather than an ice enchanted sword that is a sword.  Therefore, we can see the composition model more readily applies.  We would define the sword class through composition so that it can posses multiple enchantments as member attributes.  However, in an instance where we wish to define the sword itself we would use inheritance.  So that the parent sword class would have child classes wooden sword, iron sword, steel sword.  In this case inheritance becomes more appropriate as a wooded sword is a sword. This phrase can easily express the ownership relationship.

 

The Quarry Works Creed

We who shape mere stone must always envision cathedrals

Advertisement

Me too i'm a beginner in c++ but still learning. Thanks for the advice it really helps.

This topic is closed to new replies.

Advertisement