C++ classes

Status
Not open for further replies.

sum1nowhere

Baseband Member
Messages
85
I have read alot of explanations about what classes are supposed to do but I still do not get it. What is the point of them? and what do they do?
 
Yeah, I'm wondering about classes as well. Seems like no matter how much I read about them and practice doing them, it just doesn't make any sense...
 
They make your life easier by having a template for objects you will frequently use.

For example:

Lets say you're making a game, and you want to make all spaceship objects have certain characteristics, like Speed, Armor, etc.

You can make a class:

Code:
#include <iostream>

using namespace std;

class SpaceShip
{
protected:
	int Armor;
	int Speed;

public:
	void Accelerate()
	{
		cout<<"Your spaceship is zoomin\'!"<<endl;
	}
};

void main()
{
	SpaceShip sp;
	sp.Accelerate();
}

Hopefully that should clarify things. ;)
 
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!
 
At the risk of sounding like an idiot... I'm going to have to say that I still do not understand this. What is the difference between a class and a function?
 
*Is still confused*

Lets take that spaceship example. Instead of using classes couldn't you just declare variables like int sp_armor and int sp_speed, then make a function that could be used when any ship accelerates? It would be the same thing...
 
@Chaosenemy:
Lets take that spaceship example. Instead of using classes couldn't you just declare variables like int sp_armor and int sp_speed, then make a function that could be used when any ship accelerates? It would be the same thing...
You can... but your code would be out of control. The beauty of having an object is that all of your variables relating to that object (sp_armor and sp_speed) are all located inside one place. What would you do if you wanted to have two spaceships? You have have to create int sp_armor1, int sp_speed1, int sp_armor2 and int sp_speed2. What if you wanted to have 100 spaceships? Your code would have so many variables and functions, you would probably give up on the project. Creating classes allows you to create one class, and then create multiple instances of that class, all of which have their own properties. Its kind of like opening up notepad and writing two different text files. They are both text files, but they have different data, and different properties.


@sum1nowhere:
At the risk of sounding like an idiot... I'm going to have to say that I still do not understand this. What is the difference between a class and a function?

You don't sound like an idiot. Thats a legitimate question.

A class encapsulates both functions and variables into one thing -- an object.

Classes contain only variables and functions that relate to the overall design of your object. Say we are making a game, and we have a creature call a turtle. Our turtle has health, and can walk. Lets look at the overall design of the turtle:

Turtle:
Health
Walk

Because walking requires that multiple statements be run, that would be a function of the turtle. Because health is a scalar variable, we would make that a simple integer variable. Lets look at the turtle in class form:
Code:
class turtle{
  public:
    void walk(){
      //Increment Turtles Position
    }

  private:
    int health;
}
Do you see how our turtle object combines both integer and functions? It differs from functions because it isn't just called and then destroyed. When we call an object, it remains in the program until we destroy it. It takes functions that are relevant to the object, and places them all in one place.

I hope this explains it a little better. If not, try reading this article I wrote on my website:

http://www.digitalbrink.com/articles/index.php?article=understanding_classes.html
 
Status
Not open for further replies.
Back
Top Bottom