-4

Please tell me difference and also why??

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
Prasad007
  • 103
  • 3
  • 7
  • 5
    It's not. A `struct` is a `class` where everything is public by default, that's the only difference. Or a `class` is a `struct` where everything is private by default, either way. – Seva Alekseyev Aug 28 '12 at 15:41
  • 1
    @down-voters - well, the statement is completely wrong, but this is not Prasad007's fault. I don't think it deserves that many down-votes. – Kiril Kirov Aug 28 '12 at 15:44
  • See also: http://stackoverflow.com/questions/92859/what-are-the-differences-between-struct-and-class-in-c/92951#92951 – Robᵩ Aug 28 '12 at 15:44
  • @KirilKirov But even the most basic research could have revealed the answer. I didn't downvote, but I do think it's justified. –  Aug 28 '12 at 15:45
  • @delnan - you may be right. I wouldn't down-vote anyway, but you have a point, really :) – Kiril Kirov Aug 28 '12 at 15:46
  • Friends I heard above line from my professor and i didn't get that that's y i asked you...But still m confused!! So Plz tell me on what basis i can say struct is lightweight and class is heavyweight...I'm Student so plz help me..... – Prasad007 Aug 28 '12 at 17:21
  • It sounds like you should ask your professor to clarify their statement. The distinction of "lightweight" or "heavyweight" isn't part of the language or common usage among programmers. – Blastfurnace Aug 28 '12 at 19:05
  • Guys why u people r giving me down vote, i was confused that is why i asked you..Now I'm not able to post another Question...Thanks for good answers... – Prasad007 Aug 29 '12 at 17:36

3 Answers3

6

Why structure is lightweight and Class is Heavyweight in C++

Who said that? That is entirely an incorrect statement.

The difference between struct and class is that the default accessibility for struct is public, whereas for class it is private. Otherwise, struct is same as class.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
5

Because of how programmers' brains work.

struct comes from C, where it originally had no methods, access levels and other class-specific stuff that C++ brought along. There's also the default public access level it has in C++ that makes it more of a container of some members, rather than a full-on OOP object.

class on the other hand was introduced by C++, and provides all of the above (and private access level by default).

Of course, there's no difference (other than default access level) in C++ between the two, so it's mostly a meta way of saying light-weight and heavy-weight. When you say class, you inherently think of methods, private members, polymorphism and inheritance. When you say struct you think that it has 2 ints as members & that's it :)

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • *a meta way of saying light-weight* Nice :-) – cnicutar Aug 28 '12 at 15:47
  • I've been trying to figure out whether this answer is saying anything, but I can't help but think it's sheer personal opining and emoting. In particular, "rather than full-on OOP object" just sounds plain misleading... – Kerrek SB Aug 28 '12 at 17:55
  • @KerrekSB it's largely subjective, yes, but if you look at large codebases, you'll see it's true. Otherwise, why would there be snippets like `class X{public: ...};` instead of `struct X{...};`? – Luchian Grigore Aug 28 '12 at 17:56
  • @LuchianGrigore: Same reason that there are lots of `getFoo()` and `setFoo()` functions and huge inheritance hierarchies -- it takes a long time for a language to develop its idioms, and programmers can be very resistant to changing habits! – Kerrek SB Aug 28 '12 at 18:46
0

I am not sure about your question. Maybe you are asking about "virtual" things. There is a possibility to have virtual table in the Classes/structures in C++. You cant memset them, it will cause a lot of problems. Virtual Table cost you extra memory so maybe that is why they are "heave"...

http://www.programmerinterview.com/index.php/c-cplusplus/how-vtables-work/

Also remember that Struct and Class in C++ is the same expect that struct has public fields and functions by default.

CyberGuy
  • 2,783
  • 1
  • 21
  • 31