Solved

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

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

757 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

21 Experts available now in Live!

Get 1:1 Help Now