Hey, what's up everybody Marc Price here at Devslopes.com and today we are going to talk about classes and object-oriented programming. You may have heard these phrases before. Now, let's see how they actually work in real life. So we're going to create a new playground. My playground we're going to call this one Objects and Classes. Let's put this over here an iOS 10 course in the swift section and create. Okay. Let's make this a little larger for you here. So what is a Class? Okay. So we've been working with Classes and Object oriented programming I think sometimes since the 70s, and before that it was functional programming. Functional programming still exists today, but object-oriented programming is probably the most popular form of programming currently. There's some new paradigms coming out that are trying to move away from it slightly like protocol oriented programming which you'll also learn a little bit about later on, but object-oriented programming is the idea where you can create like a blueprint for something and create multiple copies of it. Perfect example is let's say a car in a car factory, right? You can create a blueprint for a car and then you'd create multiple copies of that car. So let's go ahead and use that example right here. We want to create a car, so you create a class by defining the type, the keyword class and then the name of the class. So we're going to say car or better yet vehicle. Okay. A vehicle has multiple properties and functions such as we say, var a vehicle has a certain amount of tires. Okay. I'm going to say equals zero or four, if it's a vehicle with four tires, that could be a default value for instance. A vehicle can have lights like headlight, right? So how many headlights does a car have? What is the horsepower of a car or a vehicle? 468 horsepower. So we got horsepower on a vehicle and a car can do things, you can do things such as drives. So we can create a function called drive. There's some type of algorithm that will accelerate the vehicle. Also a vehicle can drive, a vehicle can break. Okay. So a class has properties such as tires, headlights and horsepower and also can have functions such as drive and break. So what you want to do to understand object-oriented programming is really visualize the world around you. Some people are really good at this, like if you're an artist or you come from the design background, object-oriented programming tends to clean to you because you can do things like objects, like people. Sometimes, if you're more on the other side of the brain, it's harder to visualize, maybe your thinking numbers and things like that. Whereas, this is very much a visual type of way of laying out your code. So when we're coding for instance, let's say we're making an app like Instagram. Instagram has users who can like other photos. So you might have a user object where it has a user ID and account username and password, as properties and it maybe this user has functions such as reset password or delete account, things like that. Then even the pictures could be an object, a photo is an object, so class photo and it has the selected filter. It has how many likes it has, how many comments it has and those could be properties of it. You can add a function of a photo such as add like or a move like action items on it. So you can start looking at objects in everything that's around you from the real world like others a buccal outside. What does it have or there's a house it has a door, it has windows, those are the properties. What can a house do? Well, house can cool down from the AC unit so we can call cool down house function or we can call it heat up house function. Everything around you if you put it into some type of object can do something and also has properties of some kind or things that describe it. So you're going to see a lot of objects in programming or a lot of classes and this is exactly how it works, using the class keyword, and then giving it some type of name, giving it properties and then functionality of some kind. Some of the classes that you'll see as you move along in programming and some that you've already worked with are things like this ViewController. So I could say class new VC, VC for ViewController, and it has inherited from UIViewController. Okay. You've seen things like that and then you've seen the view did load. You seen those functions there like that. We've just created a class and we've inherited from another class. Okay. Really cool stuff. So to create an instance of your class, so what we've done here, is we've defined our class but if we want to create an instance of it. Okay. Let's give this vehicle a model. Okay. So Var model. Okay. It'll just be a new model to begin with. So what I can do is I can say let's call this BMW equals and then we'll create a new vehicle like so, and then we're going to say bmw.model equals say 328i. So you can set the properties of this class like so but what's really cool is we can create as many as we want. So I can say let, and let's call this a Ford equals vehicle. We're going to say, Ford.model equals F150. You see what's happening here? We created code one time but we can start creating multiple instances of that code and then setting the properties of it as well as calling the function such as I can say ford.break and I've now called a function of this class. I've told it to do something, and so properties functions on a class thinking it like a blueprint. When you're designing your apps, when you're designing anything, think in your mind, ''Can I give these properties or attributes that describe it?'' Then think in your mind, ''Can it do anything?'' In most cases it's going to be, yes. Like if you had a bank account balance, it's going to have your balance, is going to have your first name, your last name or your bank account. Then it's going to have a function that says, withdrawal funds or add funds. There's going to be different things that can happen to or that it can do in your class or in a bank account class for instance. So classes, they have properties, they have functions, things that can do stuff, and then they have the ability to be instantiated. Okay. At this point in time I am instantiating, that's a keyword, instantiating an instance of the vehicle class. I'm instantiating another instance of the vehicle class, these are also called objects. Once you've instantiated them there called an objects. You would say, I have a Ford object or a BMW object or I'm creating an object or using an object. Okay. At this point in time it's not an object. It's just a class or a blueprint, but once it comes out of the factory, when you instantiate it, it becomes an object. Another thing to know is that objects are passed by reference, not by value. So let me show you here, if I create a function and some function doesn't need to have a cool name, and let's say we want to pass a vehicle in here, like so. I've type vehicle. If I say, vehicle.model, no that's not what I told you to do. I said lowercase, I did not type an uppercase, is really getting me here. Vehicle.model equals cheese, something strange, right? Get this. So let's say we have the Ford model here, and then I call some function, and we know it says Ford F150 because I put that in the model. So let's say, let's print the ford.model, so we're going to print it right here. I'll put on the right-hand side that currently says, F150, but get this. Then I'm going to call some function and pass in ford. Then I'm going to print ford.model, and look what happens, it's now cheese. Maybe, you're thinking to yourself, ''What just happened, how is this being modified? This is supposed to be a constant. You're not supposed to be able to modify this, what's going on?'' Well, they're passed by reference. An object has a reference in memory, you can't copy an object, you cannot copy an object. You can copy values like integers and doubles but you cannot copy an object. So these are passed by reference, a very important distinction. This gets a lot of bugs in new developers apps pass by reference. Let me write this down right here, pass by reference. Okay. They are reference objects, and if you're still confused, let's do the same example again. Let's say some function or actually let's just call this first one pass. We're passing in an object by reference. So that's the name of this function now. Let's create a new function here called pass by value. All right. This takes a double or let's say an integer. It just takes an integer. Let's say, age. Type int, and let's say we say age equals 10. All right, and I forgot the func keyword. Okay. So here we've modified our value, nothing was copied over. So what we're going to do now? Well, first off look at this. You can't even change the value because it's a constant. All right. That's already we can't do anything with it. So if I was to say, var someone's age equals 20, and then down here I was a call pass by value and I pass in someone's age, it won't even work because these are constants. So my point is when I pass this in here like this, it's being copied in here like so, and then I can use the value here. So I could say, let new age equals age. This one will never be modified. Never ever be modified. So important distinction passed by reference, you can modify the object, it points to a specific spot in memory, value types like the string or the double or the integer or float or CGFloat, things like that are all passed by value or rather they're copied over. One is copied, one is not, very important. So this is the basics of classes and object-oriented programming. You can create a class that has properties that describe it and then it has actions that it can take or things that can act upon that object. Okay. It's going to take you a while to learn but as you're doing this course and doing other things with programming it'll all come together overtime. But that's it for now markprice@devslopes.com. See you soon.