What is the difference between class and structure?
What is the difference between class and structure?
1) Structure: Initially (in C) a structure was used to bundle different type of data types together to perform a particular functionality. But C++ extended the structure to contain functions also. The major difference is that all declarations inside a structure are by default public.
Class: Class is a successor of Structure. By default all the members inside the class are private.
2) structures in c++ doesn't provide data hiding where as a class provides data hiding
classes support polymorphism, whereas structures don't
3) class and structure are very similar. the former is heavyweight while the latter is light weight. reference to the former rests on the heap..while the latter in whole (instance and data) rests on the stack. therefor care should be taken not to make a struct very heavy else it overloads the stack causing memory hogging. class needs to have an instance explicitly created to be used. A struct doesn't have to be explicitly initiated
==
Thanks and Regards
Meetu Choudhary
Struct are Value type and are stored on stack, while Class are Reference type and are stored on heap.
Struct “do not support" inheritance, while class supports inheritance. However struct can implements interface.
Struct should be used when you want to use a small data structure, while Class is better choice for complex data structure.