structure or class?

can someone tell me the difference between a structure and a class?
In C++ the only difference as far as I know is that with a class member variables are private by default and with a structure they are public by default.
Last edited on
Also, if unspecified, inheritance in classes private by default and in structs is public by default.
there have to be more differences than that?
No.

EDIT: Ok, there used to be more differences, if you are using a really, really, really old compiler. So old that it's not even worth mentioning what the differences were, as it will just confuse rather than clarify.
Last edited on
Structs can only hold data members if programming in C I believe.
Last edited on
The keyword "class" does not exist in C.
Unless I'm mistaken there is also a way to initialize a struct with a special syntax, similar to how you can initialize an array, that will not work on a class.

EDIT: both classes and structs did work for this with gcc. (I probably forgot to make the members public before trying it with a class)
Last edited on
This compiles fine for me:

1
2
3
4
5
6
7
8
9
10
class foo {
   public:
       int x;
       float f;
       char c;
};

int main() {
   foo f[] = { { 1, 3.13, 'c' }, { 2, 3.14, 'd' } };
}


If I am not mistaken you can't declare functions in a struct, while you can in a class; which is a pretty big difference.
In C you can't put functions into the struct but in C++ you can!

http://carcino.gen.nz/tech/cpp/struct_vs_class.php


Topic archived. No new replies allowed.