Seeing we can do it with structs, why do we need classes? See the code and quote from Link

The C++ class is an extension of the C language structure. Because the only difference between a structure and a class is that structure members have public access by default and class members have private access by default, you can use the keywords class or struct to define equivalent classes.

class X {

  // private by default
  int a;

public:

  // public member function
  int f() { return a = 5; };
};

struct Y {

  // public by default
  int f() { return a = 5; };

private:

  // private data member
  int a;
};

Hope this might clear this up for you.

The notion of a struct in C++ goes far beyond the original consept of a struct in C. It incorporates the object-oriented notion of a class. This idea of classes, from which you can create your own data types and use them just like the native data types, is fundamental to C++, and the new keyword class was introduced into the language to describe this concept. The keywords struct and class are almost identical in C++ except for the access control of the members as you have already mentioned. The keyword struct is maintained the keep backward compatibilty with C., but everything you can do with a struct you can do with a class.

I tend to use structures for the more simple data types as in the original concept of C, and classes for everything else.

Milton:)

Edited 6 Years Ago by Milton Neal: add more

As Milton mentioned, both struct and class exist so that C++ is backwards-compatible with C. Even though they function nearly identically, the "class" however, is an extension of the basic struct that allows C++ to continue to evolve and improve on the concept without breaking the backwards compatibility.

You might wonder why C++ contains the two virtually equivalent keywords struct and class.
...
although struct and class are virtually equivalent today, providing two different keywords allows the definition of a class to evolve. In order for C++ to remain compatible with C, the definition of struct must always be tied to its C definition.
...

What has been said so far is all true. As Milton hinted to, what I would say is the main difference between struct and class is in the traditional use of them (or programmer conventions). Most programmers and most code that I have ever seen make use of struct almost exclusively for so-called Plain-Old-Data types (POD-types) where the main purpose of the type is to hold a bundle of data members (but not necessarily the only purpose). For example, a simple 3D vector type like this would usually be implemented with struct:

struct MyVector3D {
  double x, y, z; //just holds the three coordinates, no functionality implemented.
};

On the other hand, a type that is mostly a functional element (e.g. fairly complex member functions with some internal, encapsulated data members) and possibly in an inheritance tree (with virtual or pure virtual functions) would generally be declared as a class, by convention.

Of course, the language itself does not force you to obey those conventions and you are free to break them, but that will mostly confuse other programmers (and yourself too!). It is also a good way to start designing a struct or class by thinking about whether it is more of a data container or more of a functional element.

Of course, sometimes there are types that are in-between where it is not clear-cut (e.g. a vector type with several methods for vector arithmetic or magnitude calculation or whatever, and possibly derived into a position vector type). Then, the convention no-longer applies very much and then there is essentially no real difference. A simple rule of thumb, when in doubt, if it makes sense to have all data members public (as opposed to a trivial set/get for each one), then usually a struct would make more sense.

Also consider that there are some programmers out there who consider having types (structs or classes) that are not clearly a struct (close to C-style struct) or clearly a class (complex, possibly polymorphic class), but in-between, is a sign of bad design and might criticize you for it (I wouldn't, I'm a bit more gentle than that).

Finally, it is true that the only difference between struct and class is the default access right, but again, several programmers out there don't recommend using default access rights anywhere, because it is somewhat unclear semantics (although it is clear for any programmer who knows the default access rights, it is even clearer if all access rights are explicitly stated). The exception is the kind of extremely simple types like the MyVector3D struct shown above. So, at the end, programmers use struct or class mainly as a way to quickly convey the idea that the type is more a data bundle or more a functional element.

thanks guys!
I think C++ was supposed to preserve C-concept of structs and leave every other complexity to class. I was shocked to know that structs can do everything class can do and I'm yet to see any program treating structs as they do to classes. I think the decision added only redundancy and confusion than help anything. I think default access is too small reason to *invent* such complexity and loose original meaning of struct.

Is there a philosophy I'm missing here, I mean I don't get their reason to do that. I see disadvantage(complexity, confusion...) added to struct keyword and don't see any good reason. I think I need to hear from you on that :)

The reason that C++ has both the struct and class keywords is that Bjarne Stroustrup believes that important concepts should have names.

Consider: If C++ were to have only one of struct or class, it would have to keep struct and ditch class, because otherwise even the sipmlest C programs would not work as C++ programs. It would still be possible to write classes as structs with appropriate permissions, but then every programmer would be advised to use private labels all the time.

In that case, instead of asking why C++ has both struct and class, people would be asking "People are urged to use private labels all the time; why doesn't C++ have some way of making that the default?"

The reason that C++ has both the struct and class keywords is that Bjarne Stroustrup believes that important concepts should have names.

Consider: If C++ were to have only one of struct or class, it would have to keep struct and ditch class, because otherwise even the sipmlest C programs would not work as C++ programs. It would still be possible to write classes as structs with appropriate permissions, but then every programmer would be advised to use private labels all the time.

In that case, instead of asking why C++ has both struct and class, people would be asking "People are urged to use private labels all the time; why doesn't C++ have some way of making that the default?"

Thanks arkoenig,
My question is why did they complicate simple C structs while class could do all, and very well?

I'm not a compiler writer, but I suspect it's a matter of convenience and/or simplification. Since they are essentially the same, they probably produce similar object code. Which potentially allows them to re-use the same static libraries at compile time, just change the settings.

Edited 6 Years Ago by Fbody: n/a

I know I'm a bit offtop here, but I would like to say that not only the default access of members is the difference between a class and a struct,but also the access of derivation is public for structs by default and private for classes. Also the word class can be used in template parameter list, whereas struct can't. :)

struct X {};
struct Y : X {}; //equivalent to struct Y : public X {};
class A {};
class B : A {}; //equivalent to class B : private A {};
template <class T> class H {}; //equivalent to template <typename T> class H {};
template <struct T> class G {}; // ill-formed, syntax error
This article has been dead for over six months. Start a new discussion instead.