SlideShare uma empresa Scribd logo
1 de 21
EECS 183Discussion #8: discussion.teach(“classes”);
March 9th, 2016
Kevin 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 KittyCat!
Get my cat’sVeterinarian's name.
Get my cat’sVeterinarian's name.
READY FOR MORE?
Mode:Where’sWaldo?
Find how much money the 41st person in
the crowd has.
Find how much money the 41st person in
the crowd has.
EXAMPLETIME!
“Watch and learn, but do and understand…”
- Random Guy a.k.a me
HOTEL CALIFORNIA
Such a lovely place~

Mais conteúdo relacionado

Semelhante a Discussion 8

Normal Considered Harmful
Normal Considered HarmfulNormal Considered Harmful
Normal Considered Harmfulgreenwop
 
Object Oriented Paradigm
Object Oriented ParadigmObject Oriented Paradigm
Object Oriented ParadigmHüseyin Ergin
 
Computational Thinking - 101
Computational Thinking - 101Computational Thinking - 101
Computational Thinking - 101WhizThinkers
 
Data Day Seattle, From NLP to AI
Data Day Seattle, From NLP to AIData Day Seattle, From NLP to AI
Data Day Seattle, From NLP to AIJonathan Mugan
 
Complex things explained easily
Complex things explained easilyComplex things explained easily
Complex things explained easilyLuca Tumedei
 
Nick De Mey (Board of Innovation) - Innovate like a startup
Nick De Mey (Board of Innovation) - Innovate like a startupNick De Mey (Board of Innovation) - Innovate like a startup
Nick De Mey (Board of Innovation) - Innovate like a startupAnis Bedda
 
From Zero to Iterators: Building and Extending the Iterator Hierarchy in a Mo...
From Zero to Iterators: Building and Extending the Iterator Hierarchy in a Mo...From Zero to Iterators: Building and Extending the Iterator Hierarchy in a Mo...
From Zero to Iterators: Building and Extending the Iterator Hierarchy in a Mo...Patrick Niedzielski
 
Widgets&wizardry for MFL
Widgets&wizardry for MFLWidgets&wizardry for MFL
Widgets&wizardry for MFLjonmeier
 
From Natural Language Processing to Artificial Intelligence
From Natural Language Processing to Artificial IntelligenceFrom Natural Language Processing to Artificial Intelligence
From Natural Language Processing to Artificial IntelligenceJonathan Mugan
 
Ruby object model
Ruby object modelRuby object model
Ruby object modelmbeizer
 
Oz_Chap 2_M3_Lesson Slides_Variables.pptx
Oz_Chap 2_M3_Lesson Slides_Variables.pptxOz_Chap 2_M3_Lesson Slides_Variables.pptx
Oz_Chap 2_M3_Lesson Slides_Variables.pptxALEJANDROLEONGOVEA
 

Semelhante a Discussion 8 (15)

Scratch middle school gifted
Scratch   middle school giftedScratch   middle school gifted
Scratch middle school gifted
 
Normal Considered Harmful
Normal Considered HarmfulNormal Considered Harmful
Normal Considered Harmful
 
Object Oriented Paradigm
Object Oriented ParadigmObject Oriented Paradigm
Object Oriented Paradigm
 
Computational Thinking - 101
Computational Thinking - 101Computational Thinking - 101
Computational Thinking - 101
 
Data Day Seattle, From NLP to AI
Data Day Seattle, From NLP to AIData Day Seattle, From NLP to AI
Data Day Seattle, From NLP to AI
 
Organise a Code Dojo!
Organise a Code Dojo!Organise a Code Dojo!
Organise a Code Dojo!
 
Complex things explained easily
Complex things explained easilyComplex things explained easily
Complex things explained easily
 
Nick De Mey (Board of Innovation) - Innovate like a startup
Nick De Mey (Board of Innovation) - Innovate like a startupNick De Mey (Board of Innovation) - Innovate like a startup
Nick De Mey (Board of Innovation) - Innovate like a startup
 
Quality code 2019
Quality code 2019Quality code 2019
Quality code 2019
 
From Zero to Iterators: Building and Extending the Iterator Hierarchy in a Mo...
From Zero to Iterators: Building and Extending the Iterator Hierarchy in a Mo...From Zero to Iterators: Building and Extending the Iterator Hierarchy in a Mo...
From Zero to Iterators: Building and Extending the Iterator Hierarchy in a Mo...
 
Widgets&wizardry for MFL
Widgets&wizardry for MFLWidgets&wizardry for MFL
Widgets&wizardry for MFL
 
From Natural Language Processing to Artificial Intelligence
From Natural Language Processing to Artificial IntelligenceFrom Natural Language Processing to Artificial Intelligence
From Natural Language Processing to Artificial Intelligence
 
Ruby object model
Ruby object modelRuby object model
Ruby object model
 
Oz_Chap 2_M3_Lesson Slides_Variables.pptx
Oz_Chap 2_M3_Lesson Slides_Variables.pptxOz_Chap 2_M3_Lesson Slides_Variables.pptx
Oz_Chap 2_M3_Lesson Slides_Variables.pptx
 
Valitutti acii2015 talk
Valitutti acii2015 talkValitutti acii2015 talk
Valitutti acii2015 talk
 

Discussion 8

  • 1. EECS 183Discussion #8: discussion.teach(“classes”); March 9th, 2016 Kevin Lee (mrkevin)
  • 2. 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.
  • 3. 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!
  • 4. 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();
  • 5. 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:
  • 6. 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:
  • 9. 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.
  • 10. 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
  • 12. QUESTIONS? Asking a class if they have questions about classes… That’s so meta.
  • 13. READY FOR FUN? Mode: Cat, I’m a KittyCat!
  • 17. Find how much money the 41st person in the crowd has.
  • 18. Find how much money the 41st person in the crowd has.
  • 19. EXAMPLETIME! “Watch and learn, but do and understand…” - Random Guy a.k.a me
  • 20.
  • 21. HOTEL CALIFORNIA Such a lovely place~