+ 1
Why do we need objects if there are variables?
13 Respuestas
+ 3
Its purpose is to store a value at a memory location. Data object is a general term for a region of data storage that can be used to hold values. The C standard uses just the term object for this concept. One way to identify an object is by using the name of a variable.
+ 3
Mr_Developer
Do you mean what's the purpose of objects when you can get the job done with just primitive variables?
+ 2
The major difference lies in how we initialize and use variables and objects. Variable is a named storage of some data type(like int, float etc). And Objects are variables of data types that are user defined. So we say that an object is an instance of a class.
Similarly, what is the difference between an object and an object variable?
You can think of it in non-computing terms - an object is a thing, whereas a variable is something that points at the thing (not strictly correct, but it's a bit like this). Object is group of variables which help us identify a entity uniquely.
+ 2
objects are user defined variable in simple terms where we can store different types of data like structure and union
+ 1
If I am not wrong variables are only the objects. Are you telling about the object of class passed through constructor?
+ 1
I mean why are they needed. After all, an object is a specific subject in a class, for example, a class, a class, and an object, Andrey, Vanya, for example ...
+ 1
Mr_Developer
[Edit] apologies for the long answer
Supposed we have 3 arrays:
string[] weapons =
{ "sword", "staff", "bow", "rifle", "shield" };
int[] damages =
{ 20, 25, 15, 30, 10 };
string[] types =
{ "melee", "ranged", "ranged", "ranged", "melee" };
This kind of design could work, but each time you need to look for certain weapon, you need access all 3 of them to get their information, what's even worse is the readability.
An object represents a unique clone of the class, let's say when you need to search for the shield, you wouldn't have to look for the arrays, you only need the name or the id of the shield, the readability is better and everything is well organised.
In the future if you wish to add more properties (Price, Id, IsHalloween, IsChristmas, etc.) to Weapon, it'd be easier to work with a class instead of a bunch of arrays.
Hopefully this helps and I hope this would help you understand the use of class and when to use them 👍
https://code.sololearn.com/cn5CJTF6cVby/?ref=app
+ 1
Can anyone tell me if my answer is correct or not?
+ 1
Terel Schmitt You can use example using object instead of array?
+ 1
Atul [Inactive], An object is a lot like a variable. But I asked why this is needed if the variables already exist.
+ 1
See variables are primitive objects only. Now if you see some concepts of collection . In that case a single object is created and the data are stored in it
So in that case variables won't be working out
+ 1
This point can be described in more detail: an object is a group of variables that help us to uniquely identify an entity.
+ 1
Do words of different data types mean passing multiple arguments into one object?