What is the difference between class and structure?

Class and structure are two different types of objects. However, each one is very different from the other and each has its own set of advantages and disadvantages.

Class

Structure

Is a reference type

Is a value type

Its object is created on Heap memory.

Its object is created on Stack memory.

Can inherit another class.

Does not support the inheritance and cannot inherit another class.

Can have constructor and destructor of all types.

Structure can only have the parameterized constructor.

The member variable can be initialized directly.

The member variable cannot be initialized directly.

Class object cannot be created without using the new keyword

Structure object can be created without using the new keyword.

Members of a class are private by default

Members of structure are public by default.

Are used to model more complex behavior or data that is intended to be modified after a class object is created.

Are best suited for small data structures that contain primarily data that is not intended to be modified after the structure is created.

Can be abstract

Can't be abstract

Contains data member and member function.

Contains only data member

Values are assigned

Can't initialize the value to the variable

Can contain a volatile field

Can't contain a volatile field

Can't use sizeof with classes

Can use sizeof with classes

Fields are automatically initialized with classes to 0/false/null

Fields are not automatically initialized

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.