+ 32

I can not understand difference between Class and Struct.

9th Jan 2016, 12:39 PM
dani rzv
dani rzv - avatar
42 odpowiedzi
+ 34
1. class has the members as private by default but structure has public.. 2. class is a refrence type datatype while structure is a value type datatype. 3. class can be inherite but structure can not be. 4.class can support virtual methods but structure can not support virtual methods. 5.class provide the security of private data but structure can not provide. 6. class provide the feature of hiding the data but structure doesn't provide..
12th Nov 2016, 5:49 PM
Vinay
Vinay - avatar
+ 34
Most of these answers are wrong. There's only one difference between classes and structs and that's default access specifiers. struct CStruct { int Var; //public }; class CClass { int Var; //private }; Both can be passed by value and by reference. Both can contain functions and access specifiers. Both can inherit and be inherited from. As an example, in the Direct3D 9 helper library, there's a struct for 3D vectors that just has three floats in it, but if it detects that you're compiling for C++ instead of C, it'll add several functions to the struct for dot/cross products, operator overloading, etc. Another example can be found when dealing with IO Completion Ports in Windows, often used with networking. In it, there's a struct called OVERLAPPED that contains some synching variables Windows uses to keep track of when the call completes, but you can overload that class and bundle more information in (such as buffer address or sender/receiver ID) making the whole system significantly less complicated.
4th Dec 2016, 8:56 AM
Nemo
+ 14
class has default private members, structure has default public members. members of structure in memory are one beside another for faster access to them.
9th Jan 2016, 10:17 PM
Paweł Szydełko
Paweł Szydełko - avatar
+ 12
OMG!!! I can't believe that most of you don't know the difference of class and struct :0!!! The only difference is that the default access privilege of members for struct is public instead class is private. Everything else is exactly the same. Struct CAN be inherited, it CAN have virtual function!!!
2nd Dec 2016, 4:17 PM
Elio
Elio - avatar
+ 8
@Vinay your answer is correct for c# language. in c++ the correct behaviour is explained by user @nemo See the page that shows the differences like this between c++ and c#. https://msdn.microsoft.com/en-us/library/yyaad03b(v=vs.90).aspx Another interesting issue in C++ is slicing, it doesn't exist in c# but exists in c++. see here http://stackoverflow.com/questions/274626/what-is-object-slicing
31st Mar 2017, 6:22 AM
wave rider
+ 7
2, 3, 4, 5, and 6 of the best rated answer are FALSE!
27th Dec 2016, 3:51 PM
Iván
Iván - avatar
+ 7
"By definition, a struct is a class in which members are by default public; that is, struct s{ ... is simply shorthand for class s{ public: ... ...Except for the different names, the following declarations are equivalent: class Date1{ int d, m, y; public: Date1(int dd, int mm, int yy) ; void add_year(int n) ; / / add n years }; struct Date2{ private: int d, m, y; public: Date2(int dd, int mm, int yy) ; void add_year(int n) ; / / add n years }; Which style you use depends on circumstances and taste." SOURCE: Bjorne Stroustrup - The C++ Programming Language - Cap: 10.2.8 Structures and Classes [class.struct]
27th Dec 2016, 8:30 PM
Iván
Iván - avatar
+ 6
@Vinay That's the worst answer ever! With all that likes you are just making the community follow your mistakes. I suggest that you erase your answer for the good of the people in here that are LEARNING! Sorry about my poor english.
23rd Dec 2016, 9:16 PM
Iván
Iván - avatar
+ 5
there are different reasons to differentiate class and structure in languages like: 1. in C language,class is not there. 2. in java ,structure is not there. 3. in c++,class has all members as private by default..in structure all members are public by default. so basically,difference is compiler dependent..;)
26th Dec 2016, 10:32 AM
Himani Bhardwaj
Himani Bhardwaj - avatar
+ 3
top most answer cannot be correct always ! Pls check other responses too
27th Dec 2016, 12:10 PM
Shiv Kumar
Shiv Kumar - avatar
+ 2
class has default private members, structure has default public members. this is the basic diff
18th Nov 2016, 7:44 PM
Prerna
Prerna - avatar
+ 2
the only difference between class and structure is (in case of c++ ) default access to the members which is private in class and public for structure....
24th Dec 2016, 5:32 AM
Aruj Sharma
Aruj Sharma - avatar
+ 2
@ismail Try the app "duolingo", it's free and very good. :)
25th Dec 2016, 10:06 PM
Nemo
+ 1
In absence of an access-specifier for a base class, public is assumed when the derived class is declared struct and private is assumed when the class is declared class
5th Mar 2016, 9:07 AM
hafez
hafez - avatar
+ 1
they provide different security level.. struct can be accessed by any user but classes can not be used by anyone until defined public..
4th Dec 2016, 4:54 PM
Muhammad Rahman Arif
Muhammad Rahman Arif - avatar
+ 1
claas contains both data as well as method but structcontains data only
25th Dec 2016, 3:49 PM
Gaurav kesharwani
Gaurav kesharwani - avatar
+ 1
@Vinay don't give people wrong answer!!! As far as the compiler is concerned, there is no difference between struct and class other than the default accessibility. They're just two different keywords for defining the same thing. So, structs can have constructors, destructors, base classes, virtual functions, everything. As far as programmers are concerned, it's a common convention to use struct for classes with none of those things (specifically which are POD), or to go even further and use struct only for classes with no user-defined member functions at all, only public data members. People sometimes mess this convention up, because it's surprisingly easy to think a class is POD when it isn't, but at least they're trying. In C++, at least, information hiding is absolutely nothing to do with security. Put that right out of your mind. It does not provide any security, except in the same general way that any good coding practice makes for code that's easier to reason about, and hence programmers make fewer mistakes. The purpose of information hiding is to allow you to change the implementation later, perhaps to remove or rename private members, safe in the knowledge that none of the users of your class, outside the class itself and friends, is referring to them. Obviously it's useful to do exactly that, but less obviously and perhaps more importantly, its useful because it makes explicit in the code what your class's interface is, that you want clients to use, and that users of your class can rightfully expect to work. You can achieve the same thing in principle with documentation, but in practice it's nice for the compiler to enforce the rules. It's not "secure" because on any given compiler it's possible to work around public/private protection. But if users of your class do that, they're using some grotesque hack, they deserve for their code to stop compiling/working when you change your class, and if they come to you to complain you can laugh at them. http://stackoverflow.com/questions/3845564/s
27th Dec 2016, 8:40 PM
Navid Tak
Navid Tak - avatar
+ 1
simple answer: Differences between a class and a struct in C++ are that structs have default public members and bases and classes 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.
28th Dec 2016, 1:45 PM
Manish Garg
Manish Garg - avatar
0
My initial answer was wrong for C++ struct because I did not realize they were so vastly different to C struct. Apparently C++ modified the C struct so that it can behave almost the same as a class, except the default access modifier is public for members. This differs substantially from C struct, which had only data fields, no methods, unless you used virtual function pointers as data, no inheritance only unions, no overloading, no access control (encapsulation). C++ struct can be utilized in both the new and old form, but only the old form can use struct unions. To say that a C++ struct is ALWAYS the same as a class might therefore be incorrect. It depends if the C++ struct is implemented in the new or old style, particularly if unions are used, as that precludes the possibility of it behaving like a class. It is probably an infrequently occurring scenario, and continually decreasing in usage, but still worthy of note to be mindful of if encountered, especially in a legacy system.
3rd Dec 2016, 6:40 AM
Leif W
0
A structure is just a structure containing different variables that are clustered together. Anyone with a pointer to the structure can manipulate its content at will. However, a class can be defined to have class members of different scope. Classes that are only meant to contain data in its member, are within the object oriented programming paradigm called entity classes and are the most closes to structures. However, classes are usually used to create encapsulated functionality by disallowing direct manipulation of their members by using the private scope and thereby only make them accessible through implemented functions inside the class. So classes introduce functions for member manipulation while structures come usually without. Indicating that one can add function pointers to structures as well. In order to extrapolate this even further, I can add that 'a struct variable is to an object as typedef struct is to a class.'
4th Dec 2016, 11:26 PM
Saeed Ghasemi
Saeed Ghasemi - avatar