What is the difference between a structure and a class?

0 votes
186 views
Shikhar Arora asked 07-Dec-2019 in Technology by Shikhar Arora

What is the difference between a structure and a class

1 Answer

0 votes
Sangsaptak Pal answered 18-Jan-2020 by Sangsaptak Pal
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.