Hey What's up everybody.
Mark Price here at the slopes dot com.
And today we are going to talk about classes and object oriented programming.
And you may have heard these phrases before and analyse 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 in Iowa.
Ten course in the swift section and create OK.
It makes it a little larger for you here.
So what is a class.
OK.
So we've been working with classes and object oriented programming.
I think some time 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.
And there are some new paradigms coming out that are trying to move away from it slightly like particle
oriented programming which you also learn a little bit about of later on but object 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 and a car factory right.
You can create a blueprint for a car and then you create multiple copies of that car.
And so let's go ahead and use that example right here.
We want to create a car so you create a class.
OK.
By defining the word type the key word class and then the name of the class organise say car or better
yet view acle OK and a vehicle has multiple properties and functions such as we say var vehicle has
a certain amount of tires.
OK it's equal 0 or 4.
If it's a if it's a vehicle with four tires that can be a default value for instance a vehicle can have
lights like headlights right.
So how many headlights as a car have.
You know what is the horsepower of a car you know or an buccal 468 horsepower horse horsepower on a
vehicle and a car can do things right you can do things such as drivers can create a function called
Drive and it will you know there's some type of algorithm that will accelerate accelerate the UKL you
know.
So a vehicle can drive a vehicle can break.
OK.
So a class has properties such as tires headlights and horsepower and also can have functions such as
drive and brake.
And 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 where you come from the design background
object oriented programming tends to this 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 you're maybe you think
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 an account username and password as properties
and maybe has this user has functions such as you know reset password or delete account things like
that.
And 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 know a photo you can at a function of a photo such as ADD like
OK or remove like action items on it so you can start looking at objects and everything that's around
you from the real world.
Like others go outside.
What does it have.
Or there's a house.
It has a door has windows.
Those are the properties.
What can a house do a house can cool down from the AC unit.
So we can cool cool down house function or we can call 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 time or things
that describe it.
And 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 cured 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 as you move along and programming and some they've already
worked with are you know things like this like view controller so I could say class you know new VC
VC for view controller and it has inherited from you view controller OK.
You've seen things like that and you've seen the view did load.
You've seen those functions there like that we've just created a class and we've inherited from another
class.
OK.
Really cool stuff.
And so to create an instance of your class or what we've done here is we've defined our class.
But if we want to create an instance of it.
OK and let's give this vehicle a model case of our model so just be a no model to begin with.
So what I can do is I can say let me say Let's call this BMW equals and then we'll create a new vehicle
like so.
And then we a BMW datt model equals the say 3:28.
OK.
And 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 Well let's call this a Ford
equals vehicle and we're going to say Ford model equals F 150.
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 for break and I've now called
a function of this class.
I'm told to do something.
And so propertys functions on a class.
Think of it like a blueprint.
When you're designing your apps you're designing anything.
Think in your mind can I give this properties or attributes that describe it and then think in your
mind can it do anything.
In most cases it's going to be yes.
OK.
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 and then it's going to have a function that says withdraw 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.
OK.
At this point in time I'm instantiating that's the key word instantiating an instance of the vehicle
class I am instantiating another instance of the vehicle class.
These are also called objects.
Once you've instantiated them they're called an object so you would say I have a foreign object or a
BMW object or I'm creating an object or using an object.
OK.
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 OK when you instantiate it it becomes an object.
OK.
Another day another thing to know is that objects are passed by reference not by value.
OK so when you let me show you here if I create a function and some function doesn't need to have an
cool name and let's say we want to pass a viewpoint here like so of type buco.
OK if I say View Hinkel the model no.
Now what I told you to do I said lowercase I did not type in uppercase.
It was really good me here.
OK.
View model equals cheese.
OK.
Something strange right.
Get this.
So let's say we have the Ford Model here right.
And then I call some function and we know it's a Ford F-150 because I put that in the model.
So let's say let's print the Ford dot model so we're going to print it right here.
I'll put it on the right hand side that currently says F-150 150 but get this then I'm going to call
some function and pass in Ford and then I'm going to print Ford model and look what happens.
It's now cheese.
Maybe you're thinking to yourself wait 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 is what's going on.
Well they're pass by reference OK an object has a reference in memory.
You can't copy an object you cannot cannot copy an object.
OK 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 and new developers apps pass by reference.
You write this down right here pass by reference K or there are reference objects and if you're still
confused let's do the same example again.
Let's say some function are actually just this first one pass.
We're passing an object by reference.
So that's the name of this function now.
OK let's get a new function here called pass by value.
All right.
And this takes a double or let's say an integer is an integer let's say age K type int and let's say
we say age equals 10.
All right.
And I forgot the phunk cured.
OK.
So here we've modified our value.
OK.
Nothing was copied over.
So we're going to do now.
Well first off get this.
You can even change the value in because it's a constant.
All right so that's that's already we can't do anything with that.
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 passed
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.
OK.
It's being copied in here like so.
And then I can use the value here so I could say you know let new age equals H and this one will never
be modified.
OK.
Never ever ever be modified.
So important distinction.
Pass by reference you can modify the object it points to a specific spot of memory value types like
like the string or the double or the integer or float or Suji float things like that are all passed
by value.
Or rather they're copied over.
OK one is copied one is not very important so this is the basics of classes and object oriented programming.
You can create a class has properties that describe it and then it has actions that it can take or things
that can act upon that object.
It's going to take you some while awhile to learn but as you do in this course and doing other things
with programming it will all come together over time.
But that's it for now.
Mark Price of slopes dot com.
See you soon.