1. Why do we need classes and objects?
Imagine you are running an online pet store which sells fish (as well as birds, dogs and cats, etc.)
Of course you have many different types of fish. How would you describe each type of fish and list all fish on your website?
Obviously, each fish may have the following features; you may have come up with your own:
and of course a price (since they are for sale).
Suppose we have 2 fish (out of 100 total) as follows:
length: 2 in
weight: 3 oz
lifespan: 180 days
species: beta fish
length: 5 in
weight: 6 oz
lifespan: 365 days
How would you store all the above information for 100 fish in your program?
We learned about lists and tuples, so imagine storing all the species in a list, storing all the colors in a list, all the lengths in a list, and so forth.
A customer wants to see all beta fish that cost $9.99 or less and are at least 6 inches long. Can we find them quickly?
If you sell a fish, you'll have to remove it from your database. How would you do that?
We need a type of structure to hold all the info points, or attributes, together. That is what a class is for.
Remember the Fish class from last week. Our Fish only have length and weight attributes right now, but we can expand to include as many attributes as we wish.
So our Fish class could look like:
class Fish(): self.species = 'beta fish' self.color = 'blue' self.length = 5 self.weight = 6 self.lifespan = 365 self.price = 6.99
Similarly, if we sold dogs and cats in our pet store, we'd also need to define classes for dogs and cats.
What if you weren't selling pets, but rather had your own boba shop?
What objects are you selling? You would need a class to define that.
Would attributes such as "species" and "lifespan" be appropriate?
Can you think of any attributes that are important when selling boba, but not when selling pets?
This is why we need to know about classes and objects!
EXERCISE 1: Let's say you need to write a program to manage information of students at your school (since they'll all be learning remotely). What would you need to describe a student?
EXERCISE 2: How about the drinks available at your boba shop?
2. So, what is a class?
Class definition: a set of attributes to define any type of object (fish, student, boba drink, etc.) where you declare that everything that you call a fish/student/boba drink must have ALL of those attributes (a fish's species, a student's first and last name, the size of a boba drink, etc.)
3. But, only having classes are not enough. Think about a goldfish and beta fish.
In the real world, you would buy a specific fish and not just "any fish with a color, length, weight, and lifespan". Generally, you wouldn't go to the cash register without knowing that you want a goldfish, beta fish, or something else. The beta fish and goldfish that you see above are objects of type Fish.
Similarly, when you meet a student, you would know immediately that, for example, his name is John, he is in 7th grade, and he takes algebra. You would expect every student to have a name, a grade level, and one or more classes they are taking; there is no such thing as a generic student with no name, grade level, or anything else. John is an example of an object of type Student.
4. How would you apply the concept of classes and objects to the real world?
In the case of your pet shop, you can answer questions such as...
How many beta fish do I have for sale?
How much money have I made selling goldfish?
A customer wants an angelfish that costs no more than $4.99. Can I satisfy their request?
Can you think of other questions related to the day to day operations of a fish shop? What attributes of fish would be needed to answer those questions?
HW: Imagine you are an air traffic controller at San Francisco airport. Can you give 5 attributes for all the flights that take off and land?