If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

Difference between Class and structure?

Started by lovtzova, Jun 20, 2022, 02:22 AM

Previous topic - Next topic

lovtzovaTopic starter

A structure is a tool that consolidates data types for a specific purpose, with classes serving as an expansion of this concept.
The primary distinction between the two is that any variable declared within a structure is accessible to the public, whereas any variable declared within a class is private and holds significance exclusively within the class itself.
  •  


Kovtalo

In contrast to structures, classes have the capability of creating a subclass that acquires the parent's characteristics and functions through inheritance. Furthermore, classes default all members to private, whereas structures operate with public member accessibility.
A struct can be considered a type of class in which its members are automatically set to public.
  •  

Sevad

1. Typically, classes are utilized for large amounts of data while structs are employed for smaller quantities of data.
2. The ability to inherit is exclusive to classes and not applicable to structures.
3. Unlike classes, structures cannot be null.
4. Structures do not possess destructors as classes do.
5. Structures cannot be abstract in nature, while classes can.
6. While it is possible to override some object type methods in a structure, the only polymorphism technique that can be applied to the struct type is through implementing interfaces.
7. When declared within a class, events are automatically locked and therefore thread-safe; however, in contrast to classes, events within structures cannot be locked.

arthyk

The concept of a class is fully related to object-oriented programming, with all the ensuing consequences. That is: encapsulation, inheritance and polymorphism - all these concepts refer to classes. In contrast, a structure is just a chain of named values that are numbered sequentially.
  •  

ivanka

A struct is a value type, a class is a reference type. The main difference between the two is that a struct is stored entirely on the stack whereas a class object is stored on the heap and its reference is on the stack. As a result, access to the data structure will be faster than to the class.
  •  

DiHard

What distinguishes structs from classes?

An example with an error is used to illustrate a key difference between the two. In structs, the compiler generates a default constructor and therefore a default (parameterless) constructor cannot be used. This results in a compile-time error. Whereas in classes, the compiler creates a default constructor only if one has not been created already. The generated constructor for a struct initializes its fields to 0, false, or null, as is the case with classes. To override these values, parameterized constructors can be used. However, when initializing fields in a constructor, all variables must be initialized, or else an error will occur. In contrast to classes, the initialization of fields cannot be done directly at the place of their declaration in structures, which will cause another compilation error.

Structs can be used just like classes and other types. Additionally, struct types can have a nullable version created using the "?" modifier, and then set to null. While struct fields can be initialized using constructors, it is possible to create a structure without using a constructor, resulting in an uninitialized field.
 
Regarding the use of structs, some argue that venerable Western authors recommend using structures as little as possible, preferring classes. However, one should think independently about the best approach to take.
  •  

harryblossom0

The main differences between classes and structures are as follows:

1. Default Accessibility: In most programming languages, the default accessibility of members in a class is private, meaning they can only be accessed within the class itself. In contrast, the default accessibility of members in a structure is usually public, meaning they can be accessed from outside the structure.

2. Inheritance: Classes allow for inheritance, where one class can inherit properties and methods from another class. This enables code reuse and the creation of hierarchies. Structures, on the other hand, usually do not support inheritance. They are primarily used for simple data encapsulation.

3. Size and Memory Allocation: Another key difference is how memory is allocated for classes and structures. Instances of classes are typically created on the heap, requiring dynamic memory allocation. In contrast, instances of structures are often created on the stack, resulting in automatic memory allocation.

4. Reference Type vs Value Type: In many languages, classes are reference types, meaning when you assign a class instance to a new variable, both variables point to the same memory location. Changes made to one variable affect the other. Structures, on the other hand, are value types. When you assign a structure instance to a new variable, a copy of the data is made. Modifications to one variable do not affect the other.

These distinctions might vary depending on the programming language you are using, but these are some common differences between classes and structures.


In PHP, both structures and classes are used to define data structures, but the terms you mentioned, "structure" and "class," are not specifically used in the context you described.

In PHP, the concept of a structure is typically represented using an associative array or a simple object. An associative array allows you to group related data together using key-value pairs, while a simple object can be created using the stdClass class or by casting an array as an object.

On the other hand, classes in PHP provide more advanced features and are used for creating objects with encapsulated properties and methods. By default, class variables are private and cannot be accessed directly from outside the class. However, you can declare them as public, protected, or private depending on your needs.

To summarize, in PHP, you would typically use associative arrays or simple objects to represent structures, while classes provide a more sophisticated approach to encapsulate data with additional functionality.
  •  



If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...