What is the difference between a structure and a class?

Asked 07-Dec-2019
Viewed 501 times

1 Answer


0

Technically, both class and structure are almost same. The major difference being that class combines both data and methods generally and provides the concept of re-usability as inheritance. On the other hand, structure is generally groups data. Class is a reference type and its object is created on the heap memory whereas the structure is value type and its object is created on the stack memory. Find out stack memory vs heap memory. Size of empty class is one byte whereas that of an empty structure is zero byte. Class is preferred to structure in case of larger and complex objects whereas structure is preferred in case of small, isolated model objects. In conclusion, it is very easy to create structures that look a lot like class or vice versa. It all depends on the programmer and the needs.