How do you know when to use a class or struct in C++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How do you know when to use a class or struct in C++?

29th Jul 2018, 4:40 AM
Tevin Charles
Tevin Charles - avatar
2 Answers
+ 3
I don't see much difference except default access specifier.... I personally use structure when I just want to store data and reuse it with No requiremnt for methods (even though struct can also have methods).... With this, we are not doing encapsulation properly, but in this case, sole purpose of data storage is fulfilled... consider below case : You are going to have multiple points with x,y and z coordinates.. for this, you can either use multimap , three vectors of double type each one for x, y and z... I prefer to declare structure with three members and pass this structure as vector to store all details for all points
29th Jul 2018, 5:20 AM
Ketan Lalcheta
Ketan Lalcheta - avatar