1 00:00:05,300 --> 00:00:08,290 In this video, we'll talk more about the default constructor. 2 00:00:08,950 --> 00:00:13,150 A default constructor is a constructor that doesn't expect any arguments. 3 00:00:13,150 --> 00:00:15,510 It's also called the no args constructor. 4 00:00:16,309 --> 00:00:20,110 C++ has to have a way to construct and initialize objects 5 00:00:20,110 --> 00:00:22,910 if you don't provide any initialization information at all. 6 00:00:23,570 --> 00:00:26,230 So if you don't provide any constructors at all, 7 00:00:26,230 --> 00:00:29,830 c++ will generate a default constructor automatically. 8 00:00:30,330 --> 00:00:34,930 This default constructor does nothing, but it's still generated by c++. 9 00:00:35,730 --> 00:00:38,730 When you create objects with no initialization information, 10 00:00:38,730 --> 00:00:42,930 this is the constructor that's called. Let's take a look at the account class again. 11 00:00:44,290 --> 00:00:47,590 In this account class, we've defined no constructors at all. 12 00:00:47,890 --> 00:00:51,790 So c++ will automatically generate a default constructor 13 00:00:51,790 --> 00:00:55,990 that allows us to create objects with no initialization information. 14 00:00:55,990 --> 00:00:57,590 Let's see a few examples. 15 00:00:58,890 --> 00:01:02,250 Here we're creating two local objects Frank account and Jim account. 16 00:01:02,850 --> 00:01:05,650 The compiler provided default constructor will be used since we didn't provide 17 00:01:05,650 --> 00:01:08,010 since we didn't provide any constructors at all. 18 00:01:08,810 --> 00:01:10,910 In the case of a pointer to an object, 19 00:01:10,910 --> 00:01:14,210 the same thing happens except that the object is created on the heap. 20 00:01:15,010 --> 00:01:18,310 Since the compiler generated default constructor does nothing, 21 00:01:18,310 --> 00:01:23,210 the class member attributes of the account class could contain garbage since they haven't been initialized. 22 00:01:24,310 --> 00:01:27,990 Of course, we're free to provide our own user-defined no args constructor. 23 00:01:27,990 --> 00:01:30,210 In fact it's best practice to do so. 24 00:01:30,870 --> 00:01:35,070 In this case, we're defining our own user-defined no argus constructor. 25 00:01:35,070 --> 00:01:39,170 This constructor will be called when no initialization information is provided. 26 00:01:39,170 --> 00:01:43,630 And in this constructor, we set the account name to none and the account balance to 0. 27 00:01:43,630 --> 00:01:48,530 So we're initializing the account member attributes to values we know about, not garbage data. 28 00:01:49,230 --> 00:01:52,890 Now let's see what happens when we define a constructor that expects arguments. 29 00:01:55,190 --> 00:01:59,550 In this case, we're defining a constructor that expects a string and a double, 30 00:01:59,550 --> 00:02:03,750 and we initialize our member attributes to the values passed into the constructor. 31 00:02:03,750 --> 00:02:05,750 This is very commonly done. 32 00:02:05,750 --> 00:02:09,250 However, once we define a constructor for our class 33 00:02:09,250 --> 00:02:14,240 c++ will now not generate the no args default constructor automatically. 34 00:02:14,600 --> 00:02:17,960 If we still need it, then we must explicitly define it ourselves. 35 00:02:18,840 --> 00:02:22,740 If you have code that creates objects with no initialization information, 36 00:02:22,740 --> 00:02:26,100 then that code will no longer compile since the default constructor 37 00:02:26,100 --> 00:02:28,390 is no longer generated by c++. 38 00:02:29,290 --> 00:02:33,540 So in this example, we can no longer create account objects with no arguments, 39 00:02:33,540 --> 00:02:38,040 again, since the default constructor is no longer automatically generated by the compiler. 40 00:02:38,700 --> 00:02:42,900 So we'll get three compiler errors when we try to create Frank, Jim and Mary's account. 41 00:02:43,300 --> 00:02:45,660 However, we can still create account objects, 42 00:02:45,660 --> 00:02:49,460 but we have to use the constructor we provided that expects a string and a double. 43 00:02:49,820 --> 00:02:53,610 So we can create Bill's account since it provides the initialization information 44 00:02:53,610 --> 00:02:55,210 required by the constructor. 45 00:02:55,810 --> 00:02:58,210 Let's go through some of these examples in the IDE. 46 00:02:58,610 --> 00:03:02,210 Okay. So I'm in the IDE. I'm in the section 13 workspace 47 00:03:02,210 --> 00:03:06,010 in the default constructor project. And what I've got here is 48 00:03:06,010 --> 00:03:10,710 the same player class that we've been using where we've got the name the health and the xp for a player 49 00:03:10,710 --> 00:03:12,710 which are private instance variables, 50 00:03:12,710 --> 00:03:16,010 and we've also got a couple of member methods here: 51 00:03:16,010 --> 00:03:20,910 set name and get name that's just the setter and the getter for that name attribute right there. 52 00:03:21,790 --> 00:03:25,790 What's important to understand here, and of course, I would do the same thing for health or xp, 53 00:03:25,790 --> 00:03:29,690 but i don't want to clutter up the code, I just want to talk about the default constructors here. 54 00:03:29,690 --> 00:03:34,430 You'll notice that this player class has no constructor declared. 55 00:03:35,030 --> 00:03:38,030 C++ has to have a way to create objects. 56 00:03:38,030 --> 00:03:41,470 So if you don't provide any constructors, 57 00:03:41,470 --> 00:03:43,830 it will provide a system generated 58 00:03:43,830 --> 00:03:47,430 no args constructor that will allow you to create objects. 59 00:03:47,430 --> 00:03:50,630 So in this case, I don't have any constructors declared, 60 00:03:50,630 --> 00:03:53,230 but yet, I'm able to create 61 00:03:54,000 --> 00:03:57,000 player objects with no problem, 62 00:03:57,000 --> 00:04:00,800 I could say Frank.set name to let's say Frank. 63 00:04:03,700 --> 00:04:05,300 And I could output the 64 00:04:06,300 --> 00:04:08,960 value of Frank's name by using get name. 65 00:04:11,760 --> 00:04:14,660 Okay. Something real simple like that. And what we'll do is we'll walk through this, 66 00:04:14,660 --> 00:04:18,560 we'll step through it using the debugger. So I'll put a break point right here, 67 00:04:18,560 --> 00:04:20,560 and we can debug this real quickly. 68 00:04:21,459 --> 00:04:25,260 And we really don't care about the output. We just want to see that object. 69 00:04:25,260 --> 00:04:28,260 So you can see here that I'm on line 24, 70 00:04:28,510 --> 00:04:33,110 and I'm going to instantiate that Frank object, which is a player. 71 00:04:33,510 --> 00:04:35,010 There is no constructor. 72 00:04:35,510 --> 00:04:39,110 So behind the scenes, a default no args constructor is 73 00:04:39,110 --> 00:04:40,910 generated that does nothing, 74 00:04:40,910 --> 00:04:43,570 but it does allow c++ to create that object. 75 00:04:43,570 --> 00:04:45,170 So I'm going to go next here. 76 00:04:45,970 --> 00:04:48,570 At this point, Frank is created. 77 00:04:49,370 --> 00:04:51,370 You can see the object right up here. 78 00:04:51,370 --> 00:04:55,270 And I executed the set name, which set the attribute name to Frank. 79 00:04:55,870 --> 00:04:56,420 That's it. 80 00:04:56,420 --> 00:04:59,020 The next statement would obviously print it to the console. 81 00:04:59,020 --> 00:05:02,220 So you can see c++ has to have a way to create these objects. 82 00:05:02,220 --> 00:05:04,880 If you don't provide a way to initialize this 83 00:05:04,880 --> 00:05:07,780 using any kind of no args constructor, 84 00:05:07,780 --> 00:05:10,280 you get one automatically by the compiler. 85 00:05:10,780 --> 00:05:15,380 That's great. Now of course -- I'm going to stop this. Of course, we can create our own 86 00:05:15,380 --> 00:05:19,880 no args constructor. So let's do that. I'll do it down here so it's closer to the main. 87 00:05:20,880 --> 00:05:24,200 So what we can do is we can create our constructor. There it is. It's called player, 88 00:05:24,200 --> 00:05:26,700 same name as the class, no args, 89 00:05:27,060 --> 00:05:29,360 and we'll implement it right here. 90 00:05:29,360 --> 00:05:33,660 Now what's the default behavior I want when I don't provide any initialization information, 91 00:05:34,210 --> 00:05:37,110 anything reasonable whatever makes sense for your application. 92 00:05:37,110 --> 00:05:39,410 In this case, let's just say that the name is none. 93 00:05:40,770 --> 00:05:44,370 We'll say that the health is, let's say, a 100. 94 00:05:48,070 --> 00:05:50,270 And let's say the xp is 3. 95 00:05:52,770 --> 00:05:54,970 That's it. That's my initializer. 96 00:05:54,970 --> 00:05:58,470 That's also my default initializer. That's what's important. 97 00:05:58,470 --> 00:06:01,130 This will now be called right here. And I'll put a break 98 00:06:01,130 --> 00:06:04,730 point right in there so we can see it being called, and we'll debug this again. 99 00:06:09,130 --> 00:06:11,630 And here we go. What you can see happening is 100 00:06:11,990 --> 00:06:13,870 we're right here on line 30. 101 00:06:13,870 --> 00:06:16,070 And now when i step through this, 102 00:06:16,070 --> 00:06:19,430 notice how it transfers control to line 22, 103 00:06:19,430 --> 00:06:21,430 that's exactly what we expected. 104 00:06:21,430 --> 00:06:24,930 We didn't see that before because we didn't provide a default constructor, 105 00:06:24,930 --> 00:06:29,920 but now we're providing one. So ours will be called rather than the compiler created one. 106 00:06:30,470 --> 00:06:34,070 And this will just step through and it'll set the properties to name 107 00:06:34,670 --> 00:06:35,970 100 and 3. 108 00:06:36,770 --> 00:06:40,770 Now I'm back here on line 31. And you can see right here 109 00:06:40,770 --> 00:06:42,970 that we've got none, 100 and 3. 110 00:06:42,970 --> 00:06:45,870 And now I can change the nun to Frank right here on this line. 111 00:06:46,970 --> 00:06:49,630 And I'll refresh this, and you can see here it's Frank 112 00:06:50,430 --> 00:06:52,430 right there, the string is Frank. 113 00:06:52,430 --> 00:06:54,730 Okay. So there you go. 114 00:06:54,730 --> 00:06:59,090 If you don't provide any constructors, you get a default constructor for free if you will. 115 00:06:59,090 --> 00:07:03,390 And if you provide your own default constructor that's the one that will be used. 116 00:07:03,750 --> 00:07:06,050 Now let's do one more thing here. 117 00:07:06,050 --> 00:07:10,450 What happens if you create -- let's let me comment this out right here. 118 00:07:12,110 --> 00:07:14,010 And suppose that I want to create 119 00:07:14,560 --> 00:07:17,920 a different constructor, an overloaded constructor. So I'm going to say player 120 00:07:18,580 --> 00:07:22,180 and I want to provide the name. 121 00:07:22,680 --> 00:07:27,080 I want to provide all of those values. So this is my name val. I want health 122 00:07:29,960 --> 00:07:33,620 and I want the xp as well. I want to provide all three of these values. 123 00:07:34,320 --> 00:07:38,680 And in the body of this constructor, I'm just going to say name equals name val, 124 00:07:40,680 --> 00:07:42,280 health equals health val 125 00:07:46,280 --> 00:07:48,880 and xp equals xp val. 126 00:07:51,780 --> 00:07:55,980 Okay. So that's where we're at. Now we have a bit of a problem now. 127 00:07:56,780 --> 00:07:59,180 In c++ -- and this is true of many object- 128 00:07:59,180 --> 00:08:01,680 oriented languages, including java and others. 129 00:08:02,180 --> 00:08:05,480 You get that default constructor as long as you don't provide 130 00:08:05,480 --> 00:08:09,580 any other constructor. Well, what we just did here is we provided a constructor. 131 00:08:09,940 --> 00:08:13,140 In this case, this constructor expects three arguments. 132 00:08:13,140 --> 00:08:15,800 Now that compiler generated 133 00:08:15,800 --> 00:08:19,160 no args, default constructor will no longer be generated. 134 00:08:19,160 --> 00:08:23,060 So this line of code right here on line 35 won't compile anymore. 135 00:08:23,720 --> 00:08:27,080 Let me run the debugger here. You'll see the error right here, 136 00:08:27,080 --> 00:08:30,740 it says no matching function call for the no args constructor. 137 00:08:31,400 --> 00:08:35,200 This is perfectly normal. This is exactly the way it's supposed to work. 138 00:08:35,530 --> 00:08:39,429 The idea being that now you've provided a way to construct your object, 139 00:08:39,429 --> 00:08:42,730 so c++ note will no longer provide a default way, 140 00:08:42,730 --> 00:08:47,230 right,maybe you don't want a default way. So this won't compile. 141 00:08:47,230 --> 00:08:48,430 I'll comment that out, 142 00:08:49,420 --> 00:08:51,620 and I'll move the breakpoint down to here. 143 00:08:52,280 --> 00:08:57,270 So now the way we need to construct this object is to explicitly call that other constructor. 144 00:08:57,270 --> 00:09:01,370 So I'm going to say player Frank, and we'll initialize that to Frank 145 00:09:02,370 --> 00:09:05,570 100 and let's say 13 or something. 146 00:09:08,170 --> 00:09:12,770 Now what's going to happen is when this line executes let me get rid of fearless break points, 147 00:09:13,130 --> 00:09:16,430 when this line executes on 36, what's going to happen is 148 00:09:16,430 --> 00:09:19,430 this constructor is going to be called, the one that i just created. 149 00:09:19,430 --> 00:09:22,030 So I'll put a break point there so you can see that happening. 150 00:09:22,830 --> 00:09:26,930 Okay. So let's debug this again. We'll walk through it one more time, 151 00:09:27,920 --> 00:09:30,120 and here we are. We're on line 36. 152 00:09:31,220 --> 00:09:35,520 When I step through it, you'll see control will be transferred up to line 27 up here. 153 00:09:35,920 --> 00:09:39,580 So there we are. We're in the constructor now 154 00:09:39,580 --> 00:09:44,380 so we're explicitly calling that 3 arg constructor that we just created. 155 00:09:44,380 --> 00:09:48,980 Remember, once I create any kind of constructor, the default constructor 156 00:09:49,280 --> 00:09:52,580 that's automatically generated won't be generated anymore. 157 00:09:52,980 --> 00:09:54,280 We'll walk through this. 158 00:09:55,160 --> 00:09:59,160 And when we're done, we'll refresh this. And you can see Frank 113 has 159 00:09:59,160 --> 00:10:01,660 been created just as we expected. 160 00:10:01,660 --> 00:10:04,320 Now of course, we can 161 00:10:05,520 --> 00:10:08,820 create both of these, right. So now I can create, 162 00:10:10,480 --> 00:10:12,680 let's say, a Hero object. 163 00:10:13,980 --> 00:10:15,780 So now what we've got is we've got 164 00:10:15,780 --> 00:10:20,080 two constructors that we provided. We provided a no argus constructor,and we provided 165 00:10:20,080 --> 00:10:23,280 this overloaded constructor that expects the three arguments. 166 00:10:23,280 --> 00:10:27,530 In this case, Hero, we're not providing any initialization information. 167 00:10:27,530 --> 00:10:31,530 So the no args constructor, this guy right here on line 21 will be called. 168 00:10:32,190 --> 00:10:35,690 And in the case of Frank, it'll be just like before this one would be called. 169 00:10:35,690 --> 00:10:37,690 So let's step through that, 170 00:10:40,250 --> 00:10:43,550 and let me do that again, I'll put a break point on Hero. 171 00:10:45,910 --> 00:10:50,900 Let's try that one more time, and you can see what's happening here is. I'm right here on line 35, 172 00:10:50,900 --> 00:10:53,400 so I'm going to construct that Hero object. 173 00:10:53,950 --> 00:10:56,950 And you can see transfers control right up to here. 174 00:10:56,950 --> 00:11:00,750 So there's no args constructor is being called, that's what we expect. 175 00:11:00,750 --> 00:11:05,250 That's our constructor, right. That's not compiler generated because we wrote it ourselves. 176 00:11:05,250 --> 00:11:07,240 So that's going to execute. 177 00:11:07,600 --> 00:11:12,000 Now I come back to Frank, and this constructor will be called, so you'll see 178 00:11:12,300 --> 00:11:14,600 control will be transferred over here to 27. 179 00:11:15,260 --> 00:11:19,360 And we'll run through that, you can see it happening right here on line 29 that's where I'm at now. 180 00:11:20,160 --> 00:11:23,960 And at this point, we've got both those objects constructed 181 00:11:23,960 --> 00:11:26,120 and we can see them right here. 182 00:11:26,520 --> 00:11:31,120 There's Frank and there's Hero. Hero uses none 103, 183 00:11:31,120 --> 00:11:35,320 and Frank is using Frank 113, you see them right here. 184 00:11:35,920 --> 00:11:39,920 Okay. So that's it. That's the behavior for default constructors in c++.