"Langy" wrote:
I'm fairly new to C++ but have programmed several other languages and
found most of c++ fairly easy (so far!).
I've come to a tutorial on classes, could someone please tell me why
you would need to use a class?
You're familiar with struct's, aren't you? Well, classes are really
just slightly super-charged versions of C structs. In fact, "class" and
"struct" in C++ vary only in default access specification. (struct
defaults to public, class to private.)
Classes are most useful in programs that deal with classification,
especially hierachies of classification.
But even if you're not classifying things, classes can be very useful.
I like 'em because they allow me to bundle the data being worked-on
tightly with the methods used to work on that data. Further, they
allow me to specify somes methods and data to be "public" (so that
any function can access them) and some "private" (so that only member
functions of that class can access them). And, they provide excellent
separation between interface and implimentation for improved modularity
and ease of maintainance. And, class objects provide repositories for
data that the programmer would otherwise be tempted to put in global
variables, which is usually a bad idea.
And perhaps most of all, I love the way that classes clean-up messy
source code. For example, starting last Friday night, I starting
writing a program to count all lines of C/C++ source code in a
directory tree. Use of classes helped speed my programming (allowing
me to finish it in 2 days) and helped keep my code clean.
I first made a class to hold data on lines of source found so far
in this tree:
namespace SourceLines
{
...
class SourceTree
{
public:
SourceTree (void) : Lines(0UL) {}
void CountLines (void);
void PrintLines (void);
void CountLinesInFile (const rhdir::FileRecord& File);
void CountLinesInCurDir (void);
private:
unsigned long int Lines;
};
...
}
Then I used this class in main() like this:
int main(void)
{
using namespace SourceLines;
SourceTree Tree;
Tree.CountLines();
Tree.PrintLines();
return 0;
}
That's my whole main() function! I promise you I didn't shorten
it in any way for this post! Classes just make code that clean.
So what did I do there? First I made an instance (an "object")
of class SourceTree called "Tree". Then I called a couple of
SourceTree member functions through Tree, which did all the work.
Countlines() counted up all the source lines and put the total in
variable "Lines" in object "Tree". PrintLines() then just printed
the value of Lines. Done. Exit.
Without classes, my main() would have been 500 lines instead of
5 lines, and the whole flow and concept would be burried under
all that code.
I usually write class declarations first; then write main(),
laying out the sequence of things to be done; then finally
impliment member functions. Basically, a "top-down" approach.
This keeps the messier code confined to low-level subroutines,
leaving main() and higher-level subroutines much more readable.
So in the end, my answer to your question "why would you need
to use a class" is "Because they make programming easier".
--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant