+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++?

c++

5/17/2018 4:07:44 AM

VIJAY MOURYA

22 Answers

New Answer

+13

The only difference between a class and a struct in C++ is that structs have default public members and bases. Classes meanwhile, have default private members and bases. Both classes and structs can have a mixture of public and private members, can use inheritance, and can have member functions.

+11

Here's a more practical example Immortal🌼 and Martin Taylor https://code.sololearn.com/cE4Rp3vTBL4p/?ref=app

+10

Martin Taylor Immortal🌼 A simple example: https://stackoverflow.com/questions/13125944/function-for-c-struct

+10

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!

+9

Ipang Exactly, the bigger point is how C++ structs differ greatly from their C counterparts. As for inheritance, it is public by default. So if I had 3 structs: A, B, and C, I could write: struct A { }; struct B : A { }; //B inherits A struct C : B { }; //C inherits B Hope that helps

+7

Objects are created by using 'Class'. Each class has a name, and describes attributes and behavior.

+7

Timon Paßlick I think people are confused between the difference between C structs and C++ structs. It's a common misconception to make that they work exactly the same. Basically, if you started with C or another non-object oriented language, your view of a struct is much different than an object oriented language line C++, etc.

+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

Martin Taylor I am also unaware from that fact. It seems you are right at some extant. I only know that primary data types, arrays, and other struct can be defined under the structure while a function can't be defined as a member.

+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

I would have thought the biggest difference between a class and a struct is that classes can contain methods (functionality) as well as data whereas a struct contains only data.

+4

Nikhil Leeuwenhoek They can have member functions, read above.

+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.

+3

~ swim ~ It's recommended to use typename anyways.

+3

Timon Paßlick I know but the post is about difference. And till C++14 you cannot use typename in template template parameter

+2

Well I'll be b@##£&%! I've used pointers to functions in structs before but I've never seen a method in a struct done like that until now.

+2

VIJAY MOURYA One more difference A struct keyword cannot be used in template parameter declaration e.g. invalid declaration template<struct T> void Print(T a); valid declaration template<class T> void Print(T a); Though this difference is more of a keyword usage difference :)

+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.