Absolutely. There are three huge advantages of classes over data
structures which is Privatization, inheritance, and polymorphism.
Those are the technical terms for them, and I'll quickly explain them
With data structures, a struct, everything inside that struct is
public. I.E. always visible to the rest of your program. There are
times though when you want to write some internal piece of code
weather it is a function or variable that only belongs specifically to
that class and nowhere else in your program. This is where
Privatization comes in handy. Anything you don't want visible to the
rest of your program goes in the private section of the class and is
only accessible from inside the class itself and nowhere else.
The second biggy is inheritance. Again with data structures once you
create it you can't link it to other structures or expand the basic
functionality of that struct without modifying the struct itself.
Classes don't have this problem.
With classes you can link several classes together or expand the
functionality of a class by simply creating a new class and inheriting
the old one.
For example, let's say you have a basic Player class. It is pretty
generic storing the direction the player is moving, where the player
is located, but has no support for Jedi type characters. Well, no
problem. We'll just expand the functionality of this class by creating
a new one.
Basically, we can create a new class called Jedi and inherit the
Player class. This way we can add force powers and abilities by using
the Jedi class, but borrow or reuse all the functionality in the
Player class as well. Plus we don't have to modify the Player class to
do it. Where with a struct you either have to modify the Player
structure or write a completely new struct for Jedi characters.
Neither is as handy as simply inheriting a class.
Finally, there is an object oriented technique called polymorphism.
With structs since there is no way to inherit or expand them there is
also no way to override any functions or variables that might be
present in that structure. Again classes have a huge advantage here in
that you can have several functions named the same thing, but take and
return different data types. Some newbies find this very confusing,
but its a rather simple concept really.
For instance, let us say you are creating a game and you want to have
a class that generates random numbers. There are times where you might
only want it to generate a number from a fixed range of integers, or
you might want it to randomly generate an unlimited range of numbers.
Sometimes you might want to generate a floating point number like 0.1.
In C-Style data structures it will only allow you one function named
SelectRandom() and you'll have to create different names for the other
variations. That's sort of a pain if you have several variations on
the same function. With classes polymorphism comes to the rescue. You
can declare several different variations of SelectRandom() and when
programming the compiler will know which variation you mean to call.
It simplifies things rather than complicates them in the long run as
you, the programmer, only need to remember SelectRandom(() is the
function to use when generating numbers. The alternative that structs
provide is several different variations like SelectRandomInteger(),
SelectRandomFloat(), and SelectRandomRange() or something like that.
Personally, if I have the option I'll just name them all
SelectRandom() and be done with it, and classes let you do this.
On 4/22/11, Hayden Presley <hdpres...@hotmail.com> wrote:
> Hi Thomas,
> As we are only working with variables here, my question would be: is there
> an advantage to using a class versus a data structure?
> Best Regards,
Gamers mailing list __ Gamers@audyssey.org
If you want to leave the list, send E-mail to gamers-unsubscr...@audyssey.org.
You can make changes or update your subscription via the web, at
All messages are archived and can be searched and read at
If you have any questions or concerns regarding the management of the list,
please send E-mail to gamers-ow...@audyssey.org.