Solved

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

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

705 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