1
00:00:00,510 --> 00:00:07,290
So we're going to wrap up polymorphism with one more sub topic, so it's a pretty short portion of the

2
00:00:07,290 --> 00:00:13,080
course, but there is one more thing to discuss and that is abstract base classes.

3
00:00:13,800 --> 00:00:15,780
So you've seen what we've done so far.

4
00:00:15,840 --> 00:00:24,060
We basically have a shape based class and then we have triangle and rectangle and circle as the derive

5
00:00:24,060 --> 00:00:24,750
classes.

6
00:00:25,770 --> 00:00:33,900
So in the last lesson we talked about instantiating these and having them be in dynamic memory as a

7
00:00:33,900 --> 00:00:41,670
pointer, and we showed how we could kind of use some of the memory functions from the shape class and

8
00:00:41,670 --> 00:00:44,310
even call them a shape pointer.

9
00:00:44,310 --> 00:00:52,610
But they would be resolved through the methods and the data members to the correct and derived class.

10
00:00:52,620 --> 00:01:00,750
So if you notice the main CBP, you remember we say Shape Pointer as an R.A. and C, and this was respectively

11
00:01:01,350 --> 00:01:03,090
rectangle triangle and circle.

12
00:01:03,090 --> 00:01:07,140
And even though we were calling the same named methods, it was able to resolve them correctly.

13
00:01:08,340 --> 00:01:14,760
So what are we going to talk about now is the fact that shape isn't really something that we should

14
00:01:14,760 --> 00:01:21,870
be instantiating really, because it's actually not specified, a shape is kind of an ambiguous thing

15
00:01:22,050 --> 00:01:24,510
like you must have a type of shape, right?

16
00:01:25,230 --> 00:01:31,380
So if someone says, oh, draw a shape, you would be like, Well, what shape do I try to draw rectangle

17
00:01:31,380 --> 00:01:33,720
or triangle or circle something more complex?

18
00:01:34,680 --> 00:01:40,710
So for that reason, we shouldn't really have shape be something that can be instantiated in the client

19
00:01:40,710 --> 00:01:41,100
code.

20
00:01:41,280 --> 00:01:45,750
And so that leads us to talk about this notion of an abstract base class.

21
00:01:46,770 --> 00:01:48,630
So if we go into this shape based class.

22
00:01:49,810 --> 00:01:55,600
What we can do is make some small changes, and we will turn this into just an abstraction where it

23
00:01:55,600 --> 00:02:01,810
can be used as a base scaffolding for the rest of the derived classes.

24
00:02:02,110 --> 00:02:06,720
So it's not meant to be instantiated, but it's just meant to help create a structure.

25
00:02:06,730 --> 00:02:11,980
So when you're designing your program, you might actually want to start out with an abstract base class

26
00:02:12,880 --> 00:02:17,490
just to solidify your idea of, Hey, I'm going to have different shapes.

27
00:02:17,500 --> 00:02:23,710
Why don't I make a base class college shape and put some of the shared information between shapes here

28
00:02:23,710 --> 00:02:24,730
in this base class?

29
00:02:24,730 --> 00:02:27,610
But I don't want people to be able to create a shape object.

30
00:02:27,970 --> 00:02:34,710
I only want them to be able to create specific types of shaped objects like triangle, circle and rectangle.

31
00:02:36,490 --> 00:02:45,010
So we only need to do a few things to change our project here into something that uses an abstract base

32
00:02:45,010 --> 00:02:45,300
class.

33
00:02:45,310 --> 00:02:52,900
So all I'm going to do is I am going to go to these virtual functions and I'm going to put in equals

34
00:02:53,020 --> 00:02:55,960
zero right here and then commented out.

35
00:03:01,610 --> 00:03:08,240
So you notice that I'm not having any implementation here, the return zero is completely committed

36
00:03:08,240 --> 00:03:12,160
out here and the return shape is come into doubt as well.

37
00:03:12,440 --> 00:03:19,610
On this tape member function the equals zero might seem kind of weird, but it's really just a syntax

38
00:03:19,610 --> 00:03:23,870
to say, Hey, this is an abstracted member function.

39
00:03:24,380 --> 00:03:28,070
It is not something that's meant to be implemented in this class.

40
00:03:28,610 --> 00:03:36,170
It is something that actually must and I say must, because it's required now that you have an area

41
00:03:36,590 --> 00:03:43,330
and a type of function in every one of the classes that inherits from the shape class.

42
00:03:43,340 --> 00:03:52,600
So this triangle circle and rectangle must all have both area and type member functions now.

43
00:03:53,180 --> 00:03:56,330
And that is because of this syntax that we just put here.

44
00:03:57,410 --> 00:03:58,940
With the equals zero.

45
00:03:59,900 --> 00:04:05,900
So once again, that makes it so these are not really ever going to be used in the shape class and really,

46
00:04:06,170 --> 00:04:12,540
that's not going to ever be a problem because we're never going to instantiate a shape object here.

47
00:04:13,370 --> 00:04:20,990
But it's required now that every class that inherits from shape must implement any one of the functions

48
00:04:20,990 --> 00:04:24,080
here that you have in the base class that say equals zero.

49
00:04:24,530 --> 00:04:27,860
These are something called pure virtual functions.

50
00:04:28,070 --> 00:04:31,490
So this one and this one are both pure virtual functions.

51
00:04:32,180 --> 00:04:40,160
Another thing that happens by doing this is it turns this base class from a normal based class into

52
00:04:40,160 --> 00:04:44,390
an abstract base class, which abstract base class?

53
00:04:44,480 --> 00:04:47,410
This is what we're talking about in this specific video.

54
00:04:47,420 --> 00:04:52,340
And really, the main thing it means is that the fact that we are not going to instantiate it.

55
00:04:53,270 --> 00:04:59,420
So since we're not going to instantiate it, let's go back to Maine and see what happens after we've

56
00:04:59,420 --> 00:05:03,860
modified these and it's turned our shape class into an abstract base class.

57
00:05:05,150 --> 00:05:08,270
So I go back here and noticed that this is underlined in red now.

58
00:05:08,990 --> 00:05:16,310
If I hover over it, it says allocating an object of abstract class type shape, and it gives me two

59
00:05:16,310 --> 00:05:17,400
options right here.

60
00:05:17,420 --> 00:05:25,070
So look, it says unemployment pure virtual method area in shape and implement pure virtual method type

61
00:05:25,070 --> 00:05:25,580
in shape.

62
00:05:25,940 --> 00:05:32,540
And remember also, I've said this reminder a few times, but whenever I say method that is synonymous

63
00:05:32,900 --> 00:05:39,590
with memory function and you see that even though we call new member functions, sea lion even puts

64
00:05:39,950 --> 00:05:40,940
method in here.

65
00:05:41,390 --> 00:05:48,170
So it gives you the option to click both of one or both of these to get rid of that pure virtual method

66
00:05:48,170 --> 00:05:49,100
area in shape.

67
00:05:49,490 --> 00:05:50,930
So if I click on this?

68
00:05:52,210 --> 00:05:58,690
Quick unemployment, that method, it takes me here, so I could actually just if I wanted to get rid

69
00:05:58,690 --> 00:06:01,940
of that, but I don't want to, I still want to be an abstract base class.

70
00:06:02,920 --> 00:06:04,890
And you know, it's giving us this problem.

71
00:06:06,280 --> 00:06:11,800
So what I'm going to need to do is comment this out or completely get rid of it because we can no longer

72
00:06:11,800 --> 00:06:12,670
implement a shape.

73
00:06:14,260 --> 00:06:18,580
Because of the House, and you get rid of this because this was as was the actual shape, and we're

74
00:06:18,580 --> 00:06:22,970
not going to be trying to print out any of this information about the generic shape.

75
00:06:22,990 --> 00:06:24,940
We need a specific type of shape now.

76
00:06:25,660 --> 00:06:32,760
So all I have to do is comment out this line here on Line 10 and these two lines 15 in line 16, assuming

77
00:06:32,800 --> 00:06:35,140
bit the rest, we left the same.

78
00:06:36,130 --> 00:06:37,840
So I'll go ahead and run this.

79
00:06:39,980 --> 00:06:45,770
And you notice that everything still runs fine, except now we are using it how we're supposed to,

80
00:06:46,100 --> 00:06:51,130
where we're using the actual type of shape rather than just some ambiguous shape object, right?

81
00:06:51,140 --> 00:06:56,300
So we have the red rectangle, the yellow triangle and the green circle with their areas being printed

82
00:06:56,300 --> 00:06:57,650
out appropriately as well.

83
00:06:58,400 --> 00:07:00,260
So we didn't have to change a whole lot.

84
00:07:01,210 --> 00:07:08,020
This part of polymorphism is really about the design idea, so it's a really important part of object

85
00:07:08,020 --> 00:07:08,860
oriented programming.

86
00:07:09,490 --> 00:07:18,070
And the idea is that when you're coming up with your design for your object or in your programming classes

87
00:07:18,070 --> 00:07:25,750
and program overall, the structure you can be thinking about how you can come up with a base class

88
00:07:25,750 --> 00:07:29,510
that will allow you to build out a derived class.

89
00:07:29,530 --> 00:07:38,800
So think of different things that have a generic commonality like maybe you know, a car like we've

90
00:07:38,800 --> 00:07:39,960
dealt with car before, right?

91
00:07:40,480 --> 00:07:44,470
We have a car and we have certain types of car or like an airplane class.

92
00:07:44,470 --> 00:07:51,100
And then you have certain types of airplane like you're not just going to instantiate a generic airplane

93
00:07:51,700 --> 00:07:55,990
type, you're going to only instantiate certain types of airplane, right?

94
00:07:56,740 --> 00:07:58,270
Just like shape.

95
00:07:58,480 --> 00:08:04,090
You know, like we said in the beginning, if I say draw a shape, you would probably ask, well, what

96
00:08:04,090 --> 00:08:04,660
type of shape?

97
00:08:05,590 --> 00:08:06,760
Because you wouldn't know what to draw.

98
00:08:06,790 --> 00:08:10,540
You would just have to dress whatever type of shape that you know of, right?

99
00:08:11,050 --> 00:08:17,380
But shape is something to ambiguous to abstract, hence the name abstract base class.

100
00:08:18,040 --> 00:08:24,240
So that's why we use this just as a base to build off of, right?

101
00:08:24,880 --> 00:08:32,680
We have some common methods or member functions here that go along with every type of shape, right

102
00:08:32,680 --> 00:08:34,320
triangle circle and rectangle.

103
00:08:34,330 --> 00:08:39,340
They all have their own type of area that should be implemented.

104
00:08:39,340 --> 00:08:41,560
Write their own formulas for it.

105
00:08:41,950 --> 00:08:44,920
They also have a name for their type of shape, right?

106
00:08:44,920 --> 00:08:50,140
So they're going to be able to return the specific string that's associated with the name or the type

107
00:08:50,560 --> 00:08:51,190
of the shape.

108
00:08:51,640 --> 00:08:57,700
That's why these are appropriate to be pure virtual functions because we know we're going to implement

109
00:08:57,700 --> 00:09:02,890
these in every type of shape or every derived class that comes from these shape.

110
00:09:02,890 --> 00:09:03,940
Abstract base class.

111
00:09:05,450 --> 00:09:11,330
OK, so that's really all I have for this lesson, and this pretty much wraps up polymorphism, so make

112
00:09:11,330 --> 00:09:17,180
sure that you review anything that you're not completely sold on or go back to the video.

113
00:09:17,570 --> 00:09:22,490
We will probably still be using it throughout the class and in the next section, we're going to build

114
00:09:22,490 --> 00:09:24,770
off of our knowledge and go into templates.

115
00:09:25,520 --> 00:09:25,820
All right.

116
00:09:25,820 --> 00:09:27,800
So with that, I will see you in the next lesson.
