Solved

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

Posted on 2014-01-21
3
331 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mixing C++ & C# in Vis Studio 2013 7 178
C++ to C# code conversion issue 4 109
Safe conversion? 4 69
No module found pypyodbc, 3 19
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 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.

831 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