what is 'class'?

closed account (367kGNh0)
when I watch c++ tutorials. The presenter likes to say this a lot:

'For (example: sound effects) I like to make a whole new class'.

'class'es sound important so I would really like to know what it means.

-Thanks
A class is a definition of a "kind of thing".

I suggest taking these two tutorials on this site
http://www.cplusplus.com/doc/tutorial/structures/
http://www.cplusplus.com/doc/tutorial/classes/

I also wrote a brief description at one point:
http://www.cplusplus.com/forum/beginner/213266/#msg995536
Last edited on
at its most basic top level, a class is a user defined type, like int.
if you had a class thing, then in your code you can say

thing x;
just like you can say
int y;

There is a LOT more to it, as you will learn, but that is the starting point.

after you get into the language, 95% of everything you do will involve a class. They are very important :)
Last edited on
The int type is built into the language and is not a class, but you can write your own class that that behaves more or less the same as int if you want. std::string is an example of a class that you might have used.
I like the analogy of it just being a custom type (that's basically how it's used in templates). Another important aspect is functionality. A class isn't just a kind of thing, a class also does stuff.

You mentioned sound effects. Perhaps, in a game, you want a sound effect to be played upon getting an item. Item and SoundEffect could both be classes. But what does a SoundEffect do? A sound effect can play a sound. Pretty simple, just from a functionality point of view.

Your code, might be as simple as this:
1
2
SoundEffect sfx("my_sound_effect.wav");
sfx.play();


A class defines a thing that has functionality self-contained in it.

Furthermore, what's good about this class is that, if designed correctly, I don't (generally) need to know how that class is implemented under the hood. What I do know about is its public interface (I can make a sound effect by loading a music file, and I can play that sound). [In reality, there might be more customization you want, but these are all features which can be added.]
Last edited on
Topic archived. No new replies allowed.