The Basics of Object Oriented Programming

The Basics of Object Oriented Programming

Spoken at The Game Assembly to 1st year students.

89b8f4a7429270308ac301bf7605f974?s=128

Ólafur Waage

January 23, 2019
Tweet

Transcript

  1. The Basics of Object Oriented Programming

  2. Who the hack are you?

  3. Who the hack are you? Ólafur Waage

  4. Who the hack are you? Ólafur Waage Icelandic

  5. Who the hack are you? Ólafur Waage Icelandic Programmer at

    Ubisoft Massive
  6. Who the hack are you? Ólafur Waage Icelandic Programmer at

    Ubisoft Massive Uplay PC Client and Services
  7. Who the hack are you? Ólafur Waage Icelandic Programmer at

    Ubisoft Massive Uplay PC Client and Services @olafurw - Please yell at me there!
  8. Uplay?

  9. None
  10. None
  11. None
  12. Uplay

  13. Uplay Services and Client Core in C++

  14. Uplay Services and Client Core in C++ Client Interface in

    CEF (Chromium Embedded Framework)
  15. Uplay Services and Client Core in C++ Client Interface in

    CEF (Chromium Embedded Framework) Interface in HTML/CSS/JS (Presentation Logic Only)
  16. Uplay Services and Client Core in C++ Client Interface in

    CEF (Chromium Embedded Framework) Interface in HTML/CSS/JS (Presentation Logic Only) Backend Portal to manage all this
  17. Uplay Multithread all the things. Blocking operations are a no

    no!
  18. Uplay Multithread all the things. Blocking operations are a no

    no! Scaling is very hard. Especially in a system like this.
  19. Uplay Multithread all the things. Blocking operations are a no

    no! Scaling is very hard. Especially in a system like this. Almost no comments in the code. If it’s not readable as code you redo it.
  20. Uplay Multithread all the things. Blocking operations are a no

    no! Scaling is very hard. Especially in a system like this. Almost no comments in the code. If it’s not readable as code you redo it. Very pragmatic programming style. At this scale you don’t follow the rules.
  21. Uplay Multithread all the things. Blocking operations are a no

    no! Scaling is very hard. Especially in a system like this. Almost no comments in the code. If it’s not readable as code you redo it. Very pragmatic programming style. At this scale you don’t follow the rules. There is no book called “How to deal with all the users in the world at the same time”
  22. None
  23. Object Oriented Programming

  24. Object Oriented Programming Don’t*

  25. Object Oriented Programming Don’t* *It’s a tool like anything else.

    Use it when applicable. If it solves your problems. Good. Otherwise you should question it!
  26. Let’s go back in time

  27. Let’s go back in time In the old old time

    of far far away, the idea of software was much simpler.
  28. Let’s go back in time In the old old time

    of far far away, the idea of software was much simpler. You had data.
  29. Let’s go back in time In the old old time

    of far far away, the idea of software was much simpler. You had data. You had functions.
  30. Let’s go back in time In the old old time

    of far far away, the idea of software was much simpler. You had data. You had functions. The functions modified the data and returned new data.
  31. Let’s go back in time In the old old time

    of far far away, the idea of software was much simpler. You had data. You had functions. The functions modified the data and returned new data. End of story.
  32. This is fine This style is called functional programming, procedural

    programming or structured programming (depending how you look at it).
  33. This is fine This style is called functional programming, procedural

    programming or structured programming (depending how you look at it). But as software complexity grows, this style of programming can grow out of hand, very fast.
  34. This is fine This style is called functional programming, procedural

    programming or structured programming (depending how you look at it). But as software complexity grows, this style of programming can grow out of hand, very fast. There is no sense of structure (hah) and over time it gets difficult to get work done.
  35. History Even though some of the concepts of OOP date

    back to the 50s, it’s not until around 1965 when the a lot of the common concepts of OOP appear in the language Simula.
  36. History Even though some of the concepts of OOP date

    back to the 50s, it’s not until around 1965 when the a lot of the common concepts of OOP appear in the language Simula. These concepts influenced some languages but it wasn’t until the mid 1980 when language designers started to take notice. C++, Objective- and Eiffel were one of the first ones.
  37. History Even though some of the concepts of OOP date

    back to the 50s, it’s not until around 1965 when the a lot of the common concepts of OOP appear in the language Simula. These concepts influenced some languages but it wasn’t until the mid 1980 when language designers started to take notice. C++, Objective- and Eiffel were one of the first ones. And in the 90s OOP became the tour de force in languages like Java.
  38. Enough history old man! So what is OOP? What even

    is the point?
  39. Enough history old man! So what is OOP? What even

    is the point? OOP is usually defined by three concepts.
  40. Enough history old man! So what is OOP? What even

    is the point? OOP is usually defined by three concepts. Which I will try to explain, and then show with examples how they benefit you.
  41. Enough history old man! So what is OOP? What even

    is the point? OOP is usually defined by three concepts. Which I will try to explain, and then show with examples how they benefit you. But first. Let’s talk about OOP generally.
  42. Object Oriented Programming The classic example of OOP is to

    imagine an Animal.
  43. Animal

  44. Animal Dog

  45. Animal Dog Cat

  46. Animal Dog Cat Bark()

  47. Animal Dog Cat Bark() Meow()

  48. Animal Dog Cat Bark() Meow() English Foxhound

  49. Animal Dog Cat Bark() Meow() English Foxhound Hunt()

  50. Animal Dog Cat Bark() Meow() English Foxhound Hunt() Eat()

  51. Let’s see this in C++ Objects in C++ are defined

    by classes (or structures).
  52. Let’s see this in C++ Objects in C++ are defined

    by classes (or structures). Classes are ways to group together functions and variables.
  53. Let’s see this in C++ Objects in C++ are defined

    by classes (or structures). Classes are ways to group together functions and variables. To create an object, we create a variable that is of the Class’s type.
  54. Let’s see this in C++ Objects in C++ are defined

    by classes (or structures). Classes are ways to group together functions and variables. To create an object, we create a variable that is of the Class’s type. But even with this simple idea, we gain a lot of power.
  55. None
  56. None
  57. None
  58. None
  59. None
  60. Constructor A constructor is a function that is run only

    once, at the initialization of an object.
  61. Constructor A constructor is a function that is run only

    once, at the initialization of an object. An object is fully formed when a constructor has finished running.
  62. Constructor A constructor is a function that is run only

    once, at the initialization of an object. An object is fully formed when a constructor has finished running. They have no name and cannot be called directly.
  63. None
  64. None
  65. Destructor “A destructor is a special member function that is

    called when the lifetime of an object ends.”
  66. Destructor “A destructor is a special member function that is

    called when the lifetime of an object ends.” “The purpose of the destructor is to free the resources that the object may have acquired during its lifetime.”
  67. None
  68. Single responsibility principle “A class should have only a single

    responsibility” or “A class should have only one reason to change”
  69. Single responsibility principle “A class should have only a single

    responsibility” or “A class should have only one reason to change” Animal shouldn’t be responsible for Eat(); and GrassGrow(); that is just going to make it increasingly harder to use and maintain.
  70. Concept #1

  71. Inheritance Is the idea where a class inherits properties and

    variables from a parent class.
  72. Inheritance Is the idea where a class inherits properties and

    variables from a parent class. The child class is considered a subtype of the parent class. This is a way to extend the functionality of the parent class and creates a certain kind of hierarchy.
  73. None
  74. None
  75. None
  76. None
  77. None
  78. None
  79. None
  80. None
  81. None
  82. Liskov substitution principle “If S is a subtype of T,

    then objects of type T may be replaced with objects of type S”
  83. Liskov substitution principle “If S is a subtype of T,

    then objects of type T may be replaced with objects of type S” In our case. If you need an Animal, you can use a Cat or a FoxHound, they both 100% work as Animals.
  84. Liskov substitution principle “If S is a subtype of T,

    then objects of type T may be replaced with objects of type S” In our case. If you need an Animal, you can use a Cat or a FoxHound, they both 100% work as Animals. But if you need a Cat, you can’t use a FoxHound.
  85. Now we need to make a zoo We want to

    have a collection of Animals, and all of them need to eat.
  86. Now we need to make a zoo We want to

    have a collection of Animals, and all of them need to eat. std::vector<Animal> sounds like a perfect fit for that job!
  87. None
  88. None
  89. None
  90. None
  91. None
  92. None
  93. None
  94. None
  95. Dog is an Animal. Animal is not a Dog When

    initializing the class Animal, the compiler will allocate memory for an Animal. In our case, it will allocate for the integer stored within animal.
  96. Dog is an Animal. Animal is not a Dog When

    initializing the class Animal, the compiler will allocate memory for an Animal. In our case, it will allocate for the integer stored within animal. When trying to store a Dog in the memory space allocated for Animal, then there isn’t enough space for barkCount.
  97. Dog is an Animal. Animal is not a Dog When

    initializing the class Animal, the compiler will allocate memory for an Animal. In our case, it will allocate for the integer stored within animal. When trying to store a Dog in the memory space allocated for Animal, then there isn’t enough space for barkCount. This is called Slicing
  98. Dog is an Animal. Animal is not a Dog When

    initializing the class Animal, the compiler will allocate memory for an Animal. In our case, it will allocate for the integer stored within animal. When trying to store a Dog in the memory space allocated for Animal, then there isn’t enough space for barkCount. This is called Slicing The vector is storing memory for Animal. Nothing more.
  99. ... ...

  100. ... ...

  101. ... ... 0

  102. ... ... 1

  103. ... ... 1 0

  104. ... ... 0

  105. ... ... 0 0

  106. ... ... 0 0 0

  107. Pointers to the rescue If you would store in the

    vector a pointer to Animal (either raw/unique/shared), then the actual memory stored within the vector is a pointer. A pointer can’t slice because they are always of the same size.
  108. Pointers to the rescue If you would store in the

    vector a pointer to Animal (either raw/unique/shared), then the actual memory stored within the vector is a pointer. A pointer can’t slice because they are always of the same size. If you then allocate memory on the heap for a Dog but store it in a Animal pointer, no slicing will occur. Because you explicitly allocated memory for a Dog on the heap but point to it.
  109. None
  110. ... ... ... ...

  111. ... ... 0x0A ... ... 0

  112. ... ... 0x0A 0x5F ... ... 0 0 0

  113. But dogs eat in a special way Yes yes dogs

    eat. But they eat so much, and they slobber all over.
  114. But dogs eat in a special way Yes yes dogs

    eat. But they eat so much, and they slobber all over. I like how all Animals have an Eat(); function and it’s useful to have one function you can call. But I need a way to change how Eat(); is implemented, but only for Dogs.
  115. None
  116. None
  117. Open–closed principle "Software entities (classes, modules, functions, etc.) should be

    open for extension, but closed for modification"
  118. Open–closed principle "Software entities (classes, modules, functions, etc.) should be

    open for extension, but closed for modification" Here we have done this in two ways. Once with inheritance and in another way with overriding virtual functions.
  119. Is this magic? Wait wait! How did the for loop

    know which Eat(); function to call?
  120. Is this magic? Wait wait! How did the for loop

    know which Eat(); function to call? The variable in the loop is Animal* but it’s able to call the Dog version of the function?
  121. Is this magic? Wait wait! How did the for loop

    know which Eat(); function to call? The variable in the loop is Animal* but it’s able to call the Dog version of the function? I call heresy!
  122. No, it’s not magic... When you have a basic class,

    with no virtual functions. When that class is allocated on the stack, only the member variables are allocated to that stack.
  123. No, it’s not magic... When you have a basic class,

    with no virtual functions. When that class is allocated on the stack, only the member variables are allocated to that stack. So when someone calls a function, it’s almost like someone is calling a freestanding function.
  124. Virtual Virtuality But when you have at least one virtual

    function in your class a different thing happens.
  125. Virtual Virtuality But when you have at least one virtual

    function in your class a different thing happens. A pointer to a virtual function table is allocated. This pointer is used when calling virtual functions.
  126. Virtual Virtuality But when you have at least one virtual

    function in your class a different thing happens. A pointer to a virtual function table is allocated. This pointer is used when calling virtual functions. When a class of type Animal is created, the virtual function entry for Eat(); points to the Animal version of Eat. And when you allocate for Dog, it points to the Dog implementation of Eat
  127. Concept #2

  128. Polymorphism Is the idea where the same interface can be

    used but against many different objects.
  129. Polymorphism Is the idea where the same interface can be

    used but against many different objects. In the example we just did, we were able to use the interface of Animal but the underlying object could be a Dog without us ever knowing it.
  130. Abstract Classes / Interfaces So thinking about our class Animal

    against the real world.
  131. Abstract Classes / Interfaces So thinking about our class Animal

    against the real world. It actually doesn’t make sense to create an animal by itself. It always has to be a certain kind of animal.
  132. Abstract Classes / Interfaces So thinking about our class Animal

    against the real world. It actually doesn’t make sense to create an animal by itself. It always has to be a certain kind of animal. This can be solved by creating an Abstract Class or what is often called an Interface.
  133. None
  134. Abstract Classes / Interfaces When we do this, it will

    not work to create a variable of the type Animal.
  135. Abstract Classes / Interfaces When we do this, it will

    not work to create a variable of the type Animal. This is because there has been no implementation of the function Eat(); and any derived class has to implement that function if they want to be usable.
  136. None
  137. None
  138. Don’t play with my toys! Now we have another problem.

  139. Don’t play with my toys! Now we have another problem.

    In the last example the user was able to refer to the numberOfMeals variable directly. He could even change it if he wanted.
  140. Don’t play with my toys! Now we have another problem.

    In the last example the user was able to refer to the numberOfMeals variable directly. He could even change it if he wanted. If that is our design, then fine. But this can cause someone to write code that changes the state of our class to something invalid.
  141. The Car Example Let’s create a hypothetical class called Car.

  142. None
  143. None
  144. None
  145. None
  146. None
  147. None
  148. None
  149. None
  150. None
  151. None
  152. C++ Access Specifiers C++ implements three types of Access Specifiers.

    They apply to functions and variables (any member of the class).
  153. C++ Access Specifiers C++ implements three types of Access Specifiers.

    They apply to functions and variables (any member of the class). public: Anyone can access the member. Same as we had before.
  154. C++ Access Specifiers C++ implements three types of Access Specifiers.

    They apply to functions and variables (any member of the class). public: Anyone can access the member. Same as we had before. protected: Only accessible inside of Base and Derived classes. Not publically.
  155. C++ Access Specifiers C++ implements three types of Access Specifiers.

    They apply to functions and variables (any member of the class). public: Anyone can access the member. Same as we had before. protected: Only accessible inside of Base and Derived classes. Not publically. private: Only accessible inside of Base. Not derived classes and not publically.
  156. None
  157. Clean Interfaces This allows you to design well made interfaces

    towards classes that are then enjoyable to use correctly and hard to use incorrectly.
  158. Clean Interfaces This allows you to design well made interfaces

    towards classes that are then enjoyable to use correctly and hard to use incorrectly. Also. The only difference between struct and class is that a struct is by default public and a class is by default private. There is no other difference between them.
  159. Concept #3

  160. Encapsulation “A language mechanism for restricting direct access to some

    of the object's components.”
  161. Encapsulation “A language mechanism for restricting direct access to some

    of the object's components.” Encapsulation is often called an “information hiding mechanism” where the user of the system is not aware of the underlying complexity but can still use the system.
  162. Those are the three concepts Inheritance: “A class inherits properties

    and variables from a parent class.”
  163. Those are the three concepts Inheritance: “A class inherits properties

    and variables from a parent class.” Polymorphism: “A common interface can be used against many different underlying objects.”
  164. Those are the three concepts Inheritance: “A class inherits properties

    and variables from a parent class.” Polymorphism: “A common interface can be used against many different underlying objects.” Encapsulation: “A language mechanism for restricting direct access to some of the object's components.”
  165. This is just the beginning I have only scratched the

    surface of OOP.
  166. This is just the beginning I have only scratched the

    surface of OOP. There are countless design patterns that use the flexibility of OOP to solve commonly encountered problems.
  167. This is just the beginning I have only scratched the

    surface of OOP. There are countless design patterns that use the flexibility of OOP to solve commonly encountered problems. Aspects like Inheritance vs Composition are important as well.
  168. This is just the beginning I have only scratched the

    surface of OOP. There are countless design patterns that use the flexibility of OOP to solve commonly encountered problems. Aspects like Inheritance vs Composition are important as well. But let’s go back to an earlier slide I had.
  169. Object Oriented Programming Don’t* *It’s a tool like anything else.

    Use it when applicable. If it solves your problems. Good. Otherwise you should question it!
  170. Let’s make a game! What can we model as a

    class? Well a player sounds like a fine abstraction.
  171. Let’s make a game! What can we model as a

    class? Well a player sounds like a fine abstraction. What does a player have?
  172. Let’s make a game! What can we model as a

    class? Well a player sounds like a fine abstraction. What does a player have? Location, Health, Weapon, Inventory, Name.
  173. Let’s make a game! What can we model as a

    class? Well a player sounds like a fine abstraction. What does a player have? Location, Health, Weapon, Inventory, Name. Great! Let’s model this.
  174. None
  175. None
  176. None
  177. None
  178. None
  179. None
  180. None
  181. None
  182. None
  183. None
  184. None
  185. 20 myX

  186. 20 12 myX myY

  187. 20 12 100 myX myY myHealth

  188. 20 12 100 2 8 myX myY myHealth myWeapon {

  189. 20 12 100 2 8 0xAB 10 myX myY myHealth

    myWeapon myName { {
  190. 20 12 100 2 8 0xAB 10 myX myY myHealth

    myWeapon myName { { 31 5 77 1 0 0xC0 9 Player element 0 Player element 1
  191. Locality of Reference “Locality of reference is a term for

    the phenomenon in which the same values, or related storage locations, are frequently accessed, depending on the memory access pattern.”
  192. Locality of Reference “Locality of reference is a term for

    the phenomenon in which the same values, or related storage locations, are frequently accessed, depending on the memory access pattern.” This means that when you load that first myX variable into memory, that is not the only thing loaded into memory, because the computer is guessing that you also need the values that are stored after the value. So it loads a lot of the memory that comes after that variable.
  193. None
  194. Cache miss “A cache miss is a failed attempt to

    read or write a piece of data in the cache, which results in a main memory access with much longer latency”
  195. Cache miss “A cache miss is a failed attempt to

    read or write a piece of data in the cache, which results in a main memory access with much longer latency” If all of the data you need is tightly packed together, it’s much more likely that you will not need to go into main memory to access it, which will drastically speed up your program.
  196. Cache miss “A cache miss is a failed attempt to

    read or write a piece of data in the cache, which results in a main memory access with much longer latency” If all of the data you need is tightly packed together, it’s much more likely that you will not need to go into main memory to access it, which will drastically speed up your program. But. As with anything. Measure first!
  197. Object Oriented Programming Is very useful. The ability to model

    real world things or abstract ideas is something we do daily.
  198. Object Oriented Programming Is very useful. The ability to model

    real world things or abstract ideas is something we do daily. But be mindful of how your data is stored and used, because when it comes down to it, those are the bits that matter the most.
  199. None
  200. None
  201. None
  202. None
  203. None
  204. None
  205. None
  206. None
  207. None
  208. None
  209. None
  210. None
  211. None
  212. None
  213. None
  214. None
  215. None
  216. None
  217. None
  218. Thank you! You can follow me @olafurw On Twitter