Class and monsters

Hello, I'm pretty new to C++, but so far I have managed to figure things out from the tutorials on this site. I'm glad they were written, they have been quite helpful. But now I've ran into a problem.

I haven't actually done it yet, but I'm going to have a menu where a player selects what area they want to explore from a list. After they pick an area there is a random function that goes off, and based on the result of the random, an event will happen. Such events include finding a chest with gold in it, eating a bad berry, and running into monsters. All that I'm sure I can figure out on my own, except for the monster part.

I want to set up attributes for monsters, such as they're name, hp, level and things like that. And then when a player comes into contact with the monster function, it will run another random function and based on the result(say there are 5 monster types in the area, if the result is 3, "Crockile" or something will show up), a monster will appear. This is where the problem lies. I figured a class could be used to call the monster data that goes with the number, but I have no idea how to save the monster data or to call it with the class. I think I can figure out how to call it, but not really sure how I should save it.

I'm sorry if my description is not all that clear, I don't know the technical terms for these things. If anyone gets what I'm saying, a nudge in the right direction would be great. Thanks.

If I understand what you're wanting to do (and who knows; I'll give even money odds and take either side), then that sounds like something to be done in the constructor. The class can keep an array (or several arrays) of data, and the constructor can then call a PRNG and set up the current monster based on that result.

I guess the difference between the two approaches depends on how different the monsters are. If Monster1.attack() is going to be very different from Monster2.attack() etc., then inheritance (and all the associated virtual polymorphism stuff) is definitely the way to go. Although, cp, wouldn't variables we want to inherit have to be anything but private?

I really have no idea how classes work. I did the tutorial one on this site, and was able to change it so that it printed a name to the screen. But I'm not really what all is required for a class to work or what ways you can set it up.

cpjust, when you posted that I was like "woah" because it was a bit different than the tutorial and it kind of threw me off. But after looking at it a bit, I'm pretty sure I understand how it works. Do I need to create constructors for the two monster classes, and functions for them to display the information and then call those functions?

I'm posting this from my friends PC and wont be home for a few hours, so I cant try anything right now. That's why I'm asking these questions.

I am betting those variable inside Monster should be protected, yes, cpjust?

Oops, yes. It was very late when I posted that -- fixed.

Originally Posted by mtndewguy

cpjust, when you posted that I was like "woah" because it was a bit different than the tutorial and it kind of threw me off. But after looking at it a bit, I'm pretty sure I understand how it works. Do I need to create constructors for the two monster classes, and functions for them to display the information and then call those functions?

If you need something other than the default constructors (which it sounds like you probably do) then you need constructors for all the derived Monster classes.
You should also have functions like Attack(), RunAway(), GetInjured()... in the base Monster class and then declare them as virtual functions if you need to override them in the derived classes.

"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Actually, how about some tutorial on classes?
It sounds to me like you need to understand how classes work, and that you do not have a simple question about something you do not understand.
After you have read tutorials, then perhaps you can come back for design/implementation questions? Just so you are sure that you understand how classes work and can interpret code posted by other members... Especially cpjust now.

Originally Posted by Adak

io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.

Originally Posted by Salem

You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.