Concept
What problems arise when modeling students with separate variables and arrays?
ComputerScienceOne / Encapsulation & Objects
"As a motivating example, suppose we were to write a program that involved organizing the enrollment of students into courses. To model a particular student, we would need a collection of variables, say a first name, last name, GPA, and a unique identification number (likely a lot more, but let’s keep it simple). Each of these pieces of data could be modeled by strings, a floating-point number and perhaps an integer. 1 Each of these pieces of data are stored in separate, unrelated variables even though they represent a single entity . Even worse, suppose that we needed to keep track of a collection of students. Each piece of data would need to be stored in a separate array. If we wanted to rearrange the data (say, sort it), we would need to do a lot of manual bookkeeping to make sure that the separate pieces of data that represented a single entity were all aligned at the same index in each of the arrays. If we wanted to pass the data around to functions, we’d be forced to pass multiple arrays to each function. This becomes all the more complex when we attempt to model entities with more pieces of data."
Related Ideas
- How can one structure be composed of another structure?ComputerScienceOne · Declaration & Initialization
- How do you create and use an array of structures in C?ComputerScienceOne · Arrays of Structures
- How do you pass an array of structures to a function to compute an average GPA?ComputerScienceOne · Passing Arrays of Structures
- How does composition model objects made up of other objects?ComputerScienceOne · Composition
- What does the completed Java Student class example contain?ComputerScienceOne · Example
- How should a class manage owned collections and avoid shallow copies?ComputerScienceOne · Composition
- How can comparator functions order Student structures?ComputerScienceOne · Comparator Functions
- How can an array of structure pointers make swapping structures more efficient?ComputerScienceOne · Arrays of Structures