1 00:00:05,440 --> 00:00:06,930 Hello, everyone and welcome back. 2 00:00:07,710 --> 00:00:10,410 In this video, we'll go over the challenge solution 2. 3 00:00:10,670 --> 00:00:14,529 This is the solution for the overloaded operators using non-member functions. 4 00:00:14,589 --> 00:00:19,380 So I'm in the section 14 workspace in the challenge-solution2 project. 5 00:00:20,110 --> 00:00:24,390 And you can see my header file now from the private modifier 6 00:00:24,390 --> 00:00:25,830 down, it's exactly the same. 7 00:00:26,090 --> 00:00:28,680 The only thing that's changed is that I've declared prototypes for 8 00:00:28,689 --> 00:00:32,520 my overloaded operator functions, and they are all friend functions. 9 00:00:32,900 --> 00:00:34,130 Now we don't have to do that. 10 00:00:34,139 --> 00:00:36,850 We can certainly not have friend functions and work through 11 00:00:36,850 --> 00:00:38,290 getters and setters and so forth. 12 00:00:38,320 --> 00:00:41,670 But for the purposes of this video, I'm making everything a friend. 13 00:00:41,670 --> 00:00:44,449 So I've got access to everything that's private in this class, 14 00:00:44,610 --> 00:00:46,709 which is really just that string pointer right here. 15 00:00:47,800 --> 00:00:50,800 Now as you can see, when we have something like operator 16 00:00:50,800 --> 00:00:53,499 minus in this case, we're overloading the unary operator. 17 00:00:53,660 --> 00:00:56,050 There's exactly one item in the parameter list. 18 00:00:56,580 --> 00:00:59,290 And when we have something that's binary, we've got two 19 00:00:59,300 --> 00:01:00,600 parameters in the parameter list. 20 00:01:00,840 --> 00:01:02,990 So it's a little bit different from using member methods. 21 00:01:03,630 --> 00:01:04,620 And that's about it. 22 00:01:04,670 --> 00:01:07,320 Now what we do is we basically have to implement these functions, 23 00:01:07,370 --> 00:01:10,140 and you'll see they're very, very straightforward, same 24 00:01:10,140 --> 00:01:11,899 idea as with the member method. 25 00:01:11,910 --> 00:01:14,320 So let's go to the implementation file here, 26 00:01:15,720 --> 00:01:17,490 and we'll start with the equality operator. 27 00:01:17,890 --> 00:01:20,809 Now we don't have -- remember, in all of these cases, we no 28 00:01:20,809 --> 00:01:22,340 longer have a this pointer. 29 00:01:22,700 --> 00:01:27,320 There's no object dot operator method because they're non-member functions. 30 00:01:27,410 --> 00:01:29,080 So we don't have that this pointer. 31 00:01:29,459 --> 00:01:30,949 But we do have another object. 32 00:01:30,970 --> 00:01:33,570 And in this case, we have left-hand side and right-hand side. 33 00:01:33,990 --> 00:01:36,830 And all we want to do is compare the left-hand side string with 34 00:01:36,830 --> 00:01:38,070 the right-hand side string. 35 00:01:38,360 --> 00:01:40,459 And you can see that we're doing that right here in 36 00:01:40,460 --> 00:01:41,610 the string compare function. 37 00:01:41,910 --> 00:01:44,719 We're comparing left-hand side string to the right-hand side string. 38 00:01:45,190 --> 00:01:48,129 And if we get back to 0, they're the same, and that's what we're returning. 39 00:01:48,129 --> 00:01:49,539 So we're returning true false here. 40 00:01:50,980 --> 00:01:52,130 Same exact idea. 41 00:01:52,540 --> 00:01:55,810 The not equal operator is exactly the same thing except I'm negating 42 00:01:55,810 --> 00:01:58,859 the result right here with the bang for the less than and 43 00:01:58,860 --> 00:02:00,470 greater than overloaded operators. 44 00:02:00,480 --> 00:02:04,439 You can see that the code is almost exactly like it was in the member 45 00:02:04,440 --> 00:02:07,830 method version except that I don't have this on the left side, I've 46 00:02:07,830 --> 00:02:09,989 got an LHS or left-hand side object. 47 00:02:10,919 --> 00:02:12,980 Okay, so let's take a look at make lowercase. 48 00:02:13,440 --> 00:02:16,350 In this case, this is a unary function. 49 00:02:16,350 --> 00:02:17,970 So I've just got one argument right here. 50 00:02:18,429 --> 00:02:20,690 And what I'm doing is same thing I did before. 51 00:02:20,690 --> 00:02:24,549 I create an area in memory to copy that string into and what I'm going 52 00:02:24,550 --> 00:02:27,270 to do is make it lowercase, and that's what I'm going to do here. 53 00:02:27,270 --> 00:02:33,010 I've allocated enough space for that object strings length plus 1, then 54 00:02:33,010 --> 00:02:37,719 I copy that object string character by character into buff, and then I 55 00:02:37,730 --> 00:02:41,790 simply loop through buff and make each character is lowercase equivalent. 56 00:02:42,309 --> 00:02:44,970 When I'm done with that, I construct an object that I'm going to 57 00:02:44,970 --> 00:02:48,829 return right here, and I construct it based on buff, return it. 58 00:02:48,880 --> 00:02:51,260 And don't forget to delete the buffer as we always do, 59 00:02:51,260 --> 00:02:52,260 so we don't leak memory. 60 00:02:53,099 --> 00:02:55,470 The concatenation operator, same idea. 61 00:02:55,470 --> 00:02:57,550 I've got the left-hand side and the right-hand side. 62 00:02:57,690 --> 00:03:00,970 So obviously, you're going to have left-hand side plus right-hand side. 63 00:03:01,300 --> 00:03:03,630 So we need to allocate another character buffer here. 64 00:03:04,030 --> 00:03:07,290 That's the length of both those strings added together plus 1. 65 00:03:07,550 --> 00:03:09,850 So you can see here we have left-hand string, here we 66 00:03:09,850 --> 00:03:10,849 have right-hand string. 67 00:03:12,360 --> 00:03:15,155 We copy the left-hand string over, and then we concatenate 68 00:03:15,490 --> 00:03:17,159 the right-hand string onto that. 69 00:03:17,760 --> 00:03:22,269 We create the new object here based on that new string that's concatenated 70 00:03:22,270 --> 00:03:25,810 now, and we return it, and of course, we delete the buffer as well. 71 00:03:26,820 --> 00:03:28,150 That's it. Really straightforward. 72 00:03:28,160 --> 00:03:30,519 Now they get really really simple to implement because 73 00:03:30,820 --> 00:03:33,780 we've got our plus equal, right our concatenate and a sine. 74 00:03:34,000 --> 00:03:37,980 Well, we can implement that based on our concatenation because 75 00:03:38,010 --> 00:03:41,560 plus equal simply means left-hand side equals left-hand side plus 76 00:03:41,560 --> 00:03:44,680 right-hand side, so we can express that directly which leads to 77 00:03:44,680 --> 00:03:46,000 very, very readable code here. 78 00:03:47,730 --> 00:03:50,280 The repeat operator or the star operator that we're 79 00:03:50,280 --> 00:03:51,890 overloading, same idea. 80 00:03:51,890 --> 00:03:54,640 Remember, repeating something just means adding it over and over. 81 00:03:54,640 --> 00:03:57,970 So we'll take advantage that we've already overloaded that concatenation 82 00:03:57,970 --> 00:03:59,690 operator right here on line 44. 83 00:04:00,030 --> 00:04:01,100 We're using it right there. 84 00:04:01,420 --> 00:04:05,850 So given n, we're just simply going to loop that many times and say temp 85 00:04:05,880 --> 00:04:09,150 equals temp plus left-hand side, do it again and again and again. 86 00:04:09,160 --> 00:04:11,799 And we're just basically tacking on that string to 87 00:04:11,799 --> 00:04:13,019 itself over and over and over. 88 00:04:13,310 --> 00:04:15,190 When we're done, we return temp. 89 00:04:15,750 --> 00:04:19,700 Our star equal operator or repeat and assign operator works the same. 90 00:04:19,700 --> 00:04:23,380 In this case, it's a binary operator, it expects an object and then the 91 00:04:23,380 --> 00:04:26,259 star and then an integer is how many times you're going to repeat it. 92 00:04:26,710 --> 00:04:27,740 So we'll do the same thing. 93 00:04:27,740 --> 00:04:30,569 We'll take advantage of the fact that we've already overloaded 94 00:04:30,930 --> 00:04:33,410 the repeat operator or the star operator in this case. 95 00:04:33,740 --> 00:04:37,290 We'll simply say left-hand side equals left-hand side star n. 96 00:04:37,670 --> 00:04:38,719 That will call this. 97 00:04:38,760 --> 00:04:39,870 It will take care of it. 98 00:04:40,309 --> 00:04:42,950 And then we return left-hand side, so we can keep assigning. 99 00:04:44,059 --> 00:04:46,480 And the last two that are left are the pre-increment and 100 00:04:46,480 --> 00:04:49,099 post-increment operators, which just make the strings uppercase. 101 00:04:50,370 --> 00:04:53,979 So the semantics for the pre-increment is simply just increment that 102 00:04:53,980 --> 00:04:55,380 string, it's really straightforward. 103 00:04:55,940 --> 00:04:58,080 The string belongs to obj, right. 104 00:04:58,080 --> 00:05:01,230 Remember, this is a unary operator, so we only have one object here. 105 00:05:01,590 --> 00:05:05,950 And obj has an str pointer that points to a character array on the heap. 106 00:05:06,209 --> 00:05:10,850 So what we're doing is just looping through that character array one 107 00:05:10,850 --> 00:05:13,700 item at a time, one character at a time, and we're setting the 108 00:05:13,730 --> 00:05:17,180 item of that object string to the uppercase equivalent of it. 109 00:05:17,940 --> 00:05:20,460 And then we just simply return the object when we're done. 110 00:05:20,530 --> 00:05:23,890 So you can see we're actually modifying obj here, which 111 00:05:23,890 --> 00:05:26,200 is exactly what you would expect with a pre-increment. 112 00:05:26,980 --> 00:05:29,539 So the post-increment is very similar. 113 00:05:29,620 --> 00:05:32,060 We need to keep a copy of the original object, so 114 00:05:32,060 --> 00:05:33,150 we're doing that right here. 115 00:05:33,380 --> 00:05:36,490 We're creating a copy temp based on object, which is the 116 00:05:36,490 --> 00:05:37,640 one that's being incremented. 117 00:05:38,469 --> 00:05:41,779 Then I'm doing a pre-increment of object, which takes care of making 118 00:05:41,779 --> 00:05:42,990 all the characters uppercase. 119 00:05:43,250 --> 00:05:44,930 And then I'm returning the old object. 120 00:05:45,190 --> 00:05:47,360 So you can see that this line right here actually 121 00:05:47,370 --> 00:05:49,020 modifies the current object. 122 00:05:49,670 --> 00:05:53,020 And this line right here returns a copy of the original object, 123 00:05:53,030 --> 00:05:56,110 which is the exactly what the semantics are of post increment. 124 00:05:56,780 --> 00:05:58,739 I wrote a comment here, as I mentioned before. 125 00:05:58,740 --> 00:06:02,540 Make sure you call pre-increment and not post-increment. 126 00:06:02,550 --> 00:06:05,659 If you call post-increment, this method here will be called over 127 00:06:05,660 --> 00:06:08,400 and over recursively and you'll never be able to break out of it. 128 00:06:09,170 --> 00:06:09,830 So that's it. 129 00:06:09,870 --> 00:06:13,500 If we run this, we should have exactly the same output as we had before. 130 00:06:13,500 --> 00:06:14,500 Let's try it, 131 00:06:16,250 --> 00:06:17,559 enter a string to repeat. 132 00:06:17,559 --> 00:06:22,120 And we'll do the same string I entered before dash three zeros and a dash. 133 00:06:23,000 --> 00:06:25,270 And I believe I said five last time, I'll do that again. 134 00:06:26,849 --> 00:06:27,690 And there we go. 135 00:06:27,700 --> 00:06:30,969 We've got our five 0 0 0 strings. 136 00:06:30,969 --> 00:06:33,469 We've got 60 of these now, and the rest of the code is 137 00:06:33,469 --> 00:06:35,239 exactly the same as it was. 138 00:06:36,190 --> 00:06:38,140 All right, so congratulations. 139 00:06:38,140 --> 00:06:40,520 I hope you did well on this challenge. 140 00:06:41,300 --> 00:06:44,530 You can say now that you're no longer c++ beginner programmers. 141 00:06:44,530 --> 00:06:45,209 That's for sure. 142 00:06:45,510 --> 00:06:48,200 Yeah, you've learned a lot about the way c++ works behind the 143 00:06:48,200 --> 00:06:51,730 scenes, operator overloading, the constructors, the move assignments, 144 00:06:51,750 --> 00:06:53,110 the regular copy assignments. 145 00:06:53,369 --> 00:06:54,220 So you've learned a lot. 146 00:06:54,510 --> 00:06:58,820 The next step is to learn about inheritance and polymorphic functions, 147 00:06:58,830 --> 00:06:59,980 and that's where we're going to go. 148 00:07:00,420 --> 00:07:03,180 And I should mention one last thing is when you look at these 149 00:07:03,180 --> 00:07:06,460 test cases that I created, these aren't really test cases. 150 00:07:06,469 --> 00:07:09,010 They're just little pieces of code that exercise some of 151 00:07:09,010 --> 00:07:11,760 those methods that we wrote in functions that we wrote. 152 00:07:12,500 --> 00:07:14,940 When you do testing, you really want to do proper testing using 153 00:07:14,940 --> 00:07:18,760 unit testing, using some frameworks such as you know unit test plus 154 00:07:18,770 --> 00:07:19,990 plus or something like that. 155 00:07:19,990 --> 00:07:23,239 So keep that in mind, we're doing this just to keep it simple and 156 00:07:23,559 --> 00:07:27,430 not have to learn other frameworks in order to learn this framework. 157 00:07:27,759 --> 00:07:29,560 So I'll see you in the inheritance section.