views:

306

answers:

3

What is real difference between Class and Structure when you are dealing with Object Oriented Programming. This question is asked many times during my interviews for SE.

Some people says that there is only one difference: Structure members are public by default and Class members are private by default.

Some says there are many differences.

After reading many articles and forums, I have the following differences:

Classes DEFAULT to having private members. Structures DEFAULT to having public members.

Structures are values type. Classes are reference type.

Structure stores in memory via stack. Classes stored in memory via heap.

Structure doesn’t support inheritance. Classes support inheritance.

Constructor works in different way.

‘new’ operator works in different way.

Allocating memory for structure is very fast because this takes place inline or on the stack.

What are your opinion on my above list or you have a different one. Thanks

+1  A: 

This is entirely language dependent, so there can be no single correct answer. If you are interested in a specific language, please specify it in your question.

anon
I am interested in PHP/Java
NAVEED
+1  A: 

From an OO perspective, there are no difference. They are both types that have a public API with methods (and properties if your language supports them).

From a technical standpoint, there can be many differences, but that depends on the language and/or platform.

When it comes to OO design, I simply choose to ignore that such a thing as a struct exists at all as it gives me no additional capabilities or features. As we dive deeper into the implementation, a class may turn out to be better implemented as a struct, but it's a pure implementation detail.

Mark Seemann
+2  A: 

This is pretty language-specific. You seem to be mixing a fair share of both C++ and C#, both of which are very different languages (despite superficial similarities in syntax).

In C++ structs indeed default to public member visibility while class defaults to private. In C# struct is used to declare value types which are passed by value (note that the stack allocation is an implementation detail, not a contract).

Generally both languages seem to have the same idea of what struct and class should represent: struct is for simple data structures which do little more than holding data, while classes have state and methods to manipulate it. They are used to build objects in some concrete or abstract sense while data structures are just that: data in a structured form; they don't need to do much with that data or even know what data that is. Essentially they're dumb and don't care.

But that's just how the language designers thought they should be used. People are good at mis-using things so not every struct you see may be a simple, dumb data structure and not every class you see may be a full-blown class with lots of methods and whatnot. It's merely a convention and if people follow it others can look at the code and see "Oh, nice, that's a struct so I don't expect much logic here and move on to more interesting things." It might work ... in theory.

ETA: Since you mentioned in a comment that you are particularly interested in PHP or Java: Both languages do not have any distinction at the syntax or language level of class or struct which is why your question strikes me as a little odd. In both Java and PHP you model things as classes, regardless of whether they are just data structures without logic or actual classes with everything there is.

Joey
When I was asked about this difference by anyone during interview, they do not depend on any language. I thought it is a common question.
NAVEED
NAVEED: The point is that many languages have differing facilities and capabilities. If the question was as general as the difference between a class and a data structure, then it's actually not hard to answer. But what you listed in your question were a colorful patchwork of C++ and C# features regarding the `class` and `struct` keywords, so it was a little hard to understand what *exactly* your question was about. And as for different languages, the answer depends very much on what those languages are. As pointed out, Java for example only has classes for everything, data structure or not.
Joey
thanks for your response. At start, my question was general and my points in my questions were also general(which is my opinion, may be wrong). When users asked about a particular language then I showed interest in PHP and JAVA. My points may be wrong as general,therefore I asked for your thoughts in the end of my question. This discussion is really increasing my knowledge. thanks
NAVEED