Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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. Who the hack are you? Ólafur Waage Icelandic Programmer at

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

    Ubisoft Massive Uplay PC Client and Services @olafurw - Please yell at me there!
  3. Uplay Services and Client Core in C++ Client Interface in

    CEF (Chromium Embedded Framework) Interface in HTML/CSS/JS (Presentation Logic Only)
  4. 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
  5. Uplay Multithread all the things. Blocking operations are a no

    no! Scaling is very hard. Especially in a system like this.
  6. 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.
  7. 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.
  8. 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”
  9. 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!
  10. Let’s go back in time In the old old time

    of far far away, the idea of software was much simpler.
  11. 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.
  12. 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.
  13. 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.
  14. 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.
  15. This is fine This style is called functional programming, procedural

    programming or structured programming (depending how you look at it).
  16. 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.
  17. 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.
  18. 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.
  19. 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.
  20. 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.
  21. Enough history old man! So what is OOP? What even

    is the point? OOP is usually defined by three concepts.
  22. 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.
  23. 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.
  24. Let’s see this in C++ Objects in C++ are defined

    by classes (or structures). Classes are ways to group together functions and variables.
  25. 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.
  26. 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.
  27. Constructor A constructor is a function that is run only

    once, at the initialization of an object.
  28. 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.
  29. 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.
  30. Destructor “A destructor is a special member function that is

    called when the lifetime of an object ends.”
  31. 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.”
  32. Single responsibility principle “A class should have only a single

    responsibility” or “A class should have only one reason to change”
  33. 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.
  34. 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.
  35. Liskov substitution principle “If S is a subtype of T,

    then objects of type T may be replaced with objects of type S”
  36. 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.
  37. 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.
  38. Now we need to make a zoo We want to

    have a collection of Animals, and all of them need to eat.
  39. 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!
  40. 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.
  41. 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.
  42. 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
  43. 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.
  44. 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.
  45. 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.
  46. But dogs eat in a special way Yes yes dogs

    eat. But they eat so much, and they slobber all over.
  47. 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.
  48. 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.
  49. Is this magic? Wait wait! How did the for loop

    know which Eat(); function to call?
  50. 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?
  51. 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!
  52. 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.
  53. 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.
  54. Virtual Virtuality But when you have at least one virtual

    function in your class a different thing happens.
  55. 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.
  56. 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
  57. Polymorphism Is the idea where the same interface can be

    used but against many different objects.
  58. 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.
  59. 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.
  60. 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.
  61. Abstract Classes / Interfaces When we do this, it will

    not work to create a variable of the type Animal.
  62. 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.
  63. 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.
  64. 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.
  65. C++ Access Specifiers C++ implements three types of Access Specifiers.

    They apply to functions and variables (any member of the class).
  66. 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.
  67. 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.
  68. 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.
  69. Clean Interfaces This allows you to design well made interfaces

    towards classes that are then enjoyable to use correctly and hard to use incorrectly.
  70. 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.
  71. 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.
  72. 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.”
  73. 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.”
  74. 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.
  75. 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.
  76. 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.
  77. 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!
  78. Let’s make a game! What can we model as a

    class? Well a player sounds like a fine abstraction.
  79. 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?
  80. 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.
  81. 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.
  82. 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
  83. 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.”
  84. 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.
  85. 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”
  86. 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.
  87. 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!
  88. Object Oriented Programming Is very useful. The ability to model

    real world things or abstract ideas is something we do daily.
  89. 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.