Thread: C++ classes
View Single Post
Old 12-04-2006, 10:58 AM   #4 (permalink)
Babbage
 
Newb Techie

Join Date: Dec 2004

Posts: 45

Babbage

Default

Classes are pretty much used to help encapsulate any related variables and functions into one area.

If you take a look at KBlair's SpaceShip example, you can see the variables related to the spaceship (Armor and Speed) and the function related to the spaceship (Accelerate()) are all well defined under one class.

If we didn't put this information into a class, we would have to place the variables into a struct, and define an open function called Accelerate() to get it to work. While this would work, anything could access the Accelerate() function, which leaves yourself open for problems.

So, the basic idea behind an object is to make your job easier as a programmer, by placing all related information into one object for easy reference. It also helps to curb any programming errors that may arise from having a bunch of functions without strict reference to any information.

I hope this makes sense to you, but in case it doesn't, lets take a look at one more example.

Just image an ordinary wall clock. The clock has a seconds hand, a minutes hand, and an hours hand. If you were to define the clock as an object in your program, these would all be declared as variables, because they are all scalar variables that change over time. The clock also has the following properties: increment seconds, increment minutes, and increment hours. These could all be defined as functions to your object, because they are all going to obviously process at least one statement to increase the information.

You can see how all the clock information relates to each other. If you DIDN'T define this clock as an object, there would be a lot of structs and functions floating around to achieve the same thing.

I hope this helps you understand the concept of objects a little better.

Good luck programming!
Babbage is offline