Solved

struct vs class

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now