473,659 Members | 2,987 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 7661
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.progr amming 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.l earn.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.pow ernet.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[]=":@abcdefghijk lmnopqrstuvwxyz .\n",*i=
"iqgbgxmdbjlgdv .lksrqek.n";cha r *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.behaviou r->Y(&b->super,j)
b->Z with b->super.behaviou r->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.pow ernet.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(IMAG E *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
2728
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 usuing structures? Is there a preferred way of passing the structures (or pointers?) that would speed up the program? I'm relatively new to C++. so, if you have time, detail (or references) would be appreciated. In any case any help is much...
1
2725
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 structures. Trying to comprehend this is harder than I thought it was going to be. I should of just skipped this chapter and went right into pointers since they seem to be easier to use. But anyways here i smy question: you define a structure...
3
1337
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 know whether should I ask this question here or not? But if anyone can help me out, I'll feel good.
22
3346
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 issues about constructors that I'm not clear on. I do know that I can take a simple project with a class that has constuctors, destructors, accessors, and modifiers, change the classes to structs, and it compiles and runs fine. Can some kind soul...
5
8726
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 conceptually speaking : when do I define something as 'struct' and when as 'class' ? for example : if I want to represent a 'Time' thing, containing : - data members : hours, mins, secs
7
1264
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 Class Person Private mstrName As String Private mdtBirthDate As Date
4
1351
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 with a compiler error (invalid type conversion). Is there a way to use a collection of structures WITH the Option Strict On?
2
2404
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 the change. I know that Structures are value types, sit on the stack, and are generally more efficient to manipulate than reference types (i.e. Classes). Structures cannot use inheritance, the finalize method or default constructors. Can anyone...
13
3346
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. Please comment and correct me if I am wrong In both languages, you can start up the interactive interpreter ('python' and 'irb'), load source files and do stuff, like create objects and call their methods. When you want to change something, you can...
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8747
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8528
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8627
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5649
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2752
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.