Difference between Class and Struct

Before we understand the difference between Class and Struct, we should know a few basic concepts associated with them. Without an understanding of concepts, it is difficult to comprehend the differences between the two.

What Is Class and Object:

These are the two important concepts related to Object Oriented Programming, and they form the basis for framing the functions and data passing to other functions. For a better understanding, we can correlate it to a real-life example. A Class can be considered as a store, and an Object can be any of the individual, specific stores such as grocery, stationery, fruit, etc. All objects share the common properties of the main class –  the store –, and in turn, the objects may have their own exclusive properties such as a specific design, lighting, etc.  Through the objects, we can actually use the class; they form the instances of a class.

Syntax of a Class

Class Store {

public string things;

public string design;

}

Syntax of an Object

Store grocery = new Store();

Store Stationery = new Store();

What Is a Struct?

A Struct includes only the data, and therefore it is helpful in framing the individual data requirements through the Struct objects. Unlike a class, it lacks functions. Here is its syntax:

struct grocery_entrance{

char entrance_name[50];

int entrance_size;

} grocery_entrance;

All stores can use the Struct ‘entrance’ with individual names and sizes.

What Is Inheritance?

It is similar to how a son inherits the possessions of his father and, in turn, the son can also add some other possessions of his own. The Class can be either a base class or a derived class, in which the former can be taken as the base to form the latter. The derived class adds few other properties for itself apart from what it gets from the base class. When we consider the above example, the grocery store can still be derived to a specific grocery store, such as XYZ grocery store.

Now that we are familiar with the basic concepts, we can go into the actual difference between Class and Struct.

How Do They Differ?

  • Re-usability: As Classes form the basic framework, they can be re-used; Structs, however, are individual elements with specific properties, so they cannot be re-used. For example, the grocery store Class can be used for any type of grocery store, but the Struct grocery_entrance is specific for that alone and there is no point in re-using it in other classes.
  • Visibility: All the functions in a Class are publicly available to its Objects. For example, we have a function named ‘things’ under the Class ‘store’. The function ‘things’ is visible to all its Objects, such as ‘grocery store, ‘stationery store’, etc. Such visibility is not possible with Structs as struct’s data is restricted to itself and not visible to other Structs. To make things clear, we can say that the data of ‘grocery_entrance’ is not publically visible to all other stores.
  • Pass by Reference & Pass by Value: Pass by Reference is sending only the memory location and not the actual data to the functions. This means that whenever the value changes, the change is reflected in the corresponding functions. Pass by value, in turn, is just sending the value to the function. In this case, a change in the value after it is sent won’t be reflected in the function. Class uses pass by reference, and Struct uses pass value.
  • Inheritance: Classes can be further inherited to form sub-classes, but Structs cannot utilize inheritance. For example, the Class Store gives its functions to the sub-class ‘grocery store’. But the Struct ‘grocery_entrance’ cannot inherit any function. We can say that there is no concept like sub-struct here.
  • Default visibility: All the members of a Class are kept as private entities by default, whereas the members of a Struct are kept as public entities by default.
  • The size of an empty Class and Struct: Class uses a size of 1 Byte even when it is empty, whereas Struct never uses any memory when it is empty. This means we can say that size of an empty struct equals 0 Bytes.
  • Garbage collection: Garbage collection is possible with Classes, as they use pass by reference. Therefore, it is easier to do the cleanup at one location where the data is stored. On the other hand, garbage collection is not possible with Struct, as it uses pass by value and the data is scattered at different locations.
  • Memory management: Because Class allows garbage collections, the memory management is also effective; however, it is not that effective with Structs.
  • Constructor: A constructor generally initializes the class with certain specified values. We can look at it like something that has been initialized with values. If a new class has to be created, the constructor is called to allocate memory for that instance. We can even pass values as arguments while calling a constructor. Let us come to our actual discussion now. Classes allow constructors of all types, such as with or without arguments, whereas structs only allow the constructors with arguments, i.e. the parameterized constructors.
  • Destructor: A destructor is called whenever we need to delete an instance of a class. The destructor, in turn, deletes that instance and frees up memory. A Class can use a destructor, whereas a Struct cannot.
  • Member Variables Initialization: In Classes, we can initialize the member variables directly; such an initialization is not possible with Structs.
  • Object Creation: The general syntax for object creation in classes is:

Demo obj = new Demo();

This means that we must use the keyword ‘new’ while creating objects of a class. This is not required when creating objects of Structs. Just take a look at their syntax:

Demo obj;

It works perfectly even without the keyword ‘new’.

When to Use Class and When to Use Struct?

As Classes are more flexible in handing data and functions together, we can go for it when the objects used are complex and large. In our example, a Mall can use the class ‘store’ to express the system in a better manner. Structs, however, are restricted to smaller objects, as they are comparatively less effective than Classes. Therefore, if you design a store of your own, Structs are the better choice.

How to Convert a Struct  to a Class and Vice Versa?

You might have heard the terms ‘boxing’ and ‘unboxing’ to convert a Struct to a Class, and vice versa. Though these are effective processes to help us in the conversion, they should be handled with caution. As this directly affects the memory locations, there is a huge impact on the performance of our system. Moreover, it affects the garbage collection processes and results in overall system inefficiency. Therefore, use these conversions only when needed.

Let us look at the above-mentioned differences in tabular form.

S.No Concepts Differences
Class Struct
1 Re-usability Completely re-usable Not re-usable
2 Visibility All the functions of a Class are visible to its objects The data of an Object of a Struct is not visible to other objects of the same Struct
3 Pass by Reference & pass by Value Uses Pass by Reference Uses Pass by Value
4 Inheritance The functions of a class can be inherited by its subclasses; allows inheritance Never allows inheritance
5 Default Visibility All the members of a Class are private by default All the members of a Struct are public by default
6 Size When Empty The size of an empty Class is 1 Byte The size of the empty Struct is 0 Bytes
7 Garbage Collection As it uses pass by reference, garbage collection is possible As it uses pass by value, garbage collection is not possible
8 Memory Management The ease of the garbage collection process helps in effective memory management The lack of garbage collection results in poor memory management
9 Constructors Allows constructors of all types, such as with or without parameters Only allows parameterized constructors
10 Destructors Can use it Cannot use it
11 Member Variables Initialization Allows direct initialization of member variables Does not allow direct word initialization of member variables
12 Object Creation It is a must to use the keyword ‘new’ during object creation It is optional to use the keyword ‘new’ during object creation
13 When to Use? Better for larger and complex objects where inheritance is required Better for smaller and simpler objects where inheritance is of less importance.

We have almost covered all the differences between Class and Struct, and if you feel that something is missing, please let us know. Let’s learn together and make the most of that knowledge!