﻿1
00:00:00,490 --> 00:00:03,010
‫So now we're going to talk about classes.

2
00:00:03,010 --> 00:00:09,640
‫And this is something that we are going to use a lot in this course, especially in the data structures

3
00:00:09,640 --> 00:00:10,420
‫section.

4
00:00:10,990 --> 00:00:15,760
‫My favorite analogy for a class is that it's like a cookie cutter.

5
00:00:15,760 --> 00:00:22,690
‫It's something that you use to make cookies with or whatever object you're creating.

6
00:00:23,080 --> 00:00:29,710
‫So for my example, the class that I'm going to create is actually going to be called Cookie.

7
00:00:30,350 --> 00:00:36,470
‫So standard naming convention for a class as you always capitalize it, and then it will end with a

8
00:00:36,470 --> 00:00:44,030
‫curly brace and then you put a semicolon after it and then classes will have attributes.

9
00:00:44,480 --> 00:00:51,680
‫In this case, we'll have the attribute color, and typically you want to set your attributes to be

10
00:00:51,680 --> 00:00:52,580
‫private.

11
00:00:52,820 --> 00:00:57,680
‫And then also typically you'll have what is called a constructor.

12
00:00:57,950 --> 00:01:05,780
‫A constructor is a member function that has the same name as the class, and most of your member functions

13
00:01:05,780 --> 00:01:08,060
‫are going to be set to public.

14
00:01:08,640 --> 00:01:13,500
‫So in this case, we're going to pass the constructor a string that is called color.

15
00:01:13,620 --> 00:01:15,990
‫That is what this is.

16
00:01:16,440 --> 00:01:23,160
‫But over here, we have this color when we set this color to be equal to color.

17
00:01:23,400 --> 00:01:27,080
‫What we're actually doing is changing the color that is up here.

18
00:01:27,090 --> 00:01:30,360
‫That's what this color is.

19
00:01:30,810 --> 00:01:38,100
‫And I'm going to talk a little bit more about the key word, this in this video, but I'll talk about

20
00:01:38,100 --> 00:01:42,780
‫it in more depth when we get to the constructor for link lists.

21
00:01:43,260 --> 00:01:46,470
‫But the thing I'll talk about here is the keyword.

22
00:01:46,470 --> 00:01:53,880
‫This refers to a specific instance of whatever you're creating, in this case, a cookie.

23
00:01:54,300 --> 00:02:00,690
‫So now that we've written this code, we can actually create a new cookie like this.

24
00:02:01,200 --> 00:02:07,650
‫We're going to call our cookie cookie one and we're going to set it equal to new cookie and we'll pass

25
00:02:07,650 --> 00:02:09,600
‫it the color green.

26
00:02:10,050 --> 00:02:13,500
‫So let's walk through this line of code.

27
00:02:13,980 --> 00:02:17,400
‫So our variable name is Cookie one.

28
00:02:17,670 --> 00:02:20,600
‫So notice that this is a pointer to a cookie.

29
00:02:20,610 --> 00:02:22,170
‫This is how it's going to work.

30
00:02:22,170 --> 00:02:29,790
‫Whenever you create an object with a class, whatever variable that you set equal to that object that

31
00:02:29,790 --> 00:02:33,420
‫you've created will be a pointer to that object.

32
00:02:33,900 --> 00:02:38,070
‫And in this case, that object is a cookie.

33
00:02:38,610 --> 00:02:41,130
‫We also have this new keyword.

34
00:02:41,130 --> 00:02:47,760
‫The new keyword will make it where we run the constructor, and we're going to pass that constructor

35
00:02:47,760 --> 00:02:49,230
‫the color green.

36
00:02:49,440 --> 00:02:54,240
‫And when we do all of this, it will create a green cookie.

37
00:02:54,660 --> 00:03:02,430
‫So you could say that this cookie, this one right here has the color green.

38
00:03:03,030 --> 00:03:09,240
‫So that keyword this is referring to this specific instance of cookie.

39
00:03:09,690 --> 00:03:14,540
‫And so to make that point more clear, let's create another instance of cookie.

40
00:03:14,550 --> 00:03:20,550
‫We'll call this one Cookie two, and we'll make this one blue.

41
00:03:20,940 --> 00:03:24,000
‫So we have a different variable name cookie to.

42
00:03:24,770 --> 00:03:27,650
‫And we've made this one blue.

43
00:03:28,160 --> 00:03:35,660
‫This particular instance of Cookie that we're calling Cookie two is going to be blue.

44
00:03:36,170 --> 00:03:45,590
‫So let's drop these cookies out of here and expand this cookie class so we can create other member functions

45
00:03:45,590 --> 00:03:46,610
‫in the cookie class.

46
00:03:46,610 --> 00:03:55,010
‫So we could do something like get color that returns the color for a particular instance of cookie.

47
00:03:55,250 --> 00:04:03,020
‫Then we can also do something like set color that is going to change the color for a particular instance

48
00:04:03,020 --> 00:04:03,890
‫of cookie.

49
00:04:04,190 --> 00:04:10,310
‫So let's bring back these two lines of code that actually create our two cookies and we'll look at all

50
00:04:10,310 --> 00:04:12,740
‫of this and VTS code.

51
00:04:13,480 --> 00:04:17,410
‫So there is our cookie class that we just created there.

52
00:04:17,980 --> 00:04:19,990
‫And then I'm going to scroll up here.

53
00:04:20,350 --> 00:04:27,010
‫And our main function contains those two lines that create cookie one and Cookie two.

54
00:04:27,550 --> 00:04:34,630
‫And then with these two lines, we'll print out the color of cookie one and cookie two.

55
00:04:35,440 --> 00:04:42,310
‫So I'll run this and you can see that cookie one is green and cookie two is blue.

56
00:04:42,940 --> 00:04:52,210
‫But if I come up here and I paste in this line, we're going to set the color of cookie one to be yellow.

57
00:04:52,660 --> 00:04:57,970
‫So now when we run these two lines, we'll expect cookie one to be yellow, but cookie two to still

58
00:04:57,970 --> 00:04:59,380
‫stay blue.

59
00:04:59,890 --> 00:05:01,150
‫And I'll run this.

60
00:05:01,660 --> 00:05:07,630
‫And you can see that we've changed the color of cookie one to yellow, but cookie two still stays blue.

61
00:05:08,020 --> 00:05:14,980
‫And the reason for that is when we did the set color, we only did it to the instance of Cookie that

62
00:05:14,980 --> 00:05:16,870
‫we're calling Cookie one.

63
00:05:17,440 --> 00:05:19,570
‫So now let's flip back.

64
00:05:20,080 --> 00:05:27,850
‫So what we've created so far is a class and cookie one, which is now yellow, and then cookie two,

65
00:05:27,880 --> 00:05:29,710
‫which remains blue.

66
00:05:30,220 --> 00:05:34,450
‫So then the question becomes, how are we going to use this in the course?

67
00:05:34,720 --> 00:05:40,300
‫So an example of where we'll use it will be when we create a linked list.

68
00:05:40,630 --> 00:05:46,720
‫We'll have a constructor for our linked list, which is going to be the same name as the class, but

69
00:05:46,720 --> 00:05:50,120
‫we'll create other member functions as well.

70
00:05:50,140 --> 00:05:57,100
‫So for example, we'll create a member function that will append a node of a particular value to the

71
00:05:57,100 --> 00:05:58,420
‫end of the length list.

72
00:05:58,630 --> 00:06:06,010
‫And then we'll write another function to remove the last item in the list, and one to pretend a value

73
00:06:06,010 --> 00:06:14,290
‫to the beginning of the link list, or to insert a node with a particular value at a particular index,

74
00:06:14,710 --> 00:06:18,190
‫or to remove a value at a particular index.

75
00:06:18,640 --> 00:06:24,340
‫So in the next section, when we create length lists, we'll create all of these member functions and

76
00:06:24,340 --> 00:06:25,330
‫a few more.

77
00:06:25,990 --> 00:06:28,780
‫And that is our quick overview.

78
00:06:29,560 --> 00:06:30,730
‫Of classes.

