discussion 8

21
EECS 183 Discussion #8: discussion.teach(“classes”); March 9 th , 2016 Kevin Lee (mrkevin)

Upload: kevin-lee

Post on 14-Apr-2017

49 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Discussion 8

EECS 183Discussion #8: discussion.teach(“classes”);

March 9th, 2016Kevin Lee (mrkevin)

Page 2: Discussion 8

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.

Page 3: Discussion 8

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!

Page 4: Discussion 8

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();

Page 5: Discussion 8

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:

Page 6: Discussion 8

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:

Page 7: Discussion 8

Constructor

Page 8: Discussion 8

Overloaded Constructor

&&

Page 9: Discussion 8

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.

Page 10: Discussion 8

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

Page 11: Discussion 8

Calling a Function

Page 12: Discussion 8

QUESTIONS?Asking a class if they have questions about

classes…That’s so meta.

Page 13: Discussion 8

READY FOR FUN?Mode: Cat, I’m a Kitty Cat!

Page 14: Discussion 8

Get my cat’s Veterinarian's name.

Page 15: Discussion 8

Get my cat’s Veterinarian's name.

Page 16: Discussion 8

READY FOR MORE?Mode: Where’s Waldo?

Page 17: Discussion 8

Find how much money the 41st person in the crowd has.

Page 18: Discussion 8

Find how much money the 41st person in the crowd has.

Page 19: Discussion 8

EXAMPLE TIME!“Watch and learn, but do and understand…”

- Random Guy a.k.a me

Page 20: Discussion 8
Page 21: Discussion 8

HOTEL CALIFORNIA

Such a lovely place~