• Advertisement

Archived

This topic is now archived and is closed to further replies.

why should i use classes etc?

This topic is 6527 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i must be a really lamer when i ask this question: why should i use classes ?I mean everything that classes can do, functions can also do it! I dunno if its possible to answer in a not-joking way but please try to describe it ! i am new to C++, i used C before !

Share this post


Link to post
Share on other sites
Advertisement
Heh, I''ll save the jokes for later

Anyway, member functions in a class allow each different class object you create to contain its own set of personal functions, that only act on itself, instead of a general function. Also, due to derivation, polymorphism, etc., classes can be real cool, and allow your code to be more efficient!

Share this post


Link to post
Share on other sites
From a purely design orientation classes are cool because you can view things as an object, just like you would in life. In the world you have a dog, a dog can eat, sleep and run. These are all functionality of a dog. That may not seem at first like a big advantage, but wait until you start working with lots of code. Having everything objectified makes it a breeze to use your code base with out confusion. If your moving from C classes will not take you that long to learn and will save you a lot of pain once you get used to them.


Mike Barela
mbarela@earthlink.net

Share this post


Link to post
Share on other sites
exactly. it''s an organization tool that simplifies everything during big projects like:

class enemy
{
position[x,y,z]
orientation[x,y,z]
speed
state
etc
}

a2k

Share this post


Link to post
Share on other sites
srmeister, I recommend getting a good beginning book on C++. They offer much more than just organization. There are several good tutorials on the net too, but for some reason they''re strangely absent from the gamedev.net programming reference section.

Brian

Share this post


Link to post
Share on other sites
Well, like the previous example classes are good for enemies, they can hold their data like speed, position, hitpoints. Structs can do that to, but with classes you can have functions in them, (like drawing them, running their AI etc.)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
structs can have funtions in them as well

Share this post


Link to post
Share on other sites
That''s very very wrong, considering that a structure (struct) is the same thing as a class in c++, just that it defaults to public instead of private.
Note: The Very Same Thing.

Classes do a lot. Polymorphism. Inheritance. Data Hiding. Read a book, honestly. There''s simply too much.


A polar bear is a rectangular bear after a coordinate transform.

Share this post


Link to post
Share on other sites
Actually, Armitage, I''m pretty sure that structs are just like classes only in C++, they are not in straight C. So, it should be stressed that you are sort of MEANT to use classes if you want to use any real object oriented features...
Not that it really matters though

------------------------------
Jonathan Little
invader@hushmail.com
http://www.crosswinds.net/~uselessknowledge

Share this post


Link to post
Share on other sites
Heh, just for the record I added the "in c++" as an afterthought. srmeister had coded c, so I thought he''d realize there''s a difference between a c struct and a c++ struct.

Share this post


Link to post
Share on other sites
i would have to say that inheritance is one of the biggest reasons to use classes. it makes things a heck of a lot easier instead of writing 50 functions for enemies, you could write 1 base class, and 50 derived classes (which are probably smaller to write that cut and pasted functions)

Share this post


Link to post
Share on other sites
You also get compartmentalization almost automatically. I use just one member function to display an object on the screen. And I can initialize, manipulate and debug the other member functions without having to worry about having to debug around the fullscreen control of DX. That structuring is good and practically forced upon you.


ZoomBoy
A 2D RPG with skills, weapons, and adventure.
See my character editor, Tile editor and diary at
Check out my web-site


Share this post


Link to post
Share on other sites
jesus, i just didn''t wanna confuse or scare the guy from using classes. "polymorphism," cmon.

a2k

Share this post


Link to post
Share on other sites
quote:
Original post by srmeister

i must be a really lamer when i ask this question:
why should i use classes ?I mean everything that classes can do, functions can also do it!


And everything C can do, Assembler can do it! It''s just a way of being able to look at your program from a higher level. This means that the compiler can catch more errors for you and you can develop more quickly. I find those to be good enough reasons - a good C++ book or reference will explain more.

Share this post


Link to post
Share on other sites

  • Advertisement