Question:
What's so great about Classes in C++?
Shane Freeman
2009-08-19 19:08:22 UTC
I'm in a C/C++/C# class this semester and have been doing a lot of studying to get ahead. I can create a Class object but I don't understand why they're so great. Can't functions outside main do almost the same thing? Are they for some sense of organization? I'm not exactly clear on the subject of classes.
Three answers:
meow
2009-08-19 22:37:46 UTC
Shane, good question and I've seen it asked here quite a few times. There are a lots of reasons: lower memory image, increased speed, better maintainability, reuse, very amenable for time-slicing and multi-threading, "cleaner," inheritance, encapsulation, etc. (lots of stupid buzz words).



This is a really long answer and I hope you can follow most of it.



Depending on your background, this may make some sense to you: Let's say you have a job that requires you to drill three different holes in three different gizmos that come off an assembly line in random order. Gizmo A requires a 16 size bit, B requires a 20, and C requires a 32. With one drill you select the bit depending on the gizmo, chuck it up, drill the hole, un-chuck it, select a new bit for the next gizmo, etc.



You tell your boss, "this is dumb." So, you get three drills and chuck them up once when you get to work and un-chuck them before you go home. Great, problem solved.



Now your boss says the assembly line will start outputting 50 different gizmos requiring 50 different size holes. Crap. Organizing 50 different drills for each gizmo is going to be a nightmare and you just don't have the work space for all those damn drills.



This is where we move from reality into object oriented programming and magic.



If you could define the 1) chuck, 2) drill hole, and 3) un-chuck actions as "Gizmo" class functions and bit number as a member variable, life would be easy. All you need to do for each gizmo is to initialize the class, set the drill bit number, chuck the bit (just once) and you're good to go to town and drill. You un-chuck just once when you destroy your object.



For the entire Gizmo class (50 objects), the drill function is in reality just one drill (machine code) for the 50 objects. It actually occupies just one code space in physical memory--not 50. Likewise, there is just a single chuck and un-chuck function in memory--not 50.



This is the beauty, once you set the bit number and chuck it for each gizmo object, the drill function will be set for the specific gizmo you're drilling. Just identify the specific object and drill knowing that the bit number is correct and it has been properly chucked up.



So, you now have a magic drill. As you bring it to different objects (gizmos), the correct bit is already chucked. Wow, life is really good. Object oriented programming and classes are pretty handy.



I give you lots of credit if you followed the above. Good luck.
Pyros7
2009-08-19 19:23:39 UTC
Class objects are encapsulated and reusable. Which is incredibly important for any large organization to avoid hundreds of functions calls that do almost the same thing. Many functions will be able to call that class for various needs.



On top of that, a class and a function serve different purposes. A class defines an object, functions preform actions upon something like a class.



Think of a deck of cards. The class object might be the deck of cards. It has 52 items of various suits and values and colors. Functions would be used on that class to shuffle the deck, draw a card, etc. But the deck remains the same. There's no point in redefining a deck of cards whether you're writing a program to deal poker, blackjack or go fish.



Long story short, if you're in a programming class, the programming theories are far more important than the language. Language is just syntax, which is easily learned. Theories are far more valuable.
zelmo_t
2009-08-19 21:27:43 UTC
The first nice thing about classes is that they encapsulate both identifiers (constants and variables) and functions. This is great for organization, since you can define something like a cat as a class, with identifiers for things like name, gender, color, etc., and functions for things like walk, sleep, scratch, purr, etc. You can get kind of the same effect in C by putting all that stuff in the same namespace, but C++ lets you have several classes within a namespace, so there's an extra (and more intuitive) level of organization.



The second nice thing about classes is that you can inherit from other classes. So you can have, for instance, an animal class with identifiers for things like name, gender, color, etc., and functions for things like walk, sleep, scratch, etc. Then your cat class can inherit from your animal class, and those identifiers and functions are automatically part of your cat class, so all you have left to define is cat-specific stuff like purr. If your base class allows for polymorphism, you can have its functions do some very generic stuff (e.g., "walk" simply moves the animal from point A to point B) while the derived class gets more specific (e.g., "walk" moves the cat on four legs with its tail in the air).



The third nice thing about classes is that they let you define an interface for something your program needs to work with. This goes along with polymorphism. For example, say your program runs a farm, which has lots of animals, and you periodically want your animals to walk, sleep, or scratch, and you also want to show the animal's name. To do this, you can simply declare a collection of animal objects, and assign each one a different kind of animal (polymorphism lets you assign derived objects to base class declarations). Since all animals have functions for walk, sleep, and scratch and a variable for name (remember, those are defined in the animal class), you can just loop through your animals and call whatever animal function you want with them. Thanks to polymorphism, if the derived class (like cat) defined a more specific way to do the function, that's the one that will get used.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...