Computer Science Canada Classes, Good or bad? |
Author: | mirhagk [ Fri Dec 04, 2009 12:09 pm ] | ||||
Post subject: | Classes, Good or bad? | ||||
I recently wondered what the point of classes is. To demonstrate my point let's look at using classes vs normal types and procedures. We're going to use a snow flake engine (very simple but it serves the point)
could be a lot better but let's look at the exact same program using types
the engine part is even in a module for reuse Now can someone tell me the advantage of using classes, if there is one. To my understanding creating two hundred variables and three procedures (with types) is better than two hundred variables and three hundred procedures. Not sure if that actually makes a difference but even if it doesn't, it's still easier to do the second way because you don't need to understand pointers and things. Please correct me if I'm wrong. |
Author: | rdrake [ Fri Dec 04, 2009 1:28 pm ] |
Post subject: | RE:Classes, Good or bad? |
Classes let you structure your code in a better way and gives you encapsulation. |
Author: | DemonWasp [ Fri Dec 04, 2009 1:58 pm ] |
Post subject: | RE:Classes, Good or bad? |
Creating a new instance of a class shouldn't create a new "instance" of its procedures. Those procedures are compiled to operate on a supplied pointer to the data objects of the class itself in any reasonable implementation (no guarantees that Turing is reasonable). The big advantage of classes that you're missing out on is polymorphism. Instead of snowflakes, let's assume you're looking at vehicles. Well, some vehicles fly, some drive, and some float on water. Naturally, they'll behave differently and have different information to store. With classes, you would have a base class called Vehicle, then one for FlyingVehicle / DrivingVehicle / etc. With modules, there's no way to do that cleanly. |
Author: | Superskull85 [ Fri Dec 04, 2009 2:22 pm ] | ||
Post subject: | Re: Classes, Good or bad? | ||
To expand on what DemonWasp mentioned you could have a pointer to the base class Vehicle, but make an instance for FlyingVehicle/DrivingVehicle/etc. and than pass those pointers into the same method easily. For example an expansion of your class example could be:
How could you easily/cleanly do this with modules and types? What if you wanted rain to have additional methods that a basic particle does not have? |
: |