Coding & Programming > Programming Forum

What is the difference between Class and structure?

(1/2) > >>

     Structure used to bundle data’s of different data types to perform a particular function. Extended function of the structure is the class.
The major difference is that any variable declared inside the structure is public whereas any variable declared inside the class is private, meaning the variable has meaning only inside the class.

You can differentiate  Class and Structure as below:
Class is a reference type and its object is created on the heap memory. It can inherit the another class. It can have the all types of constructor and destructor.
obj=new Demo();
Structure is a value type that is why its object is created on the stack memory.
It does not support the inheritance. It can only have the parametrized constructor. it means a structure can not have the non-parametrized constructor,default constructor and destructor also.
Demo obj;
I hope my answer helps you.

A structure is a value type so it is stored on the stack, but a class is a reference type and is stored on the heap. A structure doesn't support inheritance, and polymorphism, but a class supports both. By default, all the struct members are public but class members are by default private in nature.

1: Classes are usually used for large amounts of data, whereas structs are usually used for smaller amounts of data.
2: Classes can be inherited whereas structures not.
3: A structure couldn't be null like a class.
4: A structure couldn't have a destructor such as a class.
5: A structure can't be abstract, a class can.
6: You cannot override any methods within a structure except the following belonging to the type object:

And the other polymorphism technique used for structures is implementing interfaces.
7: Declared events within a class are automatically locked and then they are thread safe, in contrast to the structure type where events can't be locked.

The class can create a subclass that will inherit the parent's properties and methods, whereas Structure does not support the inheritance.  A class has all members private by default. A struct is a class where members are public by default.


[0] Message Index

[#] Next page

Go to full version