Data Abstraction in Structure
As you already know that List allow data
abstraction in that you can give a name to a set of memory cells. For instance,
in the game Mastermind, you must keep track of a list of four colors that the
player guesses. Instead of using four separate variables (color1, color2, color3,
and color4) you can use a single variable ‘Predict’, e.g.,
Predict =['red', 'blue', 'green', 'green']
What lists do not allow us to do is name the
various parts of a multi- item object. In the case of a Predict, you don't
really need to name the parts:
using an index to get to each color suffices.
But in the case of something more complex, like a person, we have a multi-item object where each 'item' is a named thing: the firstName, the lastName, the id, and the email. One could use a list to represent a person:
person=['Padmashri', 'Baskar', '994-222-1234',
but such a representation doesn't explicitly
specify what each part represents.
For this problem instead of using a list, you
can use the structure construct (In OOP languages it's called class construct)
to represent multi-part objects where each part is named (given a name).
Consider the following pseudo code:
firstName := " "
lastName := " "
id := " "
email := " "
The new data type Person is pictorially
The class (structure) construct defines the
form for multi-part objects that represent a person. Its definition adds a new
data type, in this case a type named Person. Once defined, we can create new
variables (instances) of the type. In this example
Person is referred to as a class or a type,
while p1 is referred to as an object or an instance. You can think of class Person as a cookie
cutter, and p1 as a particular cookie. Using the cookie cutter you can make
many cookies. Same way using class you can create many objects of that type.
So far, you've seen how a class defines a data
abstraction by grouping related data items. A class is not just data, it has
functions defined within it. We say such functions are subordinate to the class
because their job is to do things with the data of the class, e.g., to modify
or analyze the data of a Person object.
Therefore we can define a class as bundled
data and the functions that work on that data. From All the above
example and explanation one can conclude the beauty of data abstraction is that
we can treat complex data in a very simple way.