Difference Between Structure and Class

  • Creator
  • #4969

    Structure is same as class except the following differences:
    Members of a class are private by default and members of struct are public by default.
    Structures are Values types while Classes are Reference types.
    Classes allow to perform cleanup (garbage collector) before object is deallocated because garbage collector works on heap memory. Objects are usually deallocated when instance is no longer referenced by other code. Structures can not be garbage collector so no efficient memory management.
    Sizeof empty class is 1 Byte where as Sizeof empty structure is 0 Bytes.
    In structure values stored in stack while in class value’s reference stored in heap.
    In structure direct values is stored while in class reference to a value is stored.
    Inheritance is supported in classes while structure doesn’t support.
    We cannot declare destructor in structure whereas in class it is possible.
    We can’t have explicitly parameter less constructors in structure whereas classes can have.
    Class can have protected members while structure can’t have.
    Structure is declared by using struct keyword while class is declared by using Class keyword.
    Structures don’t have memory management while classes have due to garbage collector.
    New operator works in classes while not in structure

  • You must be logged in to reply to this topic.