I have tried finding an answer to this, but most people just explain
classes as a more modular way to program. It seems to me that
(forgetting OO programming which I don't quite understand) the
structures in C are the same as classes in another language such as
C++ or Java only missing the ability to make the data private.
I've never had this explained sufficiently and would appreciate a good
answer. It doesn't need to be Mickey Mouse, but I'm not a
programmer either.
Unfortunately you have to understnad Object-orientation to understand what
C++ is doing with classes.
At the simplest level, I might define an "image" in C.
typedef struct
{
int width;
int height;
unsigned char *pixels;
} IMAGE;
Now I would write several functions to manipulate the image, all take an
IMAGE * as the first parameter
eg
void image_setpixel(IMAGE *img, int x, int y, unsigned char r, unsigned char
g, unsigned char b);
int image_save(IMAGE *img, FILE *fp);
etc.
At the simplest level, C++ classes just change the syntax slightly.
class Image
{
private:
int width;
int height;
unsigned char *pixels;
public:
Image(int width, int height); /* constructor */
~Image(); /* destructor */
void SetPixel(int x, int y, unsigned char r, unsigned char g, unsigned
char b);
int Save(FILE *fp);
};
Arguably the C++ is neater, since you don't have to disambiguate all your
image_ functions by prefixing them. However there is not much real benefit
to using C++ if we just have simple objects.
C++ comes into its own when people say " Hey, a Window on screen could be
treated as an "image" . Come to think of it, so could a greyscale image -
when you drew in colour you would translate to luminance. And what about a
circular image?"
C++ therefore allows you to derive classes from Image which inherit the
basic interface, but do different things. A Window image would physically
put up a pixel on screen when written to, for example, the greyscale would
translate the colour passed to grey, the circular image would exclude pixels
drawn outside the circle.
This is the real advantage of the C++ class - it allows for inheritance
which is the heart of OO programming.