difference between structures and classes

N

nick

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.

Thanks,
Nick
 
J

Jack Klein

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.

Thanks,
Nick

C++ and Java are completely off-topic here, as are all other languages
besides C. The C language doesn't define an interface to any other
language, or acknowledge their existence.

In this group, nobody knows what a C++ or Java class is, or what it
means to make data private.

If you want comparisons of C and other, later languages derived from C
to a greater or lesser extent, you need to ask in either a
non-language specific group like or in groups
for those descendent languages such as comp.lang.c++ and
comp.lang.java. They define their similarities and differences to C,
and they define linkage to C.

The information does not go in the other direction.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
R

Richard Heathfield

I have tried finding an answer to this, but most people just explain
classes as a more modular way to program.

It is possible, and indeed common, for classes to be a /less/ 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.

Not so. Structures, in C, can easily be used to make data private, if you
know what you're doing. (No, using the C++ "private" keyword doesn't work
in C!)
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.

In C, a struct is a collection of one or more data items, defined like this:

struct structtypename
{
char grill;
double cross;
int eresting;
float downstream;
long drive;
};

The idea is that, when you need to treat this stuff as a group, you can, and
when you need to get at the insides, you can do that too. And if you want
to get at the insides, but don't want other people to get at the insides
(i.e. make the data "private"), it's easy - don't publish the struct
definition. Just publish the typename:

struct structtypename;

and a set of interface functions, one of which delivers a pointer to a new
object of this type, and the rest of which take a pointer, much like the
stdio FILE interface functions (fopen, fread, fwrite, fclose, etc). If you
want more info on these "opaque types", ask for it and I'll cheerfully
natter on about it for hours.

<Off-topic>
By a curious coincidence, C++ structs /can/ be defined in precisely the same
way, but also have extra goodies which are off-topic here. In C++, there is
precious little difference between structs and classes, and in fact many
C++ programmers type "class foo { public:" instead of the exact equivalent
struct foo {", presumably on the grounds that they enjoy typing.
</Off-topic>
 
D

Darrell Grainger

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.

The comp.lang.c newsgroup is for discussing the C programming language.
Since there are no classes in C the most I can say is that structures
exist in C and classes do not.

I know a little about C++ as well and would suggest you ask the question
in comp.lang.c++. The C++ language has both structures and classes. They
should have opinions on what the differences are.

I am under the general impression that a structure holds a group of
related data. A class holds a group of related data and methods for
interacting with that data.
 
D

Derk Gwen

# 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.

An object class is tightly coupled combination of data and code, operations on
data are included with and linked to the data. A C structure is typically just
data; when a C structure includes function pointers, the language does not
couple the functions to the structure containing them: it is the responsibility
of the programmer to do so.

You can simulate single inheritance in C with a disciplined use of functions
whose first parameters are always, perhaps, a pointer to the structure of data
and a static structure of function pointers.

A is a class with data members x, y, z and functions X, Y, Z.
B is a subclass with data members p, q, r and functions P, Q, X.

typedef struct A A; typedef struct A_Behaviour A_Behaviour;
typedef struct B B; typedef struct B_Behaviour B_Behaviour;
int A_X(A *this,int i);
int A_Y(A *this,int j);
int A_Z(A *this,int k);
int B_P(B *this,int i);
int B_Q(B *this,int j);
int B_X(B *this,int k);

struct A_Behaviour {
int (*X)(A *this,int i);
int (*Y)(A *this,int j);
int (*Z)(A *this,int k);
};
struct A {
Root super;
A_Behaviour *behaviour;
int x;
int y;
A *z;
};
static A_Behavior = {A_X,A_Y,A_Z};
void new_A(A *a) {
new_Root(&a->super); a->behaviour = &A_behaviour;
a->x = 0; a->y = 0; a->z = 0;
}

struct B_Behaviour {
int (*P)(B *this,int i);
int (*Q)(B *this,int j);
int (*X)(B *this,int k);
};
struct B {
A super;
B_Behaviour *behaviour;
int p;
A *q;
B *r;
};
static B_Behavior = {B_P,B_Q,B_X};
void new_B(B *b) {
new_A(&b->super); b->behaviour = &B_behaviour;
b->p = 0; b->q = 0; b->r = 0;
}

For an object of class B, you call method
b->P with b->behaviour->P(b,i)
b->Q with b->behaviour->Q(b,j)
b->X with b->behaviour->X(b,k)
b->Y with b->super.behaviour->Y(&b->super,j)
b->Z with b->super.behaviour->Z(&b->super,k)
 
R

Richard Heathfield

Darrell Grainger wrote:

I know a little about C++ as well and would suggest you ask the question
in comp.lang.c++. The C++ language has both structures and classes. They
should have opinions on what the differences are.
Right.


I am under the general impression that a structure holds a group of
related data. A class holds a group of related data and methods for
interacting with that data.

So can a struct (in C++). All the more reason to ask C++ questions in a C++
newsgroup. :)
 
M

Malcolm

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,187
Latest member
RosaDemko

Latest Threads

Top