What is the Difference Between Class and Structure

The main difference between Class and Structure is that the Class is a reference type data type while the Structure is a value type data type.

In programming, a variable is a storage area to store data. Each variable has a specific data type it can store. Mainly, there are two data types as value type and reference type. In value type, a value is assigned directly. Moreover, int, float, and double are some examples of value type data types. On the other hand, reference type data types do not store the actual data. Instead, it stores a reference to the variable. Class and Structure are two programming concepts. As mentioned above, Class is a reference type data type whereas Structure is a value type data type.

Key Areas Covered

1. What is Class
     – Definition, Functionality
2. What is Structure
    – Definition, Functionality
3. What is the Difference Between Class and Structure
     – Comparison of Key Differences

Key Terms

Class, Structure

Difference Between Class and Structure - Comparison Summary

What is Class

Everything is an object in Object Oriented Programming (OOP). Objects are created using a class. A class is a blueprint to create an object. In addition, a class consists of attributes and methods. Attributes are also called properties, and they define the characteristics of an object. The methods define the behaviour of the object. Furthermore, creating an object using a class is called instantiation.

Difference Between Class and Structure

Figure 1: UML Diagram of a Class

Members of a class are the attributes and methods of a class. Mainly, they are three access specifiers to support data hiding in OOP: public, private and protected. Firstly, the public members are visible to all the classes. Secondly, the private members are only visible within the same class. Thirdly, the protected members are visible within the package and by the subclasses.

Furthermore, a class also contains a special function called a constructor. It helps to create a new object. In addition, it has the same name as class and has no return type. It can be a default constructor or a parameterized constructor.

An example class is as follows.

Class Employee{

public int id;

public string name;

}

The main program is as follows.

Employee e = new Employee();

e.id=101;

e.name= “Ann”;

According to the above program, the class has two properties: id and name. They are public and accessible by any other class. In the main program, the constructor creates an Employee type object. Then, the values are given to id and name.

What is Structure

Structure is a single variable that holds multiple data types. In other words, it is a collection of variables of dissimilar data types, all referencing by one name. Structure declaration forms a template that helps to create an instance of the structure. An example is as follows.

struct Employee{

public int id;

public string name;

};

The main program is as follows.

Employee e;

e.id=101;

e.name= “Ann”;

According to the above program, the structure contains two properties: id and name. In the main program, ‘Employee e’ statement declares ‘e’ as a struct type Employee. Then, values are given to id and name.

Difference Between Class and Structure

Definition

A class is a blueprint that defines the variables and the methods common to all objects of a certain kind. It is a reference type data type. Conversely, a structure is a value type data type that can hold related data of various data types. Therefore, the main difference between Class and Structure is in the data type.

Inheritance

A Class can inherit from other classes or structures whereas a Structure cannot inherit other classes or structures.

Destructor

Also, destructor is another difference between Class and Structure. Class can have a destructor, but structure does not have a destructor.

Instantiation

Furthermore, another difference between Class and Structure is that a class instantiates an object using a new keyword while a structure instantiates an object without using a new keyword.

Instance

Moreover, the instance of a class is an object while the instance of a structure is a structure variable. Hence, this is another difference between Class and Structure. 

Keyword

Besides, the keyword “class” defines a Class. The keyword “struct” defines a Structure.

Default Access Specifier

In addition, in a class, if there are no access specifiers declared, then the members are private. In a structure, if there no access specifiers declared, then the members are public.

Conclusion

In programming, it is necessary to store data. A variable is a location that is used to store data. There are two types of variables as value type and reference type. The basic difference between Class and Structure is that a Class is a reference type data type while a Structure is a value type data type.

Reference:

1. “C# Classes.” Www.tutorialspoint.com, Available here.
2. “C# Structures.” Www.tutorialspoint.com, Available here.

Image Courtesy:

1. “Oop-uml-class-example” By The original uploader was Esap at English Wikipedia. – Transferred from en.wikipedia to Commons (CC BY-SA 3.0) via Commons Wikimedia

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply