What is the difference between Structure and Class? Is there are any particular situations when I should use what?

Once I implemented Binary search tree using structure. Now, I am trying to implement Red black tree using Class? But many discourages using class in this kind of situation, they say struct suits best here.Can anyone explain why?

struct has default visibility of its members as public.
class has default visibility of its members as private.

That's the whole difference.

For historical reasons, convention is to use structs for occasions when you are going to fill it only with plain old data, or POD - that is, no functions and no types that are themselves classes with member functions. Some would have it no structs or classes or new types at all, just the primitives like int and double and so forth.

It's just a convention.

@moschops hits it right on the head, it also comes down to whether or not you would want to keep data members private rather then having both data and methods to work on the data all public to the program.

if you feel like you want more "containment" use a class and keep the data private and the methods public. this will result in more work (in a sense) but essentially I think of structs as the "public part" of a class.

@moschops hits it right on the head, it also comes down to whether or not you would want to keep data members private rather then having both data and methods to work on the data all public to the program.

if you feel like you want more "containment" use a class and keep the data private and the methods public. this will result in more work (in a sense) but essentially I think of structs as the "public part" of a class.

You can make members of structs protected or private as well, just like you can make members of classes public; you just need to use the appropriate keyword. They simply default to public in a struct and private in a class. Beyond that, their use-case differences are simply a matter of personal-preference, tradition, and/or convention.

>> For historical reasons, convention is to use structs for occasions when you are going to fill it only with plain old data, or POD - that is, no functions and no types that are themselves classes with member functions.


I follow this guidance. Since "structs" are available in C and store "plain old data", I put anything "C-like" in a "struct" and anything "un-C-like" in a class.

Someone correct me if I am wrong but if you implement a C-style struct(POD struct) you won't have a constructor. So you will get some increase in efficiency if you don't need a constructor.

Someone correct me if I am wrong but if you implement a C-style struct(POD struct) you won't have a constructor.

I believe that is wrong. In C++, your struct is treated just like a class, and it gets a constructor and a destructor.

I believe that is wrong.

Actually, it's correct. A POD type (either using the class or struct keyword) is one of the situations where the compiler doesn't need to synthesize a constructor, and compilers will generally not do so for performance reasons. Though it's a bit of a leap to imply that you'll see noticeable improvements in your program from this optimization alone.

Fair enough. I did not know that; is that something specified by the standard, or is it just a common compiler optimisation?

It's in the standard, though there's no requirement that a compiler not synthesize a constructor when it isn't necessary.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.