Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > difference between structures and classes

Reply
Thread Tools

difference between structures and classes

 
 
nick@no.spam
Guest
Posts: n/a
 
      08-14-2003
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


 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      08-14-2003
On Wed, 13 Aug 2003 23:50:43 -0400, wrote in comp.lang.c:

> 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 news:comp.programming 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
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      08-14-2003
wrote:

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



--
Richard Heathfield :
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
 
Reply With Quote
 
Darrell Grainger
Guest
Posts: n/a
 
      08-14-2003
On Wed, 13 Aug 2003 wrote:

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

--
main(){int j=1234;char t[]=":@abcdefghijklmnopqrstuvwxyz.\n",*i=
"iqgbgxmdbjlgdv.lksrqek.n";char *strchr(const char *,int);while(
*i){j+=strchr(t,*i++)-t;j%=sizeof t-1;putchar(t[j]);} return 0;}

 
Reply With Quote
 
Derk Gwen
Guest
Posts: n/a
 
      08-14-2003
# 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)

--
Derk Gwen http://derkgwen.250free.com/html/index.html
I love the smell of commerce in the morning.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      08-15-2003
Darrell Grainger wrote:

<snip>

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

--
Richard Heathfield :
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      08-16-2003

<> wrote in message
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between Unions and Structures... Ravikiran C Programming 7 11-03-2008 10:48 PM
Difference between Unions and Structures... Ravikiran C Programming 2 11-03-2008 11:29 AM
structures, structures and more structures (questions about nestedstructures) Alfonso Morra C Programming 11 09-24-2005 07:42 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
What is the difference between nested classes and inner classes ? Razvan Java 5 07-27-2004 07:59 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57