1
00:00:01,710 --> 00:00:02,670
Hey What's up everybody.

2
00:00:02,670 --> 00:00:04,740
Mark Price here at the slopes dot com.

3
00:00:04,770 --> 00:00:09,470
And today we are going to talk about classes and object oriented programming.

4
00:00:09,480 --> 00:00:16,370
And you may have heard these phrases before and analyse how they actually work in real life.

5
00:00:16,410 --> 00:00:25,310
So we're going to create a new playground my playground we're going to call this one objects and classes.

6
00:00:27,350 --> 00:00:28,710
Let's put this over here in Iowa.

7
00:00:28,760 --> 00:00:35,830
Ten course in the swift section and create OK.

8
00:00:39,840 --> 00:00:42,320
It makes it a little larger for you here.

9
00:00:45,550 --> 00:00:48,450
So what is a class.

10
00:00:48,610 --> 00:00:48,910
OK.

11
00:00:48,910 --> 00:00:52,760
So we've been working with classes and object oriented programming.

12
00:00:52,830 --> 00:00:58,600
I think some time since the 70s and before that it was functional programming functional programming

13
00:00:58,600 --> 00:01:05,440
still exists today but object oriented programming is probably the most popular form of programming

14
00:01:05,710 --> 00:01:06,490
currently.

15
00:01:06,490 --> 00:01:10,510
And there are some new paradigms coming out that are trying to move away from it slightly like particle

16
00:01:10,510 --> 00:01:16,390
oriented programming which you also learn a little bit about of later on but object programming is the

17
00:01:16,390 --> 00:01:22,840
idea where you can create like a blueprint for something and create multiple copies of it.

18
00:01:22,840 --> 00:01:26,560
Perfect example is let's say a car and a car factory right.

19
00:01:26,650 --> 00:01:30,920
You can create a blueprint for a car and then you create multiple copies of that car.

20
00:01:31,000 --> 00:01:33,590
And so let's go ahead and use that example right here.

21
00:01:33,640 --> 00:01:36,570
We want to create a car so you create a class.

22
00:01:36,790 --> 00:01:37,760
OK.

23
00:01:38,410 --> 00:01:44,920
By defining the word type the key word class and then the name of the class organise say car or better

24
00:01:44,920 --> 00:01:55,370
yet view acle OK and a vehicle has multiple properties and functions such as we say var vehicle has

25
00:01:56,490 --> 00:01:57,980
a certain amount of tires.

26
00:01:58,060 --> 00:02:01,160
OK it's equal 0 or 4.

27
00:02:01,350 --> 00:02:10,070
If it's a if it's a vehicle with four tires that can be a default value for instance a vehicle can have

28
00:02:11,180 --> 00:02:12,430
lights like headlights right.

29
00:02:12,440 --> 00:02:15,030
So how many headlights as a car have.

30
00:02:15,030 --> 00:02:24,650
You know what is the horsepower of a car you know or an buccal 468 horsepower horse horsepower on a

31
00:02:24,650 --> 00:02:29,960
vehicle and a car can do things right you can do things such as drivers can create a function called

32
00:02:29,960 --> 00:02:39,090
Drive and it will you know there's some type of algorithm that will accelerate accelerate the UKL you

33
00:02:39,110 --> 00:02:39,580
know.

34
00:02:39,620 --> 00:02:43,970
So a vehicle can drive a vehicle can break.

35
00:02:44,720 --> 00:02:45,190
OK.

36
00:02:45,470 --> 00:02:53,150
So a class has properties such as tires headlights and horsepower and also can have functions such as

37
00:02:53,150 --> 00:02:54,550
drive and brake.

38
00:02:54,680 --> 00:03:00,110
And so what you want to do to understand object oriented programming is really visualize the world around

39
00:03:00,110 --> 00:03:00,300
you.

40
00:03:00,350 --> 00:03:04,970
Some people are really good at this like if you're an artist where you come from the design background

41
00:03:05,360 --> 00:03:09,840
object oriented programming tends to this clean to you because you can do things like objects like people.

42
00:03:09,850 --> 00:03:14,170
Sometimes if you're more on the other side of the brain it's harder to visualize you're maybe you think

43
00:03:14,300 --> 00:03:16,720
numbers and things like that.

44
00:03:17,000 --> 00:03:22,480
Whereas this is very much a visual type of way of laying out your code.

45
00:03:22,550 --> 00:03:27,380
So when we're coding for instance let's say we're making an app like Instagram.

46
00:03:27,380 --> 00:03:31,040
Instagram has users who can like other photos.

47
00:03:31,040 --> 00:03:37,640
So you might have a user object where it has a user ID an account username and password as properties

48
00:03:37,970 --> 00:03:45,680
and maybe has this user has functions such as you know reset password or delete account things like

49
00:03:45,680 --> 00:03:46,190
that.

50
00:03:46,280 --> 00:03:48,180
And then even the pictures could be an object.

51
00:03:48,290 --> 00:03:53,540
A photo is an object so class photo and it has the selected filter.

52
00:03:53,690 --> 00:03:57,380
It has how many likes it has how many comments it has.

53
00:03:57,380 --> 00:04:03,410
And those could be properties of it you know a photo you can at a function of a photo such as ADD like

54
00:04:03,860 --> 00:04:10,310
OK or remove like action items on it so you can start looking at objects and everything that's around

55
00:04:10,310 --> 00:04:11,690
you from the real world.

56
00:04:11,750 --> 00:04:13,150
Like others go outside.

57
00:04:13,160 --> 00:04:13,930
What does it have.

58
00:04:13,930 --> 00:04:15,290
Or there's a house.

59
00:04:15,290 --> 00:04:16,790
It has a door has windows.

60
00:04:16,790 --> 00:04:18,040
Those are the properties.

61
00:04:18,050 --> 00:04:23,440
What can a house do a house can cool down from the AC unit.

62
00:04:23,450 --> 00:04:28,640
So we can cool cool down house function or we can call heat up house function everything around you

63
00:04:28,880 --> 00:04:34,640
if you put it into some type of object can do something and also has properties of some time or things

64
00:04:34,640 --> 00:04:35,600
that describe it.

65
00:04:35,590 --> 00:04:41,600
And so you're going to see a lot of objects in programming or a lot of classes and this is exactly how

66
00:04:41,600 --> 00:04:47,690
it works using the class cured and then giving it some type of name giving it properties and then functionality

67
00:04:47,690 --> 00:04:48,610
of some kind.

68
00:04:49,540 --> 00:04:54,280
Some of the classes that you'll see as you as you move along and programming and some they've already

69
00:04:54,280 --> 00:05:01,800
worked with are you know things like this like view controller so I could say class you know new VC

70
00:05:01,860 --> 00:05:06,990
VC for view controller and it has inherited from you view controller OK.

71
00:05:07,210 --> 00:05:10,670
You've seen things like that and you've seen the view did load.

72
00:05:10,750 --> 00:05:16,090
You've seen those functions there like that we've just created a class and we've inherited from another

73
00:05:16,090 --> 00:05:16,800
class.

74
00:05:16,800 --> 00:05:17,420
OK.

75
00:05:17,620 --> 00:05:18,430
Really cool stuff.

76
00:05:18,430 --> 00:05:24,340
And so to create an instance of your class or what we've done here is we've defined our class.

77
00:05:24,430 --> 00:05:26,340
But if we want to create an instance of it.

78
00:05:26,780 --> 00:05:34,930
OK and let's give this vehicle a model case of our model so just be a no model to begin with.

79
00:05:34,930 --> 00:05:43,240
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

80
00:05:44,260 --> 00:05:45,150
like so.

81
00:05:45,220 --> 00:05:53,490
And then we a BMW datt model equals the say 3:28.

82
00:05:53,920 --> 00:05:54,450
OK.

83
00:05:54,820 --> 00:05:57,990
And so you can set the properties of this class like so.

84
00:05:58,110 --> 00:06:04,030
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

85
00:06:04,210 --> 00:06:11,200
equals vehicle and we're going to say Ford model equals F 150.

86
00:06:11,350 --> 00:06:12,560
You see what's happening here.

87
00:06:12,610 --> 00:06:19,960
We created code one time but we can start creating multiple instances of that code and then setting

88
00:06:19,960 --> 00:06:26,030
the properties of it as well as calling the function such as I can say for break and I've now called

89
00:06:26,030 --> 00:06:27,900
a function of this class.

90
00:06:28,370 --> 00:06:30,000
I'm told to do something.

91
00:06:30,010 --> 00:06:33,760
And so propertys functions on a class.

92
00:06:33,760 --> 00:06:35,390
Think of it like a blueprint.

93
00:06:35,530 --> 00:06:38,380
When you're designing your apps you're designing anything.

94
00:06:38,560 --> 00:06:43,330
Think in your mind can I give this properties or attributes that describe it and then think in your

95
00:06:43,330 --> 00:06:45,490
mind can it do anything.

96
00:06:45,490 --> 00:06:47,220
In most cases it's going to be yes.

97
00:06:47,410 --> 00:06:48,000
OK.

98
00:06:48,190 --> 00:06:52,150
Like if you had a bank account balance it's going to have your balance is going to have your first name

99
00:06:52,150 --> 00:06:57,640
your last name or your bank account and then it's going to have a function that says withdraw funds

100
00:06:57,640 --> 00:07:05,020
or add funds there's going to be different things that can happen to or that it can do in your class

101
00:07:05,050 --> 00:07:11,860
or in a bank account class for instance so classes they have properties they have functions things that

102
00:07:11,860 --> 00:07:16,870
can do stuff and then they have the ability to be instantiated.

103
00:07:16,860 --> 00:07:17,400
OK.

104
00:07:17,560 --> 00:07:22,330
At this point in time I'm instantiating that's the key word instantiating an instance of the vehicle

105
00:07:22,330 --> 00:07:26,080
class I am instantiating another instance of the vehicle class.

106
00:07:26,080 --> 00:07:27,900
These are also called objects.

107
00:07:28,000 --> 00:07:32,380
Once you've instantiated them they're called an object so you would say I have a foreign object or a

108
00:07:32,380 --> 00:07:36,090
BMW object or I'm creating an object or using an object.

109
00:07:36,100 --> 00:07:36,560
OK.

110
00:07:36,700 --> 00:07:40,170
At this point in time it's not an object it's just a class or a blueprint.

111
00:07:40,270 --> 00:07:45,480
But once it comes out of the factory OK when you instantiate it it becomes an object.

112
00:07:45,490 --> 00:07:46,080
OK.

113
00:07:46,180 --> 00:07:51,710
Another day another thing to know is that objects are passed by reference not by value.

114
00:07:51,910 --> 00:08:00,660
OK so when you let me show you here if I create a function and some function doesn't need to have an

115
00:08:01,090 --> 00:08:06,220
cool name and let's say we want to pass a viewpoint here like so of type buco.

116
00:08:06,680 --> 00:08:16,120
OK if I say View Hinkel the model no.

117
00:08:16,140 --> 00:08:20,570
Now what I told you to do I said lowercase I did not type in uppercase.

118
00:08:20,840 --> 00:08:23,890
It was really good me here.

119
00:08:23,890 --> 00:08:24,160
OK.

120
00:08:24,200 --> 00:08:28,120
View model equals cheese.

121
00:08:28,480 --> 00:08:30,010
OK.

122
00:08:30,070 --> 00:08:32,050
Something strange right.

123
00:08:32,410 --> 00:08:33,350
Get this.

124
00:08:33,350 --> 00:08:37,470
So let's say we have the Ford Model here right.

125
00:08:37,660 --> 00:08:42,640
And then I call some function and we know it's a Ford F-150 because I put that in the model.

126
00:08:42,640 --> 00:08:49,890
So let's say let's print the Ford dot model so we're going to print it right here.

127
00:08:49,950 --> 00:08:55,270
I'll put it on the right hand side that currently says F-150 150 but get this then I'm going to call

128
00:08:55,270 --> 00:09:05,740
some function and pass in Ford and then I'm going to print Ford model and look what happens.

129
00:09:05,740 --> 00:09:07,180
It's now cheese.

130
00:09:07,200 --> 00:09:09,830
Maybe you're thinking to yourself wait what just happened.

131
00:09:09,850 --> 00:09:12,430
How is this being modified this is supposed to be a constant.

132
00:09:12,430 --> 00:09:15,190
You're not supposed to be able to modify this is what's going on.

133
00:09:15,190 --> 00:09:18,710
Well they're pass by reference OK an object has a reference in memory.

134
00:09:18,820 --> 00:09:22,210
You can't copy an object you cannot cannot copy an object.

135
00:09:22,220 --> 00:09:27,370
OK you can copy values like integers and doubles but you cannot copy an object so these are passed by

136
00:09:27,370 --> 00:09:29,570
reference a very important distinction.

137
00:09:29,770 --> 00:09:33,560
This gets a lot of bugs and new developers apps pass by reference.

138
00:09:33,570 --> 00:09:43,210
You write this down right here pass by reference K or there are reference objects and if you're still

139
00:09:43,220 --> 00:09:46,040
confused let's do the same example again.

140
00:09:46,150 --> 00:09:52,660
Let's say some function are actually just this first one pass.

141
00:09:53,120 --> 00:09:55,880
We're passing an object by reference.

142
00:09:56,030 --> 00:09:58,610
So that's the name of this function now.

143
00:09:59,330 --> 00:10:05,010
OK let's get a new function here called pass by value.

144
00:10:05,040 --> 00:10:05,360
All right.

145
00:10:05,360 --> 00:10:16,840
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

146
00:10:16,860 --> 00:10:20,480
we say age equals 10.

147
00:10:20,490 --> 00:10:21,100
All right.

148
00:10:21,220 --> 00:10:23,200
And I forgot the phunk cured.

149
00:10:23,700 --> 00:10:24,290
OK.

150
00:10:24,300 --> 00:10:27,000
So here we've modified our value.

151
00:10:27,150 --> 00:10:27,590
OK.

152
00:10:27,630 --> 00:10:29,870
Nothing was copied over.

153
00:10:30,060 --> 00:10:32,040
So we're going to do now.

154
00:10:33,090 --> 00:10:34,350
Well first off get this.

155
00:10:34,380 --> 00:10:37,600
You can even change the value in because it's a constant.

156
00:10:37,860 --> 00:10:40,290
All right so that's that's already we can't do anything with that.

157
00:10:40,290 --> 00:10:51,610
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

158
00:10:51,630 --> 00:10:55,290
in someone's age.

159
00:10:55,290 --> 00:11:02,030
It won't even work because these are constants so my point is when I pass this in here like this.

160
00:11:02,200 --> 00:11:02,780
OK.

161
00:11:02,880 --> 00:11:05,930
It's being copied in here like so.

162
00:11:06,600 --> 00:11:12,780
And then I can use the value here so I could say you know let new age equals H and this one will never

163
00:11:12,780 --> 00:11:13,680
be modified.

164
00:11:13,860 --> 00:11:14,280
OK.

165
00:11:14,310 --> 00:11:16,710
Never ever ever be modified.

166
00:11:16,770 --> 00:11:18,650
So important distinction.

167
00:11:18,650 --> 00:11:23,450
Pass by reference you can modify the object it points to a specific spot of memory value types like

168
00:11:23,860 --> 00:11:30,200
like the string or the double or the integer or float or Suji float things like that are all passed

169
00:11:30,210 --> 00:11:31,290
by value.

170
00:11:31,290 --> 00:11:32,850
Or rather they're copied over.

171
00:11:32,850 --> 00:11:38,960
OK one is copied one is not very important so this is the basics of classes and object oriented programming.

172
00:11:39,000 --> 00:11:44,310
You can create a class has properties that describe it and then it has actions that it can take or things

173
00:11:44,310 --> 00:11:47,020
that can act upon that object.

174
00:11:47,190 --> 00:11:50,760
It's going to take you some while awhile to learn but as you do in this course and doing other things

175
00:11:50,760 --> 00:11:53,180
with programming it will all come together over time.

176
00:11:53,250 --> 00:11:54,090
But that's it for now.

177
00:11:54,090 --> 00:11:56,640
Mark Price of slopes dot com.

178
00:11:56,670 --> 00:11:57,750
See you soon.
