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






6 comments:

Felix (2b03) said...

http://snipt.net/UJuzGtJacked/cat-shelter-problem?key=9fbd33a1ab19e0aa1c353da160371906

Felix (2b03) said...

I gt a problem with the program above.How do i declare the "c"? and if i change the char to string got more error. y?

HY said...

You're trying to access the 3 variables using a for loop which wouldn't work because the variables are not arrays. You got more errors simply because strcpy is for character arrays.

Felix (2b03) said...

ok gt it. i will post the solution asap.

Felix (2b03) said...

http://snipt.net/UJuzGtJacked/cat-shelter/?key=5b9e87ebb62e0061b983cf4ddd7a6385

Cat Shelter

sjteo said...

@felix, nice work. now you have a code that tracks object via array. eg. in each array cell there is 1 kitty object residing in it. and each kitty object have several var that you have declare.

in the later weeks, we will be looking at using other data struc to hold the obj. Nonetheless, it bears alot of reassemblies of the code you written 2 days ago