Langy said:
Hello
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?
There is no "need" to use classes. In the same way that there is no
need to use a compiler. You can encode machine language instructions if
you wish, it would be an awful waste of time, however.
Perhaps you could also give an example on when it might be used rather
than an alternative method.
Classes came about because programmers recognized that with data
structures, you usually associate a set of functions. After this point,
it became clear that you could make classes contain their own scope,
i.e. it can contain it's own types and nested classes. Still, there are
times when functions deal with multiple classes, you can't really
associate a function as a "member" of the class.
Then comes the idea of "polymorphism" - i.e. classes as interfaces when
the underlying implementation is not important, but how to use the class
is. Hence you can write one piece of code that can be used for varying
types of implementations. This is all about reducing the amount of
work. This is one of the foundations of "reusablility". Code re-use is
important because you'll find yourself re-writing at least 60% of your
code every time you do a new project.
When you start to formalize classes, there are two very important
operations that need to be well defined, construction and destruction.
C++ has some well defined rules as to how this works and it can be the
cause of some very interesting surprises if you don't grasp it fully.
(said by someone who probably will get some new surprises!).
Anyhow, when you consider operators, conversion operators, templates and
the rest of C++ into the picture, you'll start to understand how classes
are the fundamental building block of C++. In C++ consider using
classes at a very low threshold because they are very efficient
abstractions and most compilers will generate code that is just as fast
as anything you could write without them if you decided not to use classes.
As for your example. Look at the Unix (Posix) file API. A file is
represented as an "int".
int open(const char *pathname, int flags);
Well, "int" is not very useful as it can be confused with other things.
Does it really make sense to add 2 file descriptors ?
int fa = open( "a.file", O_RDWR );
int fb = open( "b.file", O_RDONLY );
int x = fa + fb; // no compiler error - but meaningless !
write( fb, "A", 1 ); // run-time error (not even checked !)
Consider using classes.
RdWrFile fa( "a.file" );
RdFile fb( "b.file" );
fa + fb; // compiler tells you about an error
fb.write( "A", 1 ); // invalid write to RdFile - compile error
Hence, well written interfaces allow the compiler to pick up errors at
compile time. In software development, the earlier you catch errors,
the more productive you are (i.e. the less costly the errors are!).
Hence errors caught at compile time are the least expensive errors.
(Yes, there are a class of errors that can only be caught at run-time,
this is what unit tests are for. Errors caught in the field can be
extremely costly to the extent of loss of life.)
Some interesting links about software development at it's worst !
http://catless.ncl.ac.uk/Risks/
http://www.cis.gsu.edu/~mmoore/CIS3300/handouts/SciAmSept1994.html
G