$30 off During Our Annual Pro Sale. View Details »

The Basics of Object Oriented Programming

The Basics of Object Oriented Programming

Spoken at The Game Assembly to 1st year students.

Ólafur Waage

January 23, 2019
Tweet

More Decks by Ólafur Waage

Other Decks in Programming

Transcript

  1. The Basics of Object
    Oriented Programming

    View Slide

  2. Who the hack are you?

    View Slide

  3. Who the hack are you?
    Ólafur Waage

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  7. Who the hack are you?
    Ólafur Waage
    Icelandic
    Programmer at Ubisoft Massive
    Uplay PC Client and Services
    @olafurw - Please yell at me there!

    View Slide

  8. Uplay?

    View Slide

  9. View Slide

  10. View Slide

  11. View Slide

  12. Uplay

    View Slide

  13. Uplay
    Services and Client Core in C++

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

  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.

    View Slide

  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.

    View Slide

  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”

    View Slide

  22. View Slide

  23. Object Oriented Programming

    View Slide

  24. Object Oriented Programming
    Don’t*

    View Slide

  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!

    View Slide

  26. Let’s go back in time

    View Slide

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

    View Slide

  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.

    View Slide

  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.

    View Slide

  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.

    View Slide

  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.

    View Slide

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

    View Slide

  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.

    View Slide

  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.

    View Slide

  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.

    View Slide

  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.

    View Slide

  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.

    View Slide

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

    View Slide

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

    View Slide

  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.

    View Slide

  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.

    View Slide

  42. Object Oriented Programming
    The classic example of OOP is to imagine an Animal.

    View Slide

  43. Animal

    View Slide

  44. Animal
    Dog

    View Slide

  45. Animal
    Dog Cat

    View Slide

  46. Animal
    Dog Cat
    Bark()

    View Slide

  47. Animal
    Dog Cat
    Bark() Meow()

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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.

    View Slide

  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.

    View Slide

  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.

    View Slide

  55. View Slide

  56. View Slide

  57. View Slide

  58. View Slide

  59. View Slide

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

    View Slide

  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.

    View Slide

  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.

    View Slide

  63. View Slide

  64. View Slide

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

    View Slide

  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.”

    View Slide

  67. View Slide

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

    View Slide

  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.

    View Slide

  70. Concept #1

    View Slide

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

    View Slide

  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.

    View Slide

  73. View Slide

  74. View Slide

  75. View Slide

  76. View Slide

  77. View Slide

  78. View Slide

  79. View Slide

  80. View Slide

  81. View Slide

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

    View Slide

  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.

    View Slide

  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.

    View Slide

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

    View Slide

  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 sounds like a perfect fit for that job!

    View Slide

  87. View Slide

  88. View Slide

  89. View Slide

  90. View Slide

  91. View Slide

  92. View Slide

  93. View Slide

  94. View Slide

  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.

    View Slide

  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.

    View Slide

  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

    View Slide

  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.

    View Slide

  99. ... ...

    View Slide

  100. ... ...

    View Slide

  101. ... ...
    0

    View Slide

  102. ... ...
    1

    View Slide

  103. ... ...
    1 0

    View Slide

  104. ... ...
    0

    View Slide

  105. ... ...
    0 0

    View Slide

  106. ... ...
    0 0 0

    View Slide

  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.

    View Slide

  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.

    View Slide

  109. View Slide

  110. ... ...
    ... ...

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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.

    View Slide

  115. View Slide

  116. View Slide

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

    View Slide

  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.

    View Slide

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

    View Slide

  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?

    View Slide

  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!

    View Slide

  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.

    View Slide

  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.

    View Slide

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

    View Slide

  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.

    View Slide

  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

    View Slide

  127. Concept #2

    View Slide

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

    View Slide

  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.

    View Slide

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

    View Slide

  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.

    View Slide

  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.

    View Slide

  133. View Slide

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

    View Slide

  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.

    View Slide

  136. View Slide

  137. View Slide

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

    View Slide

  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.

    View Slide

  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.

    View Slide

  141. The Car Example
    Let’s create a hypothetical class called Car.

    View Slide

  142. View Slide

  143. View Slide

  144. View Slide

  145. View Slide

  146. View Slide

  147. View Slide

  148. View Slide

  149. View Slide

  150. View Slide

  151. View Slide

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

    View Slide

  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.

    View Slide

  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.

    View Slide

  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.

    View Slide

  156. View Slide

  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.

    View Slide

  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.

    View Slide

  159. Concept #3

    View Slide

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

    View Slide

  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.

    View Slide

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

    View Slide

  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.”

    View Slide

  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.”

    View Slide

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

    View Slide

  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.

    View Slide

  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.

    View Slide

  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.

    View Slide

  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!

    View Slide

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

    View Slide

  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?

    View Slide

  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.

    View Slide

  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.

    View Slide

  174. View Slide

  175. View Slide

  176. View Slide

  177. View Slide

  178. View Slide

  179. View Slide

  180. View Slide

  181. View Slide

  182. View Slide

  183. View Slide

  184. View Slide

  185. 20
    myX

    View Slide

  186. 20 12
    myX myY

    View Slide

  187. 20 12 100
    myX myY myHealth

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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.”

    View Slide

  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.

    View Slide

  193. View Slide

  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”

    View Slide

  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.

    View Slide

  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!

    View Slide

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

    View Slide

  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.

    View Slide

  199. View Slide

  200. View Slide

  201. View Slide

  202. View Slide

  203. View Slide

  204. View Slide

  205. View Slide

  206. View Slide

  207. View Slide

  208. View Slide

  209. View Slide

  210. View Slide

  211. View Slide

  212. View Slide

  213. View Slide

  214. View Slide

  215. View Slide

  216. View Slide

  217. View Slide

  218. Thank you!
    You can follow me
    @olafurw
    On Twitter

    View Slide