Solved

struct vs class

Posted on 1998-09-27
5
303 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
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

685 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