text: I'm not 100% on classes and I was wondering if someone could give me a example and explain it.
  Something like using a class to make monsters in a rpg...
  Thanks ~GameGenie
  This is a discussion on Classes\Good Usage... within the C++ Programming forums, part of the General Programming Boards category; I'm not 100% on classes and I was wondering if someone could give me a example and explain it. Something ...
  I'm not 100% on classes and I was wondering if someone could give me a example and explain it.
  Something like using a class to make monsters in a rpg...
  Thanks ~GameGenie
  Here you go.
  Code:class monster { public: void fight(); void die(); private: int power; };
  Woop?
  Thanks for the reply but it doesn't help really, i know how to write one down. Can you tell me what that would do?Thanks for the reply but it doesn't help really, i know how to write one down. Can you tell me what that would do?
  Originally Posted by prog-bmanOriginally Posted by prog-bman
  Edit: Also what would be a good way to use it?
  Code:#include <iostream> using namespace std; class monster //class called monster { public: //this allows everyhting to see and interate with each other in the class void fight() { cout << "you called the fight function" << endl; } void die() { cout << "you called the die function" << endl; } private: //this keeps the what below hidden from other classes int power; }monster1; // access the class thru this from main int main() { monster1.fight(); // runs the fight function from class monster. monster1.die(); //calls the die function from class monster. cin.get(); return 0; }
  Last edited by ILoveVectors; 08-19-2005 at 11:02 PM.
  I was kidding. But for a monster class it would depend on what you wanted to do.
  You could have alot of variables eg. power, health, weapons, isaboss, etc. Have functions that attack players like void attack(player &to_attack); etc it is all about what you want to do.
  Woop?
  Thanks that helped alot. I have another question though, why do you use "monster1.fight();" and not just monster.fight();?Thanks that helped alot. I have another question though, why do you use "monster1.fight();" and not just monster.fight();?
  Originally Posted by ILoveVectorsOriginally Posted by ILoveVectors
  Edit: Never mind that last question i figured it out
  Last edited by GameGenie; 08-19-2005 at 11:15 PM.
  Originally Posted by prog-bmanOriginally Posted by prog-bman
  I was thinking more like multiple monsters...say rat, dog, cat, bear, pig..........................but any would work just so i get the point of classes
  What about making a class for alot of different types of monsters, each have there own stats.
  Is this a good way to use a class?...right....Is this a good way to use a class?...right....Code:#include<iostream> class monster { public: void cat() { health = 10; std::cout<<"Cat Health: " << health; } void dog() { health = 15; std::cout<<"Dog Health: " << health; } private: int health; }mon; int main() { mon.cat(); mon.dog(); std::cin.get(); }
  If i did this is there a way to change the health outside of the class or would i have to make a gethit(); function in the class to - the life away? Like when you monster gets hit, you would need to - some of the health.
  Edit: Ok i couldn't get it to work with it outside the class so i made this...
  It all worked before i added the GetHit() function, now i'm getting a error.It all worked before i added the GetHit() function, now i'm getting a error.Code:#include<iostream> class monster { public: void GetHit(int indam) { health -= indam; } void Cat() { health = 10; std::cout<<"Cat Health: " << health << "\n"; } void Dog() { health = 15; std::cout<<"Dog Health: " << health << "\n"; } private: int health; }mon; int main() { mon.Cat(); mon.Dog(); std::cin.get(); GetHit(5); std::cin.get(); }
  30 C:\Dev-Cpp\classetup.cpp `GetHit' undeclared (first use this function)
  Last edited by GameGenie; 08-20-2005 at 02:49 AM.
  Classes (and structs) are a mechanism to describe some type of object (in your case a class to describe monsters). You use member variables to describe attributes (in your case health) and member methods to describe activities. In the end analysis, all attributes will boild down to plain old data types like int, double, char, arrays, pointers, etc., though they can be combined into other classes contained within the current class etc.
  Once you have declared a class and defined any class methods, you can declare an actual instance of the the class (frequently called an object), (in your case mon). You can declare the objects immediately after the declaration of the class, as in your example, in which case the object has global scope. Or you can declare an object within a function, in which case it has limited scope. You can declare multiple objects of the same type but with different names.
  Sometimes you use one class to build other classes. In your case, you could have a Cat and Dog class that has all the attributes and methods of the monster class, plus some other attributes or methods unique to them. This process is called inheritance.
  When you use objects in your code you need to indicate which object you are using by indicating the name before the attribute/method (static methods are one exception to this rule, but we don't need to go there yet). In your case GetHit() is a member method of the monster class. Therefore you need to use it with an object, like mon.
  mon.GetHit(5);
  not alone as you did. When GetHit() is called without an object being bound to it, the compiler thinks you are calling a GetHit() function not associated with it, but there isn't one, so it spits out the error message that it is undeclared.
  You're only born perfect.
  Originally Posted by eladOriginally Posted by elad
  Oops
  Thanks alot =)Thanks alot =)