473,503 Members | 9,735 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

difference between structures and classes

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
Nov 13 '05 #1
6 7649
On Wed, 13 Aug 2003 23:50:43 -0400, ni**@no.spam 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
Nov 13 '05 #2
ni**@no.spam 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 : bi****@eton.powernet.co.uk
"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
Nov 13 '05 #3
On Wed, 13 Aug 2003 ni**@no.spam 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;}

Nov 13 '05 #4
# 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.
Nov 13 '05 #5
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 : bi****@eton.powernet.co.uk
"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
Nov 13 '05 #6

<ni**@no.spam> 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.
Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2716
by: Fred Bennett | last post by:
I have a simulation project in which data can naturally be held in structures for processing. There are calls to multiple functions involved. Execution speed is an issue. Do I take a big hit for...
1
2714
by: kazack | last post by:
Hi all it's me again with another question as I got further in my book. The chapter I am in covers structres, abstract data and classes. I only read through to the end of the coverage on...
3
1324
by: ambar.shome | last post by:
Hi all, I have a very basic fundamental wuestion to ask. I am confused about the difference between type and class? is "int","float","double" a type? With so many C++ gurus out here, I dont...
22
3318
by: Ook | last post by:
We have had a discussion on the differences between a class and a structure, and no one is in agreement. As I understand it, a structure defaults to public, a class defaults to private. There are...
5
8722
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but...
7
1258
by: OpticTygre | last post by:
Alright, so I'm messing around with some code, and I brought up a good question to myself. If creating a class called "Person", and filling that class with variables, properties like: Public...
4
1346
by: zacks | last post by:
A common programming technique I use in VB is making a collection of structures. But if Option Strict is on (which I would prefer), the .Add that adds the structure to the collection is flagged...
2
2393
by: thomasfarrow | last post by:
At work, our development team has a development standards document that insists Structures should never be used. I'm looking to change this standard but need a suitable argument in order to make...
13
3325
by: dmh2000 | last post by:
I am experimenting with the interactive interpreter environments of Python and Ruby and I ran into what seems to be a fundamental difference. However I may be doing something wrong in Python....
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7095
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7294
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7015
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4693
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3183
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
749
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.