Sign in to follow this  
Zyndrof

Tell me about classes!

Recommended Posts

Hi! I've just recently read the chapter about classes in my book "Beginning Visual Basic.NET" by Wrox. It left me with some questions and I don't know what to ask about... I can't really find any use for classes (my own, not the classes in the .NET Framework) at the moment, it could be because of that the applications I've built so far are so small that a class isn't needed. Therefore I would appreciate it very much if you told me why and when to use classes, what they are there for and why I shouldn't write my entire program in the same file. Thank you in advance!

Share this post


Link to post
Share on other sites
Classes are logical stuctures for containing data and functions to use that data. Say you are simulating a car. You break the car up into classes.
Engine class, transmision class, rearEnd class, Tire class, stearing class, break class. Now some of these could be combines or evliminated but we are just using it for an example.

The Engine class will have an rpm member, a fuel flow, air flow. What ever is needed to simulate the engine running. The functions you have in the class will be ways to communicate with other classes. A function that returns torque and engine output.

The reason you do this is to "encapsalate" the data in the class. You only allow the class to change the data in it's class, this makes sure that all the data going in and out of the class is controled by the class. This makes sure that you always have "valid" data. Also all data is the class is irelivent to the outside classes. If you change the way that the data is handled in the class the outside classes does not care. This makes it less prone to error when you need to change the way a class does something. You don't have to go through all of your code finding all the places you deal with this data.

theTroll

Share this post


Link to post
Share on other sites
Classes are used as in object oriented applications to represent a real-world object. For example, if you had a racing game, you would want to create a car class. From there, you would realize that the car has so many individual parts that it needs to be broken down into smaller parts. So you come up with perhaps an engine class, a tire class, an exhaust sytem class, etc. Each class would have properties (physical attributes) and methods (action items). The engine class could have properties such as size, horse power and have methods such as accellerate and explode (yikes!). I'm sure you get the idea. So as you are designing your game or whatever, you need to think through how it will be organized. There are much more in-depth explanations of what classes are and why they are used, but this should be a pretty good summary.

Good luck.


EDIT: Too funny, The Troll had the same example as me. Great minds think alike!

Share this post


Link to post
Share on other sites
As your programs grow in complexity, you will discover that having one huge main class is difficult to maintain.


If you want to study the issue, look at books on "design patterns". They discuss common reasons to use classes, and common ways that classes work together.


One reason to develop classes is to denote natural clusters of static functions and values. Think of classes like Drawing.Color for an example of this. In reality a 'color' element is just a simple number, but wrapping all color functions in a class gives you a consistant way to work with the number.

Another reason is for sets of functionality. Everything relating to files is in one class. Everything relating to directories is in one class. Everything relating to Strings is in one class, and so on. When you use the class, you don't care how it works internally and you assume it does all the error checking so you don't have to. In a simple game like pong, you might have a class for the paddle. One operation would be to move up or down, and the internals to the class would verify that the paddle didn't go off the screen, or wasn't moving too fast. Then you can hook the paddle input functions to a keyboard, mouse, or AI without worrying about the internal details.

Another reason for classes is for common sets of features. These are used for inheratance. For a framework example, there are many types of Exceptions but all of them are derived from the Exception class. It doesn't matter if the exception is a null parameter, an array bounds error, or a numerical overflow. You can handle each type of exception specifically, or you can just do a generic handler for all exceptions. A lot of books and courses will use the shape example. Imagine a drawing shape base class. It can have functions that tell the number of sides, the colors, the area and perimeter, and so on. Child classes might include an oval, rectangle, and regular n-gon. the Oval class can have a child class called "circle". The rectangle might have a child class called 'square'. The n-gon class might have specialized child classes for triangle, hexagon, and dodecagon shapes. It doesn't matter if you have a rectangle, circle, or octagon, you can still use the basic operations like counting the number of sides, getting the perimeter, and setting the drawing color.


Another reason for classes is to manage size. Depending on who you talk to, the ideal length of a function is anywhere from 5 to 30 lines. Once a source file gets above 1000 or so lines, it is hard to manage. So a natural size for a class is around 10-50 members.


There are many more reasons, but those are a few that a beginner should be able to understand.

Share this post


Link to post
Share on other sites
Thank you for your answers. I've just now finished a project called "The Hamburger Stand" to learn how to use classes. It went very well.

What about public and private functions? I wanna know a little more about them and when to use them. I'm especially interested in private functions, why would I want to use them?

Share this post


Link to post
Share on other sites
Public functions are those functions you want called from outside the class. Private functions are functions you only want the class iself to use. So you have a function that Parses a string that is used in your function. The function has no use outside your class, but you use it all the time in your class.

This is the reason for private functions.

theTroll

Share this post


Link to post
Share on other sites
Quote:
Original post by TheTroll
Public functions are those functions you want called from outside the class. Private functions are functions you only want the class iself to use. So you have a function that Parses a string that is used in your function. The function has no use outside your class, but you use it all the time in your class.

This is the reason for private functions.

theTroll


Aha... It makes sence now :)

Share this post


Link to post
Share on other sites
Quote:
Original post by vinb
Classes are used as in object oriented applications to represent a real-world object.


While this is often the case, I just wanted to point out that this way of thinking can lead to poor design. As frob has hinted, a class should really encapsulate behavior, not try to represent a real-world object. For a famous example of this see "Heuristics and Coffee" (PDF).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this