Solved

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

Posted on 2014-01-21
3
329 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
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 250 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 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 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.

863 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

19 Experts available now in Live!

Get 1:1 Help Now