What are the differences between a class and a structure? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

What are the differences between a class and a structure?

I know that a structure has only variables and not methods. But a class can have variables and methods. This is the greatest difference between a structure and a class. I am looking forward for other differences.

16th Aug 2018, 2:20 PM
Dimitris N. Kapoulas
Dimitris N. Kapoulas - avatar
4 Answers
+ 3
in c++ a struct is simply a class with members public by default and you can define constructors and other members functions for it
16th Aug 2018, 5:02 PM
MO ELomari
+ 6
The members and base classes of a struct are public by default, while in class, they default to private. Note: you should make your base classes explicitly public, private, or protected, rather than relying on the defaults. Struct and class are otherwise functionally equivalent.
10th Sep 2018, 8:24 PM
Mantafounis Panagiotis
Mantafounis Panagiotis - avatar
+ 5
thanks
21st Aug 2018, 11:38 AM
Dimitris N. Kapoulas
Dimitris N. Kapoulas - avatar
+ 3
The members and base classes of a struct are public by default, while in class, they default to private. Struct and class are otherwise functionally equivalent. They are however used in different places due to semantics. a struct is more like a data structure that is used to represent data. class, on the other hand, is more of a functionality inclined construct. It mimics the way things are and work. In c there is no option for declaring member function in structure. But c++ have ... Thanks to encapsulation and Access specifier
10th Jul 2019, 6:50 PM
Soumyadip Saha
Soumyadip Saha - avatar