1 00:00:05,400 --> 00:00:08,000 When we learned about c++ functions, 2 00:00:08,300 --> 00:00:12,200 we learned that we can supply default values as parameters to functions. 3 00:00:13,080 --> 00:00:16,079 The same applies to class methods and class constructors. 4 00:00:16,740 --> 00:00:19,840 In many cases, we can reduce the number of overloaded 5 00:00:19,840 --> 00:00:22,940 constructors by providing default constructor parameters. 6 00:00:23,440 --> 00:00:26,940 The same rules that we learned when working with default function parameters 7 00:00:26,940 --> 00:00:28,440 apply to constructors. 8 00:00:30,040 --> 00:00:32,240 Let's continue working with our player class. 9 00:00:32,790 --> 00:00:35,150 We can actually just have one constructor 10 00:00:35,150 --> 00:00:38,450 that does everything that we did with the three constructors previously. 11 00:00:39,050 --> 00:00:43,050 You can't always have default parameters in class design, but many times you can. 12 00:00:43,650 --> 00:00:45,850 Here's an example where it makes perfect sense. 13 00:00:46,510 --> 00:00:50,310 Notice that the player class now has only a single constructor, 14 00:00:50,310 --> 00:00:52,560 and that constructor has three arguments: 15 00:00:52,560 --> 00:00:55,260 the name, health and xp of the player. 16 00:00:55,920 --> 00:01:00,020 But in this example, we're providing default parameter values for all the parameters. 17 00:01:01,010 --> 00:01:04,110 For the player's name, the default is the string none. 18 00:01:04,110 --> 00:01:06,770 And for health in xp, the defaults are 0. 19 00:01:07,430 --> 00:01:11,090 These default parameters are declared in the constructor declaration 20 00:01:11,090 --> 00:01:12,590 in the class as shown. 21 00:01:13,390 --> 00:01:15,490 Now when we implement the constructor, 22 00:01:15,490 --> 00:01:20,480 we can implement it exactly as we implemented the three argument constructor in the previous videos. 23 00:01:21,840 --> 00:01:24,840 Here you can see the implementation for the single constructor. 24 00:01:25,340 --> 00:01:28,340 Notice that you don't supply the default parameters here. 25 00:01:28,340 --> 00:01:32,140 We can simply use an initializer list to initialize the class attributes, 26 00:01:32,140 --> 00:01:35,700 and the compiler will take care of providing the default values as needed. 27 00:01:36,600 --> 00:01:39,400 This is very handy and results in less code to write 28 00:01:39,400 --> 00:01:43,060 which means less code to test which means less code that could have errors. 29 00:01:44,360 --> 00:01:48,160 Since this single constructor is doing the job of several constructors, 30 00:01:48,160 --> 00:01:51,760 we have to be careful not to declare ambiguous constructors. 31 00:01:51,760 --> 00:01:55,420 For example, what would happen if we implemented a no argus constructor 32 00:01:55,920 --> 00:01:58,020 in addition to the one we see on this slide? 33 00:01:58,520 --> 00:02:02,120 Well, we'd get a compiler error because the compiler wouldn't know which one to call. 34 00:02:02,890 --> 00:02:05,690 In this example, we're creating empty, 35 00:02:05,690 --> 00:02:10,190 frank and villain objects, as we did before. But take a look at hero. 36 00:02:10,440 --> 00:02:14,340 We didn't have a two args constructor in the previous version of the player class, 37 00:02:14,340 --> 00:02:16,340 but we do in this version. That's pretty cool. 38 00:02:16,940 --> 00:02:21,600 Now let's modify our existing player class in the IDE so you can see how this works. 39 00:02:23,590 --> 00:02:27,360 Hi, so I'm in the IDE, and I'm in the section 13 workspace 40 00:02:27,360 --> 00:02:30,860 in the DefaultConstructorParameters project. 41 00:02:31,260 --> 00:02:35,660 And what we've got is the player class as we defined it in the last few videos 42 00:02:35,660 --> 00:02:39,560 where we've got our three constructors and here we're using delegating constructors 43 00:02:39,560 --> 00:02:41,760 and down here we're using an initializer, 44 00:02:41,760 --> 00:02:44,760 a constructor initializer list right here on line 32. 45 00:02:44,760 --> 00:02:47,460 What I want to do is I want to get rid of -- I want to simplify this. 46 00:02:47,460 --> 00:02:50,060 I want to get rid of some of these overloaded constructors 47 00:02:50,060 --> 00:02:53,660 and use default parameters instead as we did with the slides. 48 00:02:53,660 --> 00:02:58,060 So what I want to do is just get rid of all these constructors right here. 49 00:02:58,060 --> 00:03:01,560 I just want to end up with one single constructor and there it is, 50 00:03:01,560 --> 00:03:03,330 player with three arguments. 51 00:03:03,330 --> 00:03:07,480 And what I'd like to do is supply some default parameter values for these. 52 00:03:07,480 --> 00:03:10,280 Now you can't always do this when you're doing class design, 53 00:03:10,280 --> 00:03:14,480 but sometimes you can. And when you can, you should because it really simplifies your code. 54 00:03:14,480 --> 00:03:18,080 So in this case, I'm going to default 55 00:03:18,080 --> 00:03:19,680 the name of the 56 00:03:20,180 --> 00:03:21,880 player to none. 57 00:03:21,880 --> 00:03:25,240 I'm going to default the health value to 0. 58 00:03:25,240 --> 00:03:28,540 I'm going to default the xp value to 0. 59 00:03:30,140 --> 00:03:32,140 Okay. So that's what we've got now. 60 00:03:32,140 --> 00:03:35,440 There is my three default parameters. 61 00:03:36,040 --> 00:03:39,640 Now these two constructors go away, so I'm just going to get rid of them right here. 62 00:03:40,740 --> 00:03:44,740 And this constructor was which is our three arg constructor that 63 00:03:44,740 --> 00:03:48,400 matches the signature for this one, we really don't have to do anything to it. 64 00:03:48,400 --> 00:03:52,000 These default parameters will automatically be supplied 65 00:03:52,000 --> 00:03:54,000 by the compiler when we need them. 66 00:03:54,250 --> 00:03:57,150 Okay. So now what we've got is 67 00:03:57,150 --> 00:04:01,600 same code we had before. Now I'll put a break point here. It's the only constructor being called, 68 00:04:01,600 --> 00:04:03,600 so we'll definitely see it being called. 69 00:04:03,600 --> 00:04:08,300 And I'm going to create an empty object here. So again, it's a player object. 70 00:04:08,300 --> 00:04:11,500 And I'm not supplying any initialization information here. 71 00:04:11,500 --> 00:04:16,300 So what's going to happen is all three of those default parameters will be substituted. 72 00:04:16,300 --> 00:04:20,100 And I'll end up with an object none 0 0, which is exactly what I want. 73 00:04:20,100 --> 00:04:21,399 In the case of frank, 74 00:04:22,000 --> 00:04:26,600 I'll end up with frank 0 0, because the none will be overridden by the frank. 75 00:04:27,290 --> 00:04:32,280 In the last one, all three of my initializers will be supplied so, it'll be villain 100 and 55. 76 00:04:32,580 --> 00:04:35,580 Now there's another one that we've got here as well, 77 00:04:35,780 --> 00:04:38,780 which is this one. Let's just say player hero. 78 00:04:40,080 --> 00:04:42,980 And suppose I want to say this is hero, 79 00:04:42,980 --> 00:04:47,440 and it's 100 for the health. 80 00:04:48,100 --> 00:04:51,900 Now in this case, that hero object will have the name hero 81 00:04:51,900 --> 00:04:55,400 100 for health since the xp wasn't supplied. 82 00:04:55,400 --> 00:04:56,760 The xp will get the value of 0. 83 00:04:56,760 --> 00:05:00,420 Now this is a constructor we didn't have before. We didn't have a two args constructor. 84 00:05:01,020 --> 00:05:04,020 Okay. So let's run this, and we'll walk through it. 85 00:05:06,120 --> 00:05:08,820 And I'll put the out screen over here. The output screen will be real simple. 86 00:05:08,820 --> 00:05:13,320 We'll just get this three args constructor message being displayed all the time, 87 00:05:13,320 --> 00:05:16,720 that's the only constructor we have. Okay. So let's start here. 88 00:05:16,720 --> 00:05:18,520 Here's my player empty, 89 00:05:18,520 --> 00:05:22,120 right. This is a no args constructor that's going to be called. 90 00:05:22,120 --> 00:05:24,320 Well, we don't have a no argus constructor. 91 00:05:24,320 --> 00:05:27,920 But we've got this constructor that expects three arguments, 92 00:05:27,920 --> 00:05:31,280 and we're telling the compiler to default those three if it has to. 93 00:05:31,280 --> 00:05:32,640 In this case, it has to. 94 00:05:32,640 --> 00:05:37,300 So what it's going to do is it's going to provide none 0 0 and call this guy right here. 95 00:05:37,960 --> 00:05:40,560 Okay. So let's step through this, and we'll see -- 96 00:05:40,560 --> 00:05:44,560 we're in the constructor now, you can see that that constructor did indeed get called. 97 00:05:44,560 --> 00:05:46,560 We're on line 20 right now. 98 00:05:46,560 --> 00:05:50,860 And we're going to initialize it. We're going to display three args constructor over here. 99 00:05:51,220 --> 00:05:53,020 And then we're back to main. 100 00:05:53,020 --> 00:05:58,010 At this point, empty should have none 0 0 which is exactly what we've got. 101 00:05:58,890 --> 00:06:01,590 Now in the case of the player frank, 102 00:06:01,590 --> 00:06:04,890 I expect it to have frank 0 0. So let's walk through that. 103 00:06:06,250 --> 00:06:08,050 There I am at line 20 again. 104 00:06:08,650 --> 00:06:13,090 I'll step through it a couple more times. Now we'll see three args constructor again 105 00:06:13,090 --> 00:06:13,990 being displayed. 106 00:06:14,490 --> 00:06:17,190 I'm back in my main. And if I look at frank here, 107 00:06:17,190 --> 00:06:20,390 we see frank 0 0 which is again what we expected. 108 00:06:20,390 --> 00:06:24,590 Now the hero object we expect to have hero 100 and 0. 109 00:06:24,590 --> 00:06:25,890 So let's walk through that. 110 00:06:26,790 --> 00:06:30,090 We're in that constructor right now. We're in the initialization list. 111 00:06:30,090 --> 00:06:34,290 And we'll print out the three args constructor message one more time. 112 00:06:35,480 --> 00:06:39,380 We're done initializing that object now. So if we look at hero, 113 00:06:39,380 --> 00:06:42,040 we should see hero 100 0, 114 00:06:42,040 --> 00:06:44,240 which is again exactly what I expected. 115 00:06:44,240 --> 00:06:48,900 Now in the last case, I'm providing all three of the initializers 116 00:06:49,200 --> 00:06:52,200 So what we expect is villain 155, right. 117 00:06:52,200 --> 00:06:56,500 None of the default parameters here will be used. So let's try that out. We'll walk through it, 118 00:06:57,160 --> 00:07:00,660 and we're back in main now. If we look at villain, 119 00:07:01,460 --> 00:07:04,460 we'll have villain 155, just what we want. 120 00:07:04,960 --> 00:07:07,160 Okay. So that gives you an idea of 121 00:07:07,160 --> 00:07:10,060 how we can use default constructor parameters, 122 00:07:10,060 --> 00:07:13,060 which is really really handy. It'll definitely simplify your code. 123 00:07:13,060 --> 00:07:15,420 As I said before, you can't always use these. 124 00:07:15,420 --> 00:07:17,780 Sometimes it really doesn't make sense to provide 125 00:07:17,780 --> 00:07:22,680 default parameter values. You really want you know the user to explicitly 126 00:07:22,680 --> 00:07:23,670 provide all of them. 127 00:07:23,670 --> 00:07:26,670 But sometimes you can. And when you can, they come real handy. 128 00:07:27,870 --> 00:07:30,120 Now just before we wrap up this video 129 00:07:30,120 --> 00:07:32,920 let's see what happens if we supply 130 00:07:32,920 --> 00:07:37,520 a constructor and c++ has an issue with ambiguity. 131 00:07:37,520 --> 00:07:39,880 So in this case, remember, we've got -- 132 00:07:39,880 --> 00:07:43,280 we're handling all of these types of initialization here, 133 00:07:43,280 --> 00:07:46,780 right, 0 argument 1 argument 2 arguments and 3 arguments, 134 00:07:46,780 --> 00:07:48,680 all with a single constructor. 135 00:07:49,180 --> 00:07:53,180 So what happens if I decide to do something like player 136 00:07:53,780 --> 00:07:56,380 no args constructor and I implement that. 137 00:07:56,780 --> 00:08:01,380 Well, hopefully, you'll see the problem when the compiler sees the code on line 28, 138 00:08:01,680 --> 00:08:05,340 it doesn't know what to do, it could call this default constructor 139 00:08:05,340 --> 00:08:10,240 or it could call this overloaded constructor with the three default parameters. 140 00:08:10,240 --> 00:08:13,340 It's not going to guess, it's just going to give you a compiler error. 141 00:08:13,340 --> 00:08:16,640 So let me debug this or build it and run it you'll see the arrows. 142 00:08:16,640 --> 00:08:20,240 You can see it in case, you get it you'll know what it's talking about. 143 00:08:21,140 --> 00:08:24,240 It says right here error call of overloaded player is ambiguous, 144 00:08:24,240 --> 00:08:26,240 which is exactly correct.