• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 310
  • Last Modified:

struct vs class

Is a class just like a struct, with a constructor and destructor added,
plus the ability to hide members if you want?
It seems like you can do everything with a struct that you can do with
a class, including defining functions.  
If a class is just a struct with more features, why make up a whole
new element in the language?

--- Struggling to Achieve Understanding
0
jotjot
Asked:
jotjot
  • 2
  • 2
1 Solution
 
Booth882Commented:
there is a difference in the C language in that struct cannot declare functions.  in C++ however they are identical except that in class declarations everything is private unless otherwise indicated, and in struct declarations everything is public unless otherwise indicated.  since you can still make something any access you want in either, this difference is trivial.
0
 
Booth882Commented:
the reason they have both in C++ is because it evolved from C, and even though certain things are archaic they have hung around just through habit.  some programmers use structs for special cases, like when they arent declaring any functions and just want to have a data holding object.  this isnt necessary though, really they are just the same
0
 
jotjotAuthor Commented:
Ok, then why create this language element called a class?
What's the historical reason behind that?  

STAU
0
 
jotjotAuthor Commented:
Sorry, I missed the comment that answered the rest of my question.  Well, you get all the points anyway!

Thanks, now I can sleep....

STAU
0
 
nietodCommented:
Read "The Design and Evolution of C++" by BjarneStroustrup.  It explains why there are both classes and structures and many other odd facts about C++.  It provides great insight into the current state of the language.
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now