0

I came across this question:

"There are two types of variable data – dependent and independent. Which type is recommended to be created inside a struct and why?"

My attempt at an answer:

So I created some structs

struct Node{
    int node;
    Node *ptr;
}

struct Book{
    int page;
    Book *nxtPg;
}

struct Fruit{
    string name;
    float weight;
}

I can see that the variables are dependent. Is it correct to say that dependent variables would be recommended because structs group similar data together. And to answer the why part, is it correct to say that independent variables would defeat the purpose of creating a struct?

Sean Paul
  • 73
  • 6
  • 14
    It is not clear what the _dependent and independent variables_ means. It might be some frivolous terminology. Please have a look at this [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) list. – Ron Oct 27 '17 at 09:03
  • Is a friut name dependent on the fruit weight? –  Oct 27 '17 at 09:04
  • *Where* did you find the question? In a book? In an exam? Can you please tell us? And perhaps tell us the answer from the book/exam, and how it differs from your interpretation? – Some programmer dude Oct 27 '17 at 09:08
  • I haven't seen any notion of dependent and independent variables in context of c++. The only context I know is mathematics, and in this view, every variable is independent of each other, because their values are not correlated in any way. – luk32 Oct 27 '17 at 09:12
  • @Someprogrammerdude it's from a past year paper. Unfortunately, I don't have the answer because the lecturer who asked this question no longer teaches at my university and the current lecturer for the course doesn't give answers for papers made by other lecturers. – Sean Paul Oct 27 '17 at 09:15
  • @manni66 no it's not, sorry – Sean Paul Oct 27 '17 at 09:16
  • And does that mean that one should handle the name and the weight of a fruit in independent variables? –  Oct 27 '17 at 09:23
  • I think it's rather a question of modeling (though I understand the programming language as the tool to express the model). Regarding the `Fruit`: structures (in other languages: records) are a common way to bundle relavant properties of a modeled (real-world) object. (From this point of view, I _see_ a dependency of `name` and `weight` - both properties belong to the same thing - in C++: instance.) More about this I found on Wikipedia: [Object composition](https://en.wikipedia.org/wiki/Object_composition) . – Scheff's Cat Oct 27 '17 at 09:29
  • I would say that the question actually doesn't make sense, whatever the terminology. The items you put into the struct (or class, they're the same) is everything related to that object; but how you ACCESS those variables might be different. – UKMonkey Oct 27 '17 at 09:37

1 Answers1

0

I suspect the question is designed to get you thinking about the object oriented paradigm: high cohesion, loose coupling of which there is plenty of information for you to read through on the internet.

Objects encapsulating data where that does not depend on other data (i.e. the objects do not depend on other objects) should be separate and not coupled. Objects that encapsulating data with dependencies have high cohesion and it is better to group that data together rather than scatter the data about meaning the objects would be coupled to other objects.

keith
  • 5,122
  • 3
  • 21
  • 50