Classes Part 1
Time to move up to classes. You'll hear the term, Object Oriented Programming Languages. This is exactly what C++ offers,
and at the root of Object Oriented Programming is classes. At the basic level, a class is a lot like a struct.
They are a container to hold data. However they can also have functions which can manipulate the data which they hold.
If you are rusty on structs, I encourage you to review the struct tutorials now, because we're going to build on it here.
Lets say we want to represent a vehicle. At the very least it should have a name, number of wheels and a current speed. If we were going to do it in a struct we could do it like this:
struct Vehicle
{
int numWheels;
float currentSpeed;
char name[ 32 ];
};
To declare it as a class you do this:
class Vehicle
{
int numWheels;
float currentSpeed;
char name[ 32 ];
};
whoa! it looks exactly the same except the keyword 'class' is used. This is valid, although it isn't great coding syntax and doesn't give us any additional functionality. So we'll take it a few steps farther:
class Vehicle
{
public:
int numWheels;
float currentSpeed;
char name[ 32 ];
};
What the heck does public mean? When you declare a class you can tell it the protection level of it's internal variables. If it is public then i can access the variables from anywhere, just like with a struct:
Vehicle car;
car.numWheels = 4;
car.currentSpeed = 0.0f;
strcpy( car.name, "Taxi" );
But lets say you have 100 programmers working in the same code. Maybe you created the vehicle class and you don't want the other programmers directly setting your internal variables. Then you would use the keyword 'private:'
class Vehicle
{
private:
int numWheels;
float currentSpeed;
char name[ 32 ];
};
Vehicle car;
//now this will no longer compile
//because the variables are private
//to the class and won't let anyone
//outside the class access them
car.numWheels = 4;
car.currentSpeed = 0.0f;
strcpy( car.name, "Taxi" );
So how will those variables get set? Well you can have functions inside the class which then modify the data!
class Vehicle
{
public:
void SetNumWheels( int count ) { numWheels = count; }
void SetName( const char *pName ) { strcpy( name, pName ); }
private:
int numWheels;
float currentSpeed;
char name[ 32 ];
};
//here is how you would use it
Vehicle car;
car.SetNumWheels( 4 );
car.SetName( "Taxi" );
Look at that! Those are called accessor functions because they allow you to access the private variables inside of the class. What's the point of all this?
Lets say you want the Vehicle to take care of it's own speed and only report that speed back.
This way if you have 100 programmers messing around with the car, they can't directly set its speed which could mess up any physics computations you might be doing.
One last thing - you probably want to initialize your variables to a starting number, or just to 0 to clear them out. You can have a function in the class
which is called right when the class is created. This function name has to be the same name as the class, doesn't return any value, and is referred to as the 'constructor'.
It's also worth mentioning you can have a function that is called when the class is deleted - this way you can clean up any memory you allocated in the class, this function
is called a 'destructor' and looks just like the constructor but begins with the tilde '~' key.
class Vehicle
{
public:
//here is the constructor
Vehicle( void )
{
m_currentSpeed = 0;
m_numWheels = 0;
m_name[ 0 ] = 0;
}
//here is the destructor
~Vehicle( void )
{
//for this example we don't need to run any code here
}
void SetNumWheels( int count ) { numWheels = count; }
void SetName( const char *pName ) { strcpy( name, pName ); }
void SpeedUp( void ) { currentSpeed += 10; }
void SlowDown( void ) { currentSpeed -= 10; }
float GetSpeed( void ) { return currentSpeed; }
private:
int numWheels;
float currentSpeed;
char name[ 32 ];
};
Vehicle car;
car.SetNumWheels( 4 );
car.SetName( "Taxi" );
car.SpeedUp( );
float speed = car.GetSpeed( );
printf( "The car is going %.2fmph\r\n", speed );
You see what we did there? We simply told the car to speed up and let the Vehicle class do the rest. Obviously this is a very simple example, but you can imagine the possibilities.
What if we put a physics simulation inside of SpeedUp which sped up the car based on its current velocity, mass, wheels, etc. .. that is way beyond the scope of this lesson -
but I'm trying to give you cool examples of why we have functions in classes.
There is some coding syntax which we aren't following. Normally variables inside a class start with a special character to show someone, at a quick glance, they belong to a class.
Syntax standards vary among programmers, companies, etc. I like to use m_ the 'm' stands for module (i.e. class) meaning this variable belongs to this class:
class Vehicle
{
public:
void SetNumWheels( int count ) { m_numWheels = count; }
void SetName( const char *pName ) { strcpy( m_name, pName ); }
void SpeedUp( void ) { m_currentSpeed += 10; }
void SlowDown( void ) { m_currentSpeed -= 10; }
float GetSpeed( void ) { return m_currentSpeed; }
private:
int m_numWheels;
float m_currentSpeed;
char m_name[ 32 ];
};
The possiblities to use classes are endless. When thinking about creating a program, I like to think about all the different components needed and place each of them into their own class.
some examples:
- If you're writing a game, you could have a Character class which holds all the information about a character.
You could create and use this class for each character in the game. It could manage it's own hitpoints, the amount of damage it takes, etc.
- If you're writing a hacking module, you could have a class for each system you want to hack.
The class could be responsible for knowing how to hack into the system, and tell the main app if the hack was successfull or not.
Let me know what you come up with!!!!
Next up we're going to cover placing classes in multiple files so you don't just have one ugly code file.
Then, when you perfectly understand classes, we'll show you how they get REALLY powerful - through inheritance and polymorphism!
|