discussion 8
TRANSCRIPT
EECS 183Discussion #8: discussion.teach(“classes”);
March 9th, 2016Kevin Lee (mrkevin)
What is a class?
■ A class is a container that can hold many different types of objects.
■ What is another type of container that we have learned that can only contain objects of the same type?– Arrays!
■ int arr[3] holds 3 ints only.■ A class “Person” can hold many different types of objects inside of it.
Classes are…
■ The ingredients to a cake!– Many different components needed.– Egg cartons are arrays, holding only eggs.
■ The statistics of a movie!– How long is the movie? Who plays in it?– A class can hold all of that information!
■ A hotel!– Is it full? What’s its name?– Hotel can hold an array of Rooms, which also is a class!
Overview of Classes
■ Private vs. Public– Private means only functions inside of the class can use it!– Public means anyone from any other function (like main()) can use it!
■ Constructor– Allows you to initialize all variables of the class to some base number like 0.
■ Getters and Setters– Usually member variables are private, so you will always need a
getter/setter!■ Calling the Class
– Class.function();
Private vs. Public
■ Classes are inherently private, meaning that they start off as private unless otherwise specified.
■ Only the class’s own functions can touch its private member variables.– NOT OKAY: OKAY:
Constructor■ When we have the code int x; … What is x?
– Undefined! Because we don’t have a constructor for an int variable.
– Imagine being able to always set an undefined variable to zero…!■ Two types of constructors:
Constructor
Overloaded Constructor
&&
Getters and Setters■ How fat is my cat?■ How do I change my cat’s name?■ My cat bit me! How do I change its niceness?
■ Answer: Getters and Setters!– You need to write a function to return or change its private
member variables.
Calling a Function
■ dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot dot
Calling a Function
QUESTIONS?Asking a class if they have questions about
classes…That’s so meta.
READY FOR FUN?Mode: Cat, I’m a Kitty Cat!
Get my cat’s Veterinarian's name.
Get my cat’s Veterinarian's name.
READY FOR MORE?Mode: Where’s Waldo?
Find how much money the 41st person in the crowd has.
Find how much money the 41st person in the crowd has.
EXAMPLE TIME!“Watch and learn, but do and understand…”
- Random Guy a.k.a me
HOTEL CALIFORNIA
Such a lovely place~