+ 6
What is the difference between a class and a structure in C++?
What is the difference between a class and a structure in C++?
22 Answers
+ 12
no difference at all!
though they have different defaults access policy, in C++ struct /class is always class.
the real difference is that struct in C++ are something like a class who wannabe a c struct, just for c programmers used to use struct as a data structure..
classes are generally intended as OOP types but those differences are only inside our minds.
as example. in C++
char[] string="hello" //c style
string text = "👋 " //oop style
they both works!
+ 7
Objects are created by using 'Class'.
Each class has a name, and describes attributes and behavior.
+ 6
Ace by "can use inheritance", does that mean structures are extensible like classes are?
+ 5
Martin Taylor You can add methods to a struct, try it out.
Ace is right, that's the only difference.
+ 5
Ace that was helpful and enlightening, I had no idea it was possible. Big Thanks : )
+ 5
This is interesting. I thought C++ structs were purely for backwards compatibility with C. Well I'll be.
+ 4
Nikhil Leeuwenhoek They can have member functions, read above.
+ 4
~ swim ~ It's recommended to use typename anyways.
+ 3
Meklit Getachew They do. It's just public instead of private by default. If you don't believe me, read through the standard or see that it compiles.
+ 1
Structs have default public members by default while classes have default private members.
Structs don't have any member functions or inside of 'em but classes do.
+ 1
structures are public , while classes are private.
0
classes include public and private members but structures donot include those terms