Classes

closed account (GbX36Up4)
I don't really see how classes make things easier. Let's say I want to make a class for beeps where depending on what numbers the user inputs it makes a beep with those integers, so if they entered 500 and then 100 it would be Beep(a,b); where a is 500 and b is 100. Can't i just use a function?
It depend on the problem on hand. Based on what you described, I think a function would suffice. To me classes are only useful in 'big' program. Of cuz the term 'big' is subjective to each developer.

For OO language like Java, to test something simple, I have to write a class with a public static void main method in it. This is just plain overkill in my opinion but hey I am 'forced' to use class by the language!
Let's say I want to make a class for beeps where depending on what numbers the user inputs it makes a beep with those integers, so if they entered 500 and then 100 it would be Beep(a,b); where a is 500 and b is 100. Can't i just use a function?


What you're describing is a function, not a class. If you want to "do something", you probably want a function.

Classes typically don't "do" stuff, they "represent" stuff.

The classic example is std::string. If you didn't realize it, that is a class. It represents a string. You can create as many strings as you want by creating instances of that class. Each instance behaves as if it were a string.

Now, sure, you can accomplish more or less the same thing with functions alone (strcpy/strcmp/strlen/etc functions), but those are more error prone and more difficult to use than the string class.


Coming back to your Beep idea.... a Beep class wouldn't simply make the computer beep. It would BE the beep. You might be able to do stuff like this with it:

1
2
3
4
5
6
7
8
9
Beep mybeep;
mybeep.SetVolume( foo );
mybeep.SetFrequency( foo );

mybeep.Play();  // start actually playing it
mybeep.SetFrequency( bar );  // change the frequency (as it plays)
mybeep.FadeOut( bar );  // fade out over a period of time

//etc 


So why do you need the Beep class? Why not just have SetBeepVolume/SetBeepFrequency/etc functions? Simple! Having it in a class objectifies it, which allows you to have any number of beeps.

So you could have two beeps playing at the same time:

1
2
3
4
5
6
7
8
9
10
11
Beep a;
Beep b;

// play them both
a.Play();
b.Play();

// fade out 'b', but keep playing 'a' normally
b.FadeOut( foo );

//... etc 




To me classes are only useful in 'big' program.


I'm sure you use string and vector even in simple programs. ;P

But of course if you meant "writing your own classes is only useful in big programs" then that's true to a point.

Typically most programs (even small ones) have ideas that can be objectified and put into classes. Whether not it's worth it to do that depends. If you just want a quick-n-dirty program that's 1 file and only maybe 200-300 lines then it probably isn't worth it to objectify stuff. But when you start spreading your ideas across multiple files, objectifying really helps bigtime.
Typically most programs (even small ones) have ideas that can be objectified and put into classes. Whether not it's worth it to do that depends. If you just want a quick-n-dirty program that's 1 file and only maybe 200-300 lines then it probably isn't worth it to objectify stuff. But when you start spreading your ideas across multiple files, objectifying really helps bigtime.


Agree.

There was once I read some article elsewhere where they argue on the merits of a class vs functions. Their proposal is simple. Count number of lines to write a Hello World in both function and classes style. They uses C and Java for comparison. Albeit this is too trivial a case but I think the idea they are trying to bring across is for simple stuff using a class is plain overkill.
Topic archived. No new replies allowed.