What are similarities between a class and a structure.

Started by beingchinmay, 01-17-2017, 23:36:21

Previous topic - Next topic

beingchinmayTopic starter

Structures and classes are the two most important data structures that are used by programmers to build modular programs by using OOP languages, such as Visual Basic .NET, and Visual C#. The following are some of the similarities between a class and a structure:



 Access specifiers, such as public, private, and protected, are identically used in structures and classes to restrict the access of their data and methods outside their body.
 The access level for class members and struct members, including nested classes and structs, is private by default. Private nested types are not accessible from outside the containing type.
 Both can have constructors, methods, properties, fields, constants, enumerations, events, and event handlers.
 Both structures and classes can implement interfaces to use multiple-inheritance in code.
 Both structures and classes can have constructors with parameter.
 Both structures and classes can have delegates and events.


wellliving

Similarities between classes and structures: -

Both can have constructors, methods, properties, fields, constants, enumerations, events, and event handlers.
Structures and classes can implement interface.
Both of them can have constructors with and without parameter.

Both can have delegates and events.


Tanu S

Hi. Here are some of the basic similarities between a class and a structure I would like to mention:
Class and structures both have constructors, methods, properties, fields, constants, enumerations, events, and event handlers.
Both classes and structures can implement interfaces to use multiple-inheritance in code.
Both can have constructors with parameters.
And, both can have delegates and events.
  •  

Lishmalinyjames

Both can have constructors, methods, properties, fields, constants, enumerations, events, and event handlers. Structures and classes can implement the interface. Both of them can have constructors with and without parameter.

saravanan28

Basically, a class combines the fields and methods(member function which defines actions) into a single unit. A structure is a collection of variables of different data types under a single unit. It is almost similar to a class because both are user-defined data types and both hold a bunch of different data types.
  •  


Lishmalinyjames

Both classes and structures are used as building blocks in object-oriented programming and share several similarities:

1. Encapsulation: Both classes and structures provide a way to encapsulate data members and methods into a single unit. This enables the concept of data hiding and abstraction, allowing controlled access to the internal implementation details.

2. Member Variables: Both classes and structures can have member variables that store data. These variables can have different visibility levels (public, private, protected) to control their accessibility.

3. Member Functions: Both classes and structures can have member functions (also known as methods) that define the behavior or operations that can be performed on the data. These functions can manipulate the data and perform various tasks within the class or structure.

4. Object Creation: Objects can be created from both classes and structures. These objects represent instances of the class or structure and can be used to access the member variables and functions.

5. Inheritance: Both classes and structures support inheritance, where one class or structure can inherit properties and behaviors from another. This allows for code reuse and the creation of hierarchical relationships between classes and structures.

Despite these similarities, there are also some differences between classes and structures in certain programming languages. For example, in some languages, structures are typically used for lightweight data containers without inheritance or complex behavior, while classes are used for more complex entities with inheritance and advanced functionality. However, the exact specifics may vary depending on the programming language being used.

Both can have constructors, methods, properties, fields, constants, enumerations, events, and event handlers. Structures and classes can implement interface. Both of them can have constructors with and without parameter.