Solved

struct vs class

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

690 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