7

Using a struct we can achieve all the functionality of a class: constructors (that can be modified/overloaded), destructors (that can be modified/overloaded), operator overloading, instance methods, static methods, public/private/protected fields/methods.

Why do we need class then?

Note: I don't want the answer saying that in struct, fields/methods are public by default.

OmarOthman
  • 1,718
  • 2
  • 19
  • 36
someone
  • 1,638
  • 3
  • 21
  • 36
  • I'd say it's for 'historical' reason of compatibility with [tag:c]. – πάντα ῥεῖ Jul 29 '13 at 08:44
  • Can do data encapsulation , data hiding with structures – Sanyam Goel Jul 29 '13 at 08:44
  • Getter ( accessors), setters (mutators) have some meaning in OOPs that we can achieve in classes and not in structures – Sanyam Goel Jul 29 '13 at 08:47
  • `class` has a meaning in OOP. `struct` exists in C. So it makes sense to have "`class`" in C++, and at some stage it must have seemed like a good idea to keep `struct` but make it essentially the same as `class`. The fact is that this is all irrelevant. – juanchopanza Jul 29 '13 at 08:48
  • 1
    @SanyamGoel I'd be interested in seeing those "meaning" in OOPs that we can achieve in classes and not in structs. – JBL Jul 29 '13 at 08:49
  • @juanchopanza, really so you mean to say u can do object oriented programming in c with structures than c ++ in classes – Sanyam Goel Jul 29 '13 at 08:50
  • `class` is a nicer name. That said, I personally think it's a great shame and a wasted opportunity that `struct` and `class` are basically the same. In hindsight, it would have been nice if `class` required that copy-constructor, copy-assignment operator, and destructors be explicit; the implicitness of them is required to follow C `struct` semantics. – jamesdlin Jul 29 '13 at 08:50
  • @SanyamGoel Concerning your first two comments: Not true in C++. Anything you can do with `class` you can do with `struct` (with the exception of type template parameters, which have to be `typename` or `class`). – juanchopanza Jul 29 '13 at 08:51
  • 3
    @SanyamGoel: You can do OOP with structs in C, but that is not the point. The question is about C++, not C. And a `struct` in C++ is not the same as a `struct` in C. But a `struct` in C++ is almost exactly the same thing as a `class` in C++ (the differences are completely trivial). – Benjamin Lindley Jul 29 '13 at 08:51
  • @SanyamGoel In **C++** they are interchangeable. – juanchopanza Jul 29 '13 at 08:52
  • @juanchopanza yes I agree initially c was named c with clases :) – Sanyam Goel Jul 29 '13 at 08:54
  • @jamesdlin I'm not too sure about that. The implicitly generated copy constructor, copy assignment operator and destructors should do the job for most well designed classes. It would be a burden to have to provide them yourself, although C++11 `default` helps in this sense. – juanchopanza Jul 29 '13 at 08:55
  • @juanchopanza I do agree that providing full implementations of the Big Three all the time would be a burden, but I think overall it'd be better if they weren't implicit and an opt-in mechanism (`default`) were introduced at the beginning. Besides, people who want the implicit implementations of the Big Three could use `struct` in my fantasy world. – jamesdlin Jul 29 '13 at 09:00
  • 1
    The reason to have almost identical *class* and *struct* is described in ch. 3.5.1 "The object layout model" of *The design and evolution of C++* by B. Stroustrup. Quote - "...May be we would have lived with two set of rules, but a single concept provides a smoother integration of features and simpler implementations." – SChepurin Jul 29 '13 at 09:36

5 Answers5

10

You don't need classes, the language just gives you another option to choose from. Technically, you're right, you can achieve anything a class can do with a struct.

Besides the default access level, there's also the meaning most programmers associate with the two - struct generally means a light-weight, typically POD, data-type with little to no functionality. A class is usually associated with something bigger.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
3

As Tal Pressman answered at When should you use a class vs a struct in C++?:

From the C++ FAQ:

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.

http://www.parashift.com/c++-faq-lite/classes-and-objects.html#faq-7.8

I think one addition to this reason could be that C already had structs. When Bjarne Stroustrup designed C++, he wanted to add classes with all the functionalities you listed in your original question, features which were not present in C. When implementing those features, he probably realised it didn't make sense to make two separate implementations for struct and class (except the public/private default visibility).

TL/DR: in C++ structs/classes describe the intent of the programmer to have POD or more complex abstractions, and the introduction of the class keyword is probably here for historical reasons (add an additional keyword in order to create featurefull classes, then backport those features into the struct keyword because it's more pragmatic to implement).

Community
  • 1
  • 1
Silex
  • 1,707
  • 17
  • 24
2

class is simply the commonly accepted name in OO for a type used for instantiating objects. When introducing the OO paradigm in C++, it was deemed less surprising to use class instead of struct.

struct was kept to maximize backwards compatibility with C.

Today's usage of the two is in line with this : struct is most commonly used for C-style POD types, while class is used for the OO concept of classes.

Sander De Dycker
  • 16,053
  • 1
  • 35
  • 40
1

There are no such difference between C++ struct and C++ class, you can perform almost all the functions with struct as you can do with class, but struct is a C keyword which gradually got modified/evolved in C++ and named as class. A we are in C++, it is better to use class rather than struct.

Take an example, if you have done some coding in C++ and some person who works in Java came after 2 months to review your code, which one will he find comfortable to understand a code with "struct" or a code with "class"?

Saby
  • 718
  • 9
  • 29
  • 2
    What can you do with a `class` that you can't do with a `struct`? – jamesdlin Jul 29 '13 at 08:47
  • 1
    Everything that can be done with a C++ struct can be done with C++ class, but I think personally that class looks better than struct in C++. Otherwise Stroutstrup wouldnt have introduced the keyword at all.. What do you think? – Saby Jul 29 '13 at 08:50
  • 2
    But you said "you can perform almost all the functions...". How is it "almost"? – jamesdlin Jul 29 '13 at 08:52
  • Okay, I personally have tried doing almost everything with struct like operator inheritance, overloading, overriding almost all. As C++ is huge, I am not sure if there may be something which creates a streamline between the two. But when I am a C++ developer and I am thinking with OOPs shoes on, I will start writing the code with class keyword rather than struct , and that most developers will do, I can guarnatee that. – Saby Jul 29 '13 at 08:55
  • @log0: I have mentioned in my answer that "There are no such difference between C++ struct and C++ class", what does that mean? For your answer also, pls refer to my above comment. It is more of a good practice for using Class I think because in almost most of the languages it is mentioned as "class". Take an example, if you have done some coding in C++ and some person who works in Java came after 2 months to review your code, which one will he find comfortable to undertsand a code with "struct" or a code with "class"? – Saby Jul 29 '13 at 09:00
  • @jamesdlin: In template void func() {} try replacing the class keyword with struct, it will give an error. So this is a convention used in C++ templates where class can be used and struct cannot, because concepts of templates are not directly an evolution from C. Here Stroutstoup could have reused the already existing struct keyword here also, but he has not done the same, he has given more preference to Class(he inherited the keyword from Simula 67) than struct as it gives more feel for object oriented architecture. – Saby Jul 29 '13 at 09:14
1

To make a long story short, class really wasn't needed at all. It changes the defaults to ones that are arguably safer and more applicable to OO programming, but you could use a struct (as defined by C++) for any place that you currently use a class (if you wanted to get cute and meta about it, you could call struct a base class of class that satisfies the LSP).

At the same time, misunderstanding of struct in C++ is rampant, and class fits the intended concept enough better that it's often much easier to explain. New users often seem to find it at least marginally more understandable -- a reasonable goal in itself.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111