9

Possible Duplicate:
What are the differences between struct and class in C++

I've done my homework and had diverse answers on Google. Some say structs do not have inheritance, some say structs do not have access specifiers, while others say they have both.
Could someone clarify then, the differences between a struct and a class in C and C++, and also the difference between a struct in C & C++.

Siddhartha
  • 4,296
  • 6
  • 44
  • 65

2 Answers2

23

In C++, the only difference between a struct and a class is that struct members are public by default, and class members are private by default.

However, as a matter of style, it's best to use the struct keyword for something that could reasonably be a struct in C (more or less POD types), and the class keyword if it uses C++-specific features such as inheritance and member functions.

C does not have classes.

C structs cannot use C++-specific features.

EDIT:

The C++ FAQ Lite, question 7.9, has this to say:

The members and base classes of a struct are public by default, while in class, they default to private. Note: you should make your base classes explicitly public, private, or protected, rather than relying on the defaults.

struct and class are otherwise functionally equivalent.

OK, enough of that squeaky clean techno talk. Emotionally, most developers make a strong distinction between a class and a struct. A struct simply feels like an open pile of bits with very little in the way of encapsulation or functionality. A class feels like a living and responsible member of society with intelligent services, a strong encapsulation barrier, and a well defined interface. Since that's the connotation most people already have, you should probably use the struct keyword if you have a class that has very few methods and has public data (such things do exist in well designed systems!), but otherwise you should probably use the class keyword.

And quoting Stroustrup's "The C++ Programming Language", 4th edition, section 16.2.4:

These two definitions of S are interchangeable, though it is usually wise to stick to one style. Which style you use depends on circumstances and taste. I tend to use struct for classes that I think of as "just simple data structures." If I think of a class as "a proper type with an invariant," I use class. Constructors and access functions can be quite useful even for *struct*s, but as a shorthand rather than guarantors of invariants.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • 4
    -1 style point is opinion and highly debateable – John Dibling Oct 14 '11 at 01:24
  • @JohnDibling: Yes, it's an opinion. Do you have any support for a different opinion? – Keith Thompson Oct 14 '11 at 01:26
  • Structs don't have inheritance then? – Siddhartha Oct 14 '11 at 01:29
  • 1
    @Siddhartha: Sure they do. As a matter of style (*in my opinion*) they shouldn't use inheritance, but they can. – Keith Thompson Oct 14 '11 at 01:32
  • Almost all of MS's STL code uses structs pretty much everywhere for meta-programming. I would guess that the reason is that it incurs less typing, as most functors only have public members. Given that almost all code I see has the public members at the top of the class definition anyway, I think that the `class` keyword should really just have been a `#define class struct`. – Ayjay Oct 14 '11 at 01:50
  • +1 right to the point. The recommended style is nice too – Ulterior Oct 14 '11 at 02:16
  • Structs "feeling" like bags of bits aside, I posit that no professional C++ programmer worth his salt will be thrown by a structure having methods and private data. Or at least they should not. – John Dibling Oct 14 '11 at 02:50
  • 1
    @JohnDibling: By all means post an answer stating your opinion. I'm not saying that a programmer should be "thrown" by such a struct, just that, in my opinion, it should have been declared as a `class`. Yes, it's just an opinion (style issues generally are) -- but I believe it's an opinion shared by most C++ programmers. – Keith Thompson Oct 14 '11 at 08:26
  • @JohnDibling: (I've *finally* gotten around to looking this up) -- it appears that Stroustrup mostly agrees with me. I'm not claiming that that settles the question, but I think it does suggest that it's a reasonable preference. – Keith Thompson Oct 11 '13 at 15:43
  • 1
    @KeithThompson: Wow, diggin' up an medium-oldie. While I do not challenge the validity of your opinion, or that of Our Holy Father, I *still* do not agree with the style point. My stance is somewhat different, though admittedly not space-alien different from yours. I didn't feel like I had the energy to enter in to this debate years ago, so didn't post a dissenting answer. Perhaps I will now. In the meantime, I *do* think it's reasonable for me to revert my downvote as your opinion is completely valid in my view. – John Dibling Oct 11 '13 at 15:55
3

In C, classes do not exist. In C++, structs have a default access specifier of public, while classes default to private.

There are several differences between structs in C and C++; in C++ they can inherit from other classes or structs, they can contain member functions, and their names don't need to be referred to with an elaborated type specifier.

Hugh
  • 8,872
  • 2
  • 37
  • 42
  • kk so structs do inherit..from other classes and structs? how bout if a class tries to inherit from a struct? – Siddhartha Oct 14 '11 at 01:31