Hello, everyone. Welcome to the section 14 challenge.
Congratulations on finishing up this part of the course.
I'm in the section 14 workspace.
And you can see that I have a challenge project, and
this is the basic shell that I want to talk about now.
There's also a challenge solution one and a challenge solution
two, and I'll talk about those projects in just a second.
So the whole idea with this challenge is for you to overload
operators for the mystring class.
Now I'm providing a basic mystring class that's got all the
constructors, your move constructors, your assignment operators.
And I've even overloaded the insertion and extraction operators just to make
it easier for you to test your code.
So let's talk about this challenge.
The purpose of the challenge for you to gain experience overloading
operators, and I would recommend that you do this challenge twice that's
why we have two solutions here.
The first time do it by overloading the operators using member functions
and then start a new project and do the same thing but this
time use non-member functions.
Okay, so let's talk about some of the operators that you could overload.
Now I'm just giving you an example here.
You can certainly do whatever you like, whatever makes
sense to you as crazy as you like, this is just to learn.
So be as creative as you like.
But here's a couple of examples.
We've already done the unary minus in the slides in the examples.
All it does is makes a string lowercase.
Here's the example for equality.
We want to compare two strings, s1 and s2.
Now obviously, this will call s1.operator,
the equality operator with s2, right.
And this is assuming the member method.
So remember, all of these operators that we're talking about get mapped to
some function whether it be a member function or a non-member function,
just as we talked about in the class.
So that's the equality operator.
Then we're also going to have the not equal operator, which just checks
to see if two strings are not equal.
So in the case of s1 being Joe and s2 being Frank, this will return
true because they're not equal.
Then we've got the less than and greater than operators, which
just test strings lexically.
So for example, if I've got frank and I've got George, frank will be less
than George since f is less than g.
You get the idea, just basic string comparison.
We can also do the greater than equal to or less than or equal
to, I'll leave those up to you if you choose to do them.
We've got concatenation which we've already done.
But again, I haven't done any of these so you can get experience doing them.
I'd recommend that you try to do them by yourself without going
back to the slides and looking at the code we've written.
That way you get a little bit you know better feel for how
to overload the operators and how they work with the code.
We've got the plus equal operator.
This is an interesting operator because it's s1 plus equals s2.
So this is the same as s1 equals s1 plus s2.
Okay, then I've got this operator here. And this one may be a little confusing.
So I'm going to take a second to talk about it.
The idea here is that we have s2 times 3.
And this is an integer here.
And the idea would be if I have s2 being abc, then s2 times 3 is going
to give me three copies of abc.
It's going to repeat that string whatever it is n times
where n is this guy right here.
So this could be a pretty handy operator.
So s1 equals s2 times 3, what's going to happen is, you're
going to get abc, abc, abc, and then it gets assigned to s1.
Okay, so the last one is the star equal operator.
Here we're going to repeat the string on the left-hand side and times.
So it's really simple.
So if s1 is abc, then s1 star equal 4 gives me 4 abcs, not 5.
We're not really assigning back to the original s1.
If you want to do that, that would be perfectly fine.
But in this case, it's really easy to think repeat s1 four times
and store it right back in s1.
So it's pretty handy.
And then the last operators we'll talk about are the increment
and decrement operators.
I haven't really talked about them too much in this class,
but I'll talk about them now.
And the idea here is that -- remember, these are increment and decrement
operators and we have pre and post versions of each one.
Now assuming that we've got member versions and that's
what I'm talking about here, member method versions of these.
These are the semantics for this.
You'll notice we have operator plus plus.
And then we've got operator plus plus with an int.
That int is not used.
It's not even named, it's just a placeholder.
And the compiler is using that behind the scenes to be able to
tell the difference between these two prototypes if you will because
otherwise it'd be exactly the same and it wouldn't know which one to call.
So this guy right here is the pre-increment.
And the way that this works is the semantics are really clear.
You do whatever you want increment to do.
I'm making it make all the characters uppercase in
the in the current object.
And then you return this because you're actually modifying
that object before you go back to the caller, right.
That's what pre-increment does.
Now the semantics for post-increment are a little bit different.
And let me just scroll up just a little here so you can probably
see it a little more clearly.
Here we go.
Now the semantics for the post increment are a little bit different.
We need to make a copy of what we've got now.
Then we call operator plus plus.
Notice that this doesn't have an int in there, right.
So we're calling pre-increment.
That's really important.
If you call post increment, you end up with a recursive loop here and you're
never going to get out of there.
So what we're doing is we're incrementing, we're doing a
pre-increment, and then we're returning this temporary
object that we saved, which is the old copy, right.
And that's exactly what the the whole idea with post increment means.
So that's the semantics of that.
And then the rest is pretty straightforward.
Have fun.
Think of some other operators that might be useful.
It doesn't matter if they make sense or not.
This is all about learning how to overload operators.
So be as crazy and creative as you want to be.
Some hints.
Take advantage of the std string compare function
for the equality operators.
Remember that guy returns 0 if they're equal, negative something
or positive something if they're greater than or less than.
I'll let you guys look that up so you can learn about it.
Another thing is the plus equal and star equals
operators should return a ref.
Okay, that's really important because we want to be able to chain,
and we don't want extra copies.
Finally, when you're doing plus sequel and star equal,
rather than duplicate all your code again, use plus and star.
We've already used those operators.
We've already implemented them, and if you do that these operators,
when you go to overlay these operators right here, the code
really becomes almost trivial.
It's just a line or two of code.
It's really, really straightforward.
One more thing I want to mention is when you do your increment and
decrement operators, make sure that the pre-increment right here returns
a ref, and make sure that the post increment does not return a ref, it
should return a mystring by value.
Okay, so that's basically it.
Let me go over the mystring class so that you can see
where the starting point is.
This should be really familiar now after this section.
There's the mystring class.
You can see that I've overloaded the insertion and extraction
operators as friend functions.
Here's mystring class, and it's got nothing that you
wouldn't expect, right.
It's got all the constructors, the move constructor, the assignment
operators, both the copy and the move and your getters on your display.
You shouldn't even have to use display anymore because now
we've got this guy right here.
Okay, so that's about it.
If you look at implementation, again, it's exactly like we've done in class.
The one thing that I have done is I've commented out these
output statements using copy assignment using move assignments.
But I left them in there in case you want to uncomment them and
trace your code or you can use the debugger to trace your code.
I'll leave that up to you.
The last thing is here's the main.
Here's a real sample name.
And I've commented the entire main out so that you can uncomment as you
go or create your own test cases and.
Here's some simple test cases for this.
By no means, these are exhaustive but they'll certainly you know begin
to test your overload operators.
So let me go through a couple of them.
You can see right here, I've got a string a frank, I've
got a string b frank they're. They're the same string right,
so in this case, I'm comparing a to b.
I expect that to be true since they're both frank.
And I'm asking is a not equal to b, well, that's false
because they are equal, right.
So what you see over here in the comments is what I expect as output.
Now we create b as george.
Remember, a is still frank.
Are a and b equal?
No, they're not.
Is a not equal to b?
That's correct.
Is a less than b?
Well, a is frank b is george, the f comes before the g, so this is true.
Is a greater than b?
This is false.
You get the idea.
So these are just some sample test cases that you can use along the way.
And then I've got some other ones that are testing the the negation operator
right here, the unary minus, right.
So s1 is frank.
And then I'm just saying s1 equals minus s1, which makes s1 lowercase.
So when I output it, I expect frank in lowercase.
There's my concatenation operator.
Right now frank is lowercase that's s1.
I'm just adding a bunch of asterisks to the end, displaying it.
That's what I expect.
I'm doing it again, but this time I'm using plus equal,
which just means added these dashes to the left-hand side.
Well, the left-hand side is this guy right here.
So you can see the dash is being added right here.
Okay, here's another example.
S2 is 1 2 3 4 5,
and what this says is repeat s2 three times
and then store it into s1.
So when i display s1, I'm seeing 1 2 3 4 5, 1 2 3 4 5 1 2 3 4 5, three times.
And I think there's a couple of more test cases down
here that we can talk about.
And again, I encourage you to come up with your own test cases and write
some tests that really test this and exercise the code so you can be
sure that it's working correctly.
Here's another one s3 is abcdef.
And the semantics of this one just says hey repeat whatever's
on the left five times.
So whatever's on the left is abcdef.
So we're going 1 2 3 4 5.
Okay, now here's some tests with the increment operators.
I haven't done the decrement operator.
I'll leave that totally up to you.
We already have a unitary minus that makes it lowercase.
But you could do whatever you like.
Remember, we're programmers.
We can do anything.
So you can make that decrement operator behave how however you like.
So let's take a look at this mystring s is frank.
And then I'm doing an increment in this case a pre-increment.
Remember, when we have plus plus s or s plus plus all alone on a line, it
means exactly the same thing, right.
The pre the post don't mean much it's just increment s.
So in this case, you can see s is frank with a capital f.
Then I'm saying pre-increment s at this point.
When I output s, I expect all uppercase characters, which
is what our semantics say.
Over here, I'm decrementing s using unity minus.
So I'm putting it right back to lowercase.
You could make -- you could do something like minus minus s if you like
to do the same thing as the unary minus.
But again, I'll leave that up to you guys.
And now I've got result.
And here I'm saying result plus -- equals, sorry pre-increment s.
And remember, s right now is frank lowercase.
That's what s is.
So I want to pre-increment s that means I want to make s
uppercase and then assign it.
That's what the pre-increment does.
Make s uppercase then assign it.
So when I display both those values, I expect them both to be uppercase
and that's what's happening here.
And let me scroll up just a little bit right around line 54.
And in this case, we're doing a post increment.
Again, here it's the same as the pre-increment.
So I expect frank to be uppercase.
Now I make frank lower case again.
So s is frank in lowercase.
And now what we're doing is result equals s post incremented.
So what we do is first we assign s over to result, then we increment s.
So when I assign s over to result, result should be lowercase frank,
and you can see it is right here and s should be now uppercase
franc because I just moved it over.
Okay, so there you go.
That's a quick intro to the challenge.
This could be a pretty long challenge.
Take it one step at a time.
Do the operators do the easy ones first.
I would suggest to do the quality operators first because they're
pretty straightforward and then make sure that you do your star operator,
which means repeat in this case and your concatenation operator.
Do those before you do plus equal and star equal, that way you can
take advantage of them and really make the code much, much simpler.
Okay, so I'll be back.
Have a go at this.
And when you're done, you can come back.
And solution one will be the version where we've got member
methods, and solution two is the version where we've got non-member
methods or basically non-member functions global functions.
All right, so have fun and good luck.