tags:

views:

214

answers:

4

After finishing my C++ class it seemed to me the structs/classes are virtually identical except with a few minor differences.

I've never programmed in C before; but I do know that it has structs. In C is it possible to inherit other structs and set a modifier of public/private?

If you can do this in regular C why in the world do we need C++? What makes classes different from a struct?

+3  A: 

It's not possible to define member functions or derive structs from each other in C.

Also, C++ is not only C + "derive structs". Templates, references, user defined namespaces and operator overloading all do not exist in C.

Johannes Schaub - litb
I know that the templates, etc to do not exist in C but I was not aware of the `power` of structs in C.So then C++ only uses structs to be 'backwards' compatible with C?
Just for backwards compatibility? On a practical basis there is probably something to that, but the distinction can be a signal of intent: where I use a `struct` I mean a largely passive POD type of thingy.
dmckee
@dmckee: For what it's worth, most STL functors (i.e. `std::less`) are defined as structs, not classes.
Billy ONeal
C++ is not fully backards compatible with C. You could say that the struct keyword is an accommodation to C developers. I like the struct keyword for classes that merely hold data in an ordered fashion but not provide (much) logic themselves.
ypnos
@ypnos: See my last comment. The only difference between the two is that one's members are default public, and the other are default private.
Billy ONeal
@Billy ONeal: Well, you could argue that STL functors are another implementation of a function pointer, which, essentially, is data ;-)) And I know of the single difference.
ypnos
+11  A: 
Antal S-Z
It also differs with the inheritance default access modifier.With a class it's private and with a struct it's public
the_drow
So in C++ this is the only difference.Thanks!
the_drow: I meant to include that, let me add it.
Antal S-Z
A: 

C++ uses structs primarily for 1) backwards compatibility with C and 2) POD types. C structs do not have methods, inheritance or visibility.

Chris Hafey
For what it's worth, most STL functors (i.e. `std::less`) are defined as structs, not classes.
Billy ONeal
A: 

Other that the differences in the default access (public/private), there is no difference.

However, some shops that code in C and C++ will use "class/struct" to indicate that which can be used in C and C++ (struct) and which are C++ only (class). In other words, in this style all structs must work with C and C++. This is kind of why there was a difference in the first place long ago, back when C++ was still known as "C with Classes."

Note that C unions work with C++, but not the other way around. For example

union WorksWithCppOnly{
    WorksWithCppOnly():a(0){}
    friend class FloatAccessor;
    int a;
private:
    float b;
};

And likewise

typedef union friend{
    int a;
    float b;
} class;

only works in C

Lance Diduck