?
Solved

Class and struct in c++? when to use what?

Posted on 2014-01-21
3
Medium Priority
?
351 Views
Last Modified: 2014-01-21
I come from C then to C++.  I have not touched C++ for a long time. Now when I try to review and get back to C++.  It seems to me that struct with method are used quite frequent in c++. I remember when I first use C++, we only create a struct for storing complex data. But now it is different. Can someone kindly explain, when you would use struct but not class.  I am trying to oriented myself back to C++ again.  Thanks
0
Comment
Question by:tommym121
[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
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 39798182
It's actually quite straightfoward:

- use a struct when you need to group data

- use a class when you want to encapsulate data

'Grouping' means that you need to tie data together that belons together for logical or functional reasons. 'Encapsulating' means that you want the data to not be visible to everyone and have methods to access, set or manipulate it.

An explanatory counterexample would be to use a class with all members being 'public' - that defeats the whole purpose, you could as well have used a struct.
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 1000 total points
ID: 39798229
The only difference between a class and a struct is the default access specification. On classes members are always private unless you use an access specifier to change it, a structs is always public.

struct foo{ }; is the same as class foo{ public: };

struct foo{ private: }; is the same as class foo{  };

From a binary point of view they are both represented in memory identically. Which you choose depends on what you're doing. If your object only has public access requirements it is more convenient to use a struct, otherwise use a class. This is probably just another way of saying what jkr has said, but I just wanted to be clear that from the compilers point of view it makes no difference and the choice is yours.
0
 

Author Closing Comment

by:tommym121
ID: 39798308
Thanks for a straight forward explanation. Thanks.
0

Featured Post

Industry Leaders: 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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

719 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