What is the difference between a class and a structure in C++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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++?

17th May 2018, 4:07 AM
VIJAY MOURYA
VIJAY MOURYA - avatar
12 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!
17th May 2018, 9:18 PM
AZTECCO
AZTECCO - avatar
+ 7
Objects are created by using 'Class'. Each class has a name, and describes attributes and behavior.
18th May 2018, 1:02 PM
Prabhat
Prabhat - avatar
+ 6
Ace by "can use inheritance", does that mean structures are extensible like classes are?
17th May 2018, 7:29 PM
Ipang
+ 5
Martin Taylor You can add methods to a struct, try it out. Ace is right, that's the only difference.
17th May 2018, 8:29 AM
Timon Paßlick
+ 5
Ace that was helpful and enlightening, I had no idea it was possible. Big Thanks : )
17th May 2018, 7:38 PM
Ipang
+ 5
This is interesting. I thought C++ structs were purely for backwards compatibility with C. Well I'll be.
18th May 2018, 9:49 AM
non
+ 4
Nikhil Leeuwenhoek They can have member functions, read above.
18th May 2018, 4:13 PM
Timon Paßlick
+ 4
~ swim ~ It's recommended to use typename anyways.
13th Jun 2018, 5:04 AM
Timon Paßlick
+ 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.
17th May 2018, 8:56 PM
Timon Paßlick
+ 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.
18th May 2018, 4:09 PM
Nikhil Barik
Nikhil Barik - avatar
+ 1
structures are public , while classes are private.
18th May 2018, 4:48 PM
Bhavesh Sangtani
Bhavesh Sangtani - avatar
0
classes include public and private members but structures donot include those terms
17th May 2018, 7:22 PM
Meklit Getachew
Meklit Getachew - avatar