Solved

struct vs class

Posted on 1998-09-27
5
304 Views
Last Modified: 2010-04-01
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
Comment
Question by:jotjot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 1

Accepted Solution

by:
Booth882 earned 200 total points
ID: 1173872
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
 
LVL 1

Expert Comment

by:Booth882
ID: 1173873
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
 

Author Comment

by:jotjot
ID: 1173874
Ok, then why create this language element called a class?
What's the historical reason behind that?  

STAU
0
 

Author Comment

by:jotjot
ID: 1173875
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
 
LVL 22

Expert Comment

by:nietod
ID: 1173876
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel/Word Add-in in what language? 5 162
FMX StringGrid1->Canvas->FillRect Problem 3 220
Arduino EDI - Programming language 3 126
object oriented programming comparison 5 93
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question