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