What is the difference between Class and structure?

Started by chinmay.sahoo, 01-05-2017, 03:23:06

Previous topic - Next topic

chinmay.sahooTopic starter

     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.


michral86

You can differentiate  Class and Structure as below:
Class
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.
Example:
obj=new Demo();
Structure
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.
Example:
Demo obj;
I hope my answer helps you.


richardmsmith

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.

greatshivam

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:

Equals()
GetHashCode()
GetType()
ToString()

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.
  •  

Lishmalinyjames

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.


saravanan28

A class is a user-defined blueprint or prototype from which objects are created. Basically, a class combines the fields and methods(member function which defines actions) into a single unit. A structure is a collection of variables of different data types under a single unit.
  •  

Lishmalinyjames

A class is a user-defined blueprint or prototype from which objects are created. Basically, a class combines the fields and methods(member function which defines actions) into a single unit. A structure is a collection of variables of different data types under a single unit.

dark404

Structure used to package information's of various information types to carry out a specific role. Broadened capability of the design is the class.
The significant contrast is that any factor announced inside the design is public though any factor proclaimed inside the class is private, meaning the variable has meaning just inside the class.