What is the difference between class and structure?
The main difference between class and struct 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 for storing data. Each variable has a specific data type that it can store. Mainly, there are two types of data as value type and reference type. In the value type, a value is assigned directly. Also, 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, while 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
what is the class
Everything is an object in object-oriented programming (OOP). Objects are created using a class. A class is a blueprint for creating an object. Furthermore, a class consists of attributes and methods. Attributes are also called properties and define the characteristics of an object. Methods define the behavior of the object. Also, creating an object using a class is called instantiation.
Figure 1: UML diagram of a class
Members of a class are the attributes and methods of a class. There are primarily three access specifiers to support data hiding in OOP: public, private, and protected. First, public members are visible to all classes. Second, private members are only visible within the same class. Third, protected members are visible within the package and by subclasses.
Furthermore, a class also contains a special function called a constructor. Helps to create a new object. Also, 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 clerk
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 object of type Employee. Next, the values are given to id and name.
What is the structure
The structure is a single variable that contains multiple data types. In other words, it is a collection of variables of different data types, all referenced by a name. The struct declaration forms a template that helps create an instance of the struct. An example is the following.
structure employee
public int id;
public string name;
The main program is as follows.
Employee and;
e.id = 101;
e.name = “Ann”;
According to the above program, the structure contains two properties: id and name. In the main program, the ‘Employee e’ declaration declares ‘e’ as an Employee struct type. So, the values are given to id and name.
Difference Between Class and Structure
Definition
A class is a blueprint that defines the variables and methods common to all objects of a given type. It is a reference type data type. Conversely, a struct is a value type data type that can contain related data of multiple data types. Therefore, the main difference between Class and Structure is in the data type.
Inheritance
A class can inherit from other classes or structs, while a struct cannot inherit from other classes or structs.
garbage incinerator
Also, destructor is another difference between class and struct. The class may have a destructor, but the struct does not have a destructor.
instantiation
Also, another difference between Class and Struct is that a class instantiates an object using a new keyword while a struct instantiates an object without using a new keyword.
Example
Also, the instance of a class is an object, while the instance of a struct is a struct variable. Hence, this is another difference between Class and Structure.
Keyword
Also, the “class” keyword defines a class. The keyword “structure” defines a structure.
Default access specifier
Also, in a class, if no access specifiers have been declared, the members are private. In a structure, if there are no declared access specifiers, 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.
Courtesy image:
1. “Oop-uml-class-example” by Original uploader was Esap at English Wikipedia. – Transferred from en.wikipedia to Commons (CC BY-SA 3.0) via Commons Wikimedia