Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between an object and a struct in OOP?

  • What distinguishes and object from a struct?
  • When and why do we use an object as opposed to a struct?
  • How does an array differ from both, and when and why would we use an array as opposed to an object or a struct?

I would like to get an idea of what each is intended for.

like image 879
Mohamad Avatar asked Dec 22 '10 23:12

Mohamad


People also ask

What is the difference between structured and object oriented programming?

The main difference between structured and object oriented programming is that structured programming allows developing a program using a set of modules or functions, while object oriented programming allows constructing a program using a set of objects and their interactions.

What is the difference between struct and struct?

Basically struct is used to define a structure. But when we want to use it we have to use the struct keyword in C. If we use the typedef keyword, then a new name, we can use the struct by that name, without writing the struct keyword.

What is the main difference between a struct and a class?

Difference between Structs and Classes: Struct are value types whereas Classes are reference types. Structs are stored on the stack whereas Classes are stored on the heap. Value types hold their value in memory where they are declared, but a reference type holds a reference to an object in memory.


1 Answers

Obviously you can blur the distinctions according to your programming style, but generally a struct is a structured piece of data. An object is a sovereign entity that can perform some sort of task. In most systems, objects have some state and as a result have some structured data behind them. However, one of the primary functions of a well-designed class is data hiding — exactly how a class achieves whatever it does is opaque and irrelevant.

Since classes can be used to represent classic data structures such as arrays, hash maps, trees, etc, you often see them as the individual things within a block of structured data.

An array is a block of unstructured data. In many programming languages, every separate thing in an array must be of the same basic type (such as every one being an integer number, every one being a string, or similar) but that isn't true in many other languages.

As guidelines:

  • use an array as a place to put a large group of things with no other inherent structure or hierarchy, such as "all receipts from January" or "everything I bought in Denmark"
  • use structured data to compound several discrete bits of data into a single block, such as you might want to combine an x position and a y position to describe a point
  • use an object where there's a particular actor or thing that thinks or acts for itself

The implicit purpose of an object is therefore directly to associate tasks with the data on which they can operate and to bundle that all together so that no other part of the system can interfere. Obeying proper object-oriented design principles may require discipline at first but will ultimately massively improve your code structure and hence your ability to tackle larger projects and to work with others.

like image 148
Tommy Avatar answered Oct 13 '22 00:10

Tommy