Difference between Class and Structure

Here you will learn about difference between class and structure.

A class and a structure, both are user defined data types and understanding the difference between both of them might be confusing at times. In order to understand this, the following table shows clear differences between the two.

Class Structure
It is a reference data type and uses the keyword “class”. It is a value data type and uses the keyword “struct”.
Object for a class is created in the heap memory. Object for a structure is created in the stack memory.
We can always inherit another class. i.e., the concept of inheritance is applied . Structures can never be inherited.
Object is created using the “new” keyword. We may or may not use the keyword “new” while creating objects.
It occupies more space. A structure occupies less space.
Class allows both the parameterized and the non parameterized constructors. It only allows for the parameterized constructors, even the default constructors cannot be used.
Example:

class fruits

{

Fruit F1;

F1.apple= “red”;

F1.mango=”yellow”;

}

Example:

public struct fruit

{

public string apple;

public string mango;

}

The post Difference between Class and Structure appeared first on The Crazy Programmer.

Source: Computer

Leave a Reply

Your email address will not be published. Required fields are marked *