Tuesday, April 24, 2007

Abstract Classes and Virtual Functions

As quoted from IBM developer's website, the definition of an abstract class is as follows: "An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function."

My definition and benefits of abstract classes
An abstract class is used as a guideline and lays down a set of rules that a game engine should constrain itself to. Abstract classes is used to standardize code in a game engine making tasks less complicated and more efficient. In my opinion, I feel that abstract classes are a key role in engines be it game engines or program frameworks. By setting a standard, the programmer saves time by not having the need to recode class structures or overloading function parameters.

My definition and benefits of virtual functions
From my understanding, virtual functions are defined by an inherited class. These functions are declared, but not defined in the base/abstract class, but can be used to access variables in the abstract level, via the inherited class. These functions must also be defined by the inherited class in order for the class to function normally. Additionally, virtual functions can be used without definition in the abstract level, although a definition must be provided in the inherited class for this to function.

Advantages and Application of abstract classes and virtual functions
With the above knowledge of abstract classes and virtual functions, it is relatively easy to see the advantage of applying the abstract structure to a game engine or software framework.

One of the advantages would be decreasing the number of parameters in a function of the inherited class. Say, I have the declaration of the base class as such:

Example abstract class
class Sentient
int hp;
int mp;
int attack_power;
int damage;
virtual void Attack(Sentient &Target);

Advantage 1: Less parameters
The first advantage of having an abstract class can be seen from the virtual function: virtual void Attack(Sentient Target); Notice that the function only needs to take in 1 parameter, which is basically an object of type Sentient, itself. Knowing that there are 4 standard parameters defined in the base class, (hp, mp, attack_power and damage) we can conclude that any class inherited from Sentient can be specified in this parameter, because they already have the basic requirements of the base class, having inherited from it after all, this in turn reduces the number of parameters required in a class, and makes programming more efficient and time-saving.

Advantage 2: 1 definition for each function
The problem
Another advantage of abstract classes is that any class inherited from an abstract class can be specified in the Attack class, giving us no need to overload the Attack function. If we had NOT used abstract classes and instead defined 2 different classes, human and demon, we would have to overload the Attack function to take in different parameters, after all humans can attack demons and can sometimes attack other humans too, right? These may not be a problem if we only have 2 different classes, but imagine a world with over 100 different kinds of monsters and enermies. We would have to overload the Attack function once for every imaginable creature.

The solution
By using abstract classes, all we need to do is write the Attack function once, and any class inherited from the base class can be fitted into the parameter specified in the base class's function.

Advantage 3: Different definitions for virtual functions
Finally, note that Attack is a virtual functions. As different monsters attack in different ways, using a virtual function will surely furfill our needs. For example, a human class inheriting from a Sentient class could use the factor damage but not the factor attack_power when attacking, but a monster class could use both the attack_power factor and the damage factor. This gives us a wide range of options, and makes our game more intuitive.

The joys of abstract classes are limitless and to summarize this article, I feel that the use of abstract classes will benefit game engine design and is highly useful to large-scaled games where many entities can exist.

The Assignment
Here is the assignment that was requested from the lesson, it can be downloaded from: http://simian.brankenonline.com/uploads/gdev_abstract_classes.zip.


No comments: