Always two there are, a master and an apprentice. -yoda
Monday, November 1, 2010
[DSA] Class & Objects
In programming, class is a common topic. No matter is C++ or Java, the same concept on class still applies. Killing N birds with 1 stone.
In programming, objects are end product of a class. Objects represent data that are generalized by the class.
Think of the Class as a cookie cutter, Objects are the cookies, the dough is generic material that all cookie need to use. Each object may have some unique attribute(s), such you would sprinkle some chocolate on the dough to make a chocolate chip cookie or add some almond to it almond cookie. In the generalization point of view (the class pov), it is still cookie. All cookie uses the same dough.
Think about it, I went to eat dim sum http://en.wikipedia.org/wiki/Dim_sum last weekend and realize some of the items taste the same. I wonder they use the class method to make it. I.e generic minced meat to make dim sum of different shapes.
How would data be generalized and yet representable?
For an example, Cats and dogs. Both are mammals. Both have four legs. Does it means that the cat is a dog and vice versa???!!! Sure we can think of some other attribute that differentiates a dog and a cat. dog barks and cat meows.
So we can have a class named pets, we can have data attribute of pet type, pet breed etc. Below is the sample code for inventory I have for the dog class for my imaginary dog kennel.
drill question 1, create a class for a cat shelter? Think of the attribute you can put into the class to represent the data of a cat. Some attributes like owner telephone and address should be set as private, and accompany by the neccesary get and set function.
drill question 2, create bank account objects with classes as specified in DS03 Objects Q3.
Notice from the code I posted, I have to manually track the objects, manually remember what objects I have created and use it.
In programming, the ability to link one topic learned previously to another topic, "mesh" the knowledge together is very important. So to speak.
From data structures , we know that it is convenient to manipulate data in a data structure, and depends on the condition we can choose the best data structure to represent the data. Can we "link" class&objects with data structures?
Refer to the code below,
Drill question 3. Based on the class you have created, can you store the objects in a data structure of your choice???? Think of what are the possible applications for data structures "meshing" with class&objects?
HINT: try to use constructors
below is the code for the SpStudent class in the slides, with my comments