classes

can someone give me a fairly basic rundown of classes. like what they are for and how to use them.
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.

http://cplusplus.com/doc/tutorial/classes/
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.
A class is a way of creating "objects" and giving them properties or members(location, velocity, size, etc). A class is declared and used like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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.
Last edited on
1
2
3
4
5
6
class ogre {
    public:
        int x, y;
        void attack1();
        void attack2();
}ogre1, ogre2;
Last edited on
You could start with something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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);
i still dont think i fully understand... im just going to wait until i get better at c++
I think i can tell you what a class is...

A class is like a blueprint.

To create a new type, you can define a class, which is code that groups data members and member functions.

So if you create a class called "Critter",

and in the class you made an variable called "hunger",

then every Critter object would contain an variable called "hunger".

Does that help, i found it hard to understand what classes are.
Last edited on
ya that helps a lil bit

class ogre
{
public:
void attack1();
void attack2();
}
axe;
void ogre::attack1()
{
cout << " the ogre attacked with the axe\n";
}
}


is that right?????????

p.s. did you learn bout classes from dat purple and black c++ book?
That the right idea yeah :)

So then every ogre object would have two functions called attack1() and attack2().

And if the object called the function called attack1() it would cout " the ogre attacked with the axe"

:D
yay i feel smart haha. but i do want to know 1 more thing... how exactly would you call the function "attack1()"
Well, for example, lets say youve made a class called bearClass:)

Now, if we wanted to make an object from that class we would put:

bearClass bear1;

Now, we have a object called bear1 which is of the type called bearClass (which is the class weve made)

Now, if we wanted bear1 to do attack1(), we would put:

bear1.attack1();

The dot in between bear1 and attack1() tells the computer that we want to use the function called attack1() which belongs to bear1.

Does that help, if not ill try to explain better.
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
Yes, just put it wherever you want it to execute. It's just like calling a normal function.
thanx alot yall have really helped me out cuz for the program i am making, classes are a HUGE part of it. :-)
Topic archived. No new replies allowed.