Slide 1

Slide 1 text

The Basics of Object Oriented Programming

Slide 2

Slide 2 text

Who the hack are you?

Slide 3

Slide 3 text

Who the hack are you? Ólafur Waage

Slide 4

Slide 4 text

Who the hack are you? Ólafur Waage Icelandic

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

Uplay?

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Uplay

Slide 13

Slide 13 text

Uplay Services and Client Core in C++

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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.

Slide 20

Slide 20 text

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.

Slide 21

Slide 21 text

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”

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Object Oriented Programming

Slide 24

Slide 24 text

Object Oriented Programming Don’t*

Slide 25

Slide 25 text

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!

Slide 26

Slide 26 text

Let’s go back in time

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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.

Slide 29

Slide 29 text

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.

Slide 30

Slide 30 text

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.

Slide 31

Slide 31 text

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.

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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.

Slide 34

Slide 34 text

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.

Slide 35

Slide 35 text

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.

Slide 36

Slide 36 text

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.

Slide 37

Slide 37 text

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.

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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.

Slide 41

Slide 41 text

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.

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

Animal

Slide 44

Slide 44 text

Animal Dog

Slide 45

Slide 45 text

Animal Dog Cat

Slide 46

Slide 46 text

Animal Dog Cat Bark()

Slide 47

Slide 47 text

Animal Dog Cat Bark() Meow()

Slide 48

Slide 48 text

Animal Dog Cat Bark() Meow() English Foxhound

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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.

Slide 54

Slide 54 text

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.

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

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.

Slide 62

Slide 62 text

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.

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

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

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

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.

Slide 70

Slide 70 text

Concept #1

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

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.

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

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

Slide 83

Slide 83 text

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.

Slide 84

Slide 84 text

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.

Slide 85

Slide 85 text

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

Slide 86

Slide 86 text

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!

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

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.

Slide 96

Slide 96 text

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.

Slide 97

Slide 97 text

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

Slide 98

Slide 98 text

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.

Slide 99

Slide 99 text

... ...

Slide 100

Slide 100 text

... ...

Slide 101

Slide 101 text

... ... 0

Slide 102

Slide 102 text

... ... 1

Slide 103

Slide 103 text

... ... 1 0

Slide 104

Slide 104 text

... ... 0

Slide 105

Slide 105 text

... ... 0 0

Slide 106

Slide 106 text

... ... 0 0 0

Slide 107

Slide 107 text

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.

Slide 108

Slide 108 text

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.

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

... ... ... ...

Slide 111

Slide 111 text

... ... 0x0A ... ... 0

Slide 112

Slide 112 text

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

Slide 113

Slide 113 text

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

Slide 114

Slide 114 text

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.

Slide 115

Slide 115 text

No content

Slide 116

Slide 116 text

No content

Slide 117

Slide 117 text

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

Slide 118

Slide 118 text

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.

Slide 119

Slide 119 text

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

Slide 120

Slide 120 text

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?

Slide 121

Slide 121 text

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!

Slide 122

Slide 122 text

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.

Slide 123

Slide 123 text

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.

Slide 124

Slide 124 text

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

Slide 125

Slide 125 text

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.

Slide 126

Slide 126 text

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

Slide 127

Slide 127 text

Concept #2

Slide 128

Slide 128 text

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

Slide 129

Slide 129 text

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.

Slide 130

Slide 130 text

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

Slide 131

Slide 131 text

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.

Slide 132

Slide 132 text

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.

Slide 133

Slide 133 text

No content

Slide 134

Slide 134 text

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

Slide 135

Slide 135 text

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.

Slide 136

Slide 136 text

No content

Slide 137

Slide 137 text

No content

Slide 138

Slide 138 text

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

Slide 139

Slide 139 text

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.

Slide 140

Slide 140 text

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.

Slide 141

Slide 141 text

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

Slide 142

Slide 142 text

No content

Slide 143

Slide 143 text

No content

Slide 144

Slide 144 text

No content

Slide 145

Slide 145 text

No content

Slide 146

Slide 146 text

No content

Slide 147

Slide 147 text

No content

Slide 148

Slide 148 text

No content

Slide 149

Slide 149 text

No content

Slide 150

Slide 150 text

No content

Slide 151

Slide 151 text

No content

Slide 152

Slide 152 text

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

Slide 153

Slide 153 text

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.

Slide 154

Slide 154 text

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.

Slide 155

Slide 155 text

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.

Slide 156

Slide 156 text

No content

Slide 157

Slide 157 text

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

Slide 158

Slide 158 text

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.

Slide 159

Slide 159 text

Concept #3

Slide 160

Slide 160 text

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

Slide 161

Slide 161 text

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.

Slide 162

Slide 162 text

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

Slide 163

Slide 163 text

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

Slide 164

Slide 164 text

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

Slide 165

Slide 165 text

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

Slide 166

Slide 166 text

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.

Slide 167

Slide 167 text

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.

Slide 168

Slide 168 text

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.

Slide 169

Slide 169 text

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!

Slide 170

Slide 170 text

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

Slide 171

Slide 171 text

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?

Slide 172

Slide 172 text

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.

Slide 173

Slide 173 text

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.

Slide 174

Slide 174 text

No content

Slide 175

Slide 175 text

No content

Slide 176

Slide 176 text

No content

Slide 177

Slide 177 text

No content

Slide 178

Slide 178 text

No content

Slide 179

Slide 179 text

No content

Slide 180

Slide 180 text

No content

Slide 181

Slide 181 text

No content

Slide 182

Slide 182 text

No content

Slide 183

Slide 183 text

No content

Slide 184

Slide 184 text

No content

Slide 185

Slide 185 text

20 myX

Slide 186

Slide 186 text

20 12 myX myY

Slide 187

Slide 187 text

20 12 100 myX myY myHealth

Slide 188

Slide 188 text

20 12 100 2 8 myX myY myHealth myWeapon {

Slide 189

Slide 189 text

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

Slide 190

Slide 190 text

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

Slide 191

Slide 191 text

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

Slide 192

Slide 192 text

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.

Slide 193

Slide 193 text

No content

Slide 194

Slide 194 text

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”

Slide 195

Slide 195 text

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.

Slide 196

Slide 196 text

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!

Slide 197

Slide 197 text

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

Slide 198

Slide 198 text

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.

Slide 199

Slide 199 text

No content

Slide 200

Slide 200 text

No content

Slide 201

Slide 201 text

No content

Slide 202

Slide 202 text

No content

Slide 203

Slide 203 text

No content

Slide 204

Slide 204 text

No content

Slide 205

Slide 205 text

No content

Slide 206

Slide 206 text

No content

Slide 207

Slide 207 text

No content

Slide 208

Slide 208 text

No content

Slide 209

Slide 209 text

No content

Slide 210

Slide 210 text

No content

Slide 211

Slide 211 text

No content

Slide 212

Slide 212 text

No content

Slide 213

Slide 213 text

No content

Slide 214

Slide 214 text

No content

Slide 215

Slide 215 text

No content

Slide 216

Slide 216 text

No content

Slide 217

Slide 217 text

No content

Slide 218

Slide 218 text

Thank you! You can follow me @olafurw On Twitter