Basically they represent nouns most of the time. A noun can do stuff by using verbs (methods) or perhaps you define your noun with a definition out of the dictionary (variables). For example say there is a chair in some room somewhere. Perhaps you would want to represent that chair with a class? The chair class would probably have variables describing its dimensions and its location on the floor. The chair class would also have methods like push and pull that would affect the location on the floor. Or maybe you could even add a variable for whether or not someone is sitting on the chair? Then there would be methods like sitOn and sitOff to represent someone sitting on the chair or no one sitting on the chair.
I hope I helped. Also, here is a link to the tutorial for more another explanation.
thanx that helps alot but for the program that i am making i need to make a class for a monster with at least 3 different attacks. if you dont mind i would like to see a VERY SIMPLE(just dont want to take up too much of your time) class for an oger that has 2 randomly chosen attacks.
class example { //name of class, used to declare objects (i.e. example cat; just like int cat;)
public: //so functions not declared in this class can access its members
int x, y; //plain old int's
void move(); //function declaration, must be defined else ware
void move2();
} ball; //declares ball to be an object of class "example"
void example::move(){
/*do stuff*/ } //our function definition
//access an objects members like this:
//object_name.member_name operation;
ball.move();
ball.x = 5;
//more objects of class "example" can be declared like this:
example ball2;
//classes are useful if you have multiple objects with the same/similar properties/members.
//although we have only defined void move() once, we can use it for multiple objects
ball.move();
ball2.move();
/*if a function of an object wants to access one of the objects members, it only needs the
member name, not the object_name.member_name format.*/
void example::move2(){
x = 5; //not ball.x = 5;
}
Hope this helps.
kevinkjt2000's link is also very useful. That is where I learned all I know about classes.
class Monster {
private:
int m_HP = 20;
int m_str = 10;
string m_name; // "Ogre"
string m_weapon;
// plus whatever other attributes you want the class to have
public:
// explicit constructor
Monster Monster(string name);
// pass this function another Monster object
void Attack(Monster attackee) {
// code to choose attacks, apply damage
}
};
1 2 3 4 5
// example declaration and call
Monster Ogre = new Monster("Ogre");
Ogre.Attack(Player);
Ogre.Attack(Ogre2);
that helps but maybe i should have made what i wanted clearer. i want the "bear" to do attack 1 after the program user does a choice. would i just put bear1.attack1 after the choice is made