When to use classes

Hey. I've been learning the basics of classes and have learned:

declaration and function implementation of classes
object instantiation: constructors and destructors
member variable and functions: public vs private, set/get functions
passing objects to functions
object composition

I understand the concepts and can write code to perform them, but when I try and practice, I cannot figure out when to use classes. Could someone shed some light on this? Thanks.
Easiest way: Think of computer games.

Why? A class is used to describe real-world objects. What objects can you describe in a game? The main character, monsters, vehicles, weapons, etc.... It depends on your imagination!

Start from a gaming perspective and very soon, you'll expand your horizons to knowing where to use classes. In fact, classes are used in almost EVERY program to describe almost anything.

Joe - C++ tutor
functions are pieces of codes that you will execute multiple times. a class is used to represent:
a) data types (for example me and IWishIKnew are working on a class for representing binary strings)
b) ast's (dont worry about it. just felt like making this as complete as possible)
c) exceptions (for try-catch blocks)
d) a container (std::vector, std::string, std::list)
@Little Captain

Ok... I was trying to plan a battleship game with text-based graphics in the console today.

I tried using noun-verb analysis like my book taught me, and i narrowed it down to three potential classes:

players
maps
ships

I was thinking of having the ships class compose the players class. Does that make sense?

What I don't understand at all is the maps class. Should I make the map compose the players class as well; I really don't know.
Last edited on
Think of it this way. In the game, the player controls a ship. So, the ship class can represent the player as well.

The map is actually an object that "contains' ships. (The ships are on a map, therefore the map contains ships.)

If you need help with this game, I'd be happy to help you and coach you as I actually have experience making games from the ground up.

Joseph
Oh... So I won't need a player class at all; that makes sense.

And that would be awesome, thank you! I will work on the game more and see if I can figure it out.

Thanks for your help!
You can check this out:
http://gameprogrammingpatterns.com/index.html
You can find useful programming patterns there, and maybe when you see how they work, you'll know when to use them, and when not.
Cheers!
@Mat

That is awesome, thanks!
If you need help, just throw me an email at:
sparkyqian@yahoo.ca

Joe
Topic archived. No new replies allowed.