2

Using struct we can achieve all the functionalities of a class:

  • constructors, destructors
  • member functions, static functions.
  • overloaded functions, virtual functions
  • public / private / protected access specifiers.
  • operators

The only difference are the default access rights: private for class, public for struct.
Why do we need a Class then ?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
cprogrammer
  • 5,503
  • 3
  • 36
  • 56
  • 4
    See http://stackoverflow.com/questions/92859/what-are-the-differences-between-struct-and-class-in-c for an extensive list of differences between `struct` and `class` – Gregory Pakosz Aug 23 '11 at 09:12
  • @Gregory: i know the difference between struct and class. I'm interested why class was added in C++ ? – cprogrammer Aug 23 '11 at 09:16
  • 3
    Given that the OP already mentions in this question the difference between `class` and `struct`, it does *not* look like a duplicate and seems more targetted at the rationale behind the introduction of a new keyword. – Matthieu M. Aug 23 '11 at 09:32
  • This question is more or less purely speculative. Unless someone actually is Bjarne Stroustrup, or was otherwise intimately involved in the development of C++ before C++98, there won't be an actual answer that is anything more than speculation. Just look at the "answers" we already got to see the general quality one could expect if this was reopened. – Nicol Bolas Aug 23 '11 at 09:41
  • @Nicol: That is correct. But it also gives a change to think how Bjarne thinks, so as to speculate why he introduced the keyword. Just pretend to be Bjarne when he is not around. It feels good pretending to be the creator of C++ :P – Nawaz Aug 23 '11 at 09:44
  • @Nicol: in this case, maybe there are no conclusive answers yet, but there have been other C++ rationale questions where it has been possible to quote defect reports (or other publications of the C++ committee), or "The Design and Evolution of C++" (or other publications by Stroustrup) to give answers. Those aren't purely speculative, since documentation exists where the designers of C++ have shown their reasoning. So I don't think this is purely speculative either, even if it happens that for this question no documentation exists (which I doubt). – Steve Jessop Aug 23 '11 at 11:17
  • After some research i have found on Bjarne Stroustrup's FAQ a posibile answear: "A class is the representation of an idea, a concept, in the code." http://www2.research.att.com/~bs/bs_faq.html. while struct means data. So, i suppose this is the only reason. – cprogrammer Aug 23 '11 at 12:23

8 Answers8

5

IIRC when C++ was created (as C with classes), a design goal was to only ever add functionality, so the initial idea was to have a class as in C++, and a struct as in C. But then things got blurred, and in the following 20 years struct "mutated" into some kind of class.

PlasmaHH
  • 15,673
  • 5
  • 44
  • 57
  • would you (by any chance) have a citation from the *Design and Evolution of C++* ? I wonder if Stroustrup mentions a rationale for this particular point there. – Matthieu M. Aug 23 '11 at 09:27
  • Unfortunately I don't, but he is talking in there about structs in C not forming nested namespaces, whereas classes do. – PlasmaHH Aug 23 '11 at 10:28
3

Because it's more OO, obviously.

Note: this answer is ironic, as noted by the OP struct achieves all the functionality of class, so there is little added value and it seems more an arbitrary design choice. Quite strange from a language which advocates limiting the number of reserved words.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
2

While struct can behave like a class, it doesn't emphasize the object-orientedness of the langauge. So I think class was added to the language to emphasize the object-orientedness of C++. It also emphasizes data-encapsulation, as everything in a class is private by default, whereas struct doesn't emphasize data-encapsulation, as everything in a struct is public by default.

Data-encapsulation is one of the principles of object-oriented programming, so I think, making default-access private is very important. It gives a sense of class-design from the very beginning. Just pretend for a moment that there is no keyword class in C++, then imagine how many people would have created topics like "why everything in C++ is public by default, as everything in struct is public by default? Is it a characteristic of a language which supports object-oriented programming". To me, it feels that C++ would be lacking the sense of class-design.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • A structure can acheive everything that a class can in C++, the only difference being a syntactic default access specifier, It is hard to believe that C++ standards committee came up with a new language construct just for **emphasizing** OO unless they had other(bigger) things in mind to go along for a *class*, @PlasmaHH answr brings out this fact rather convincingly. – Alok Save Aug 23 '11 at 09:33
  • @Als: Just pretend for a moment that there is no keyword class in C++, then imagine how many people would have created topics like *"why everything in C++ is public by default, as everything in struct is public by default? Is it a characteristic of a language which supports object-oriented programming"*. To me, it feels that C++ would be lacking the sense of class-design. – Nawaz Aug 23 '11 at 09:37
2

You could argue you get more readable code if you adopt a convention where you use class for encapsulated OO class types and struct for non-encpasulated structural aggregate types with public data members: this is the usage typically mandated by coding standards.

AndrzejJ
  • 720
  • 5
  • 11
0

It is mostly a more intuitive syntax. It would seem strange to have no class in a class based OO language.

Juraj Blaho
  • 13,301
  • 7
  • 50
  • 96
0

I think the difference is semantic, and a bit syntactic. class emphasizes that this is a class and not anything else. So by the name it tells its usage. Where as with struct we can structure some thing, like a new datatype, by grouping other basic types. but grouping datatypes and making a new one with a class is possible, but the name class in this case will be misleading.

phoxis
  • 60,131
  • 14
  • 81
  • 117
0

You are correct. Ideally we don't need class. It has been introduced for historical reasons, where the inventor of C++ wanted to distinguish between struct as plain old data type (C style) constructs and class as the interface which can have functions and constructors as well. (Though, struct still held all the privileges as class).

If you remove class keyword from the C++ language, it can still work flawlessly (not the old code). Because it's normal usage is achievable by struct and its template related meaning is achievable by typename.

iammilind
  • 68,093
  • 33
  • 169
  • 336
0

To me the difference between class and struct is documentation.

So technically we do not need them (like many features of the language) but they allow us to impart extra information to the user of our code by the way we use them (as long as you don't abuse them and follow the standard conventions).

Underneath they are the same thing. But I find that in different context I use them to identify different things. Thus I am able to convey extra information to the user of my types by the way they are declared.

How I use them:

  • class usually infer some form of object that has actions associated with it.
    • i.e. your standard OO concept of objects.
  • struct (usually) infers some form of property bag
    • It may have methods but not necessarily action orientated.
  • functors are usually implemented as struct
    • Especially if they have no internal state.
Martin York
  • 257,169
  • 86
  • 333
  • 562