Exam 4 Review, Quiz 4, Convert, Aggregation+composition – Cse100 Programming With C++ 11/09

Putter with a whole bunch of unique putters if I wanted to it’d be crazy but I could if I wanted to so.

It’s just when is it more convenient to use one than the other so of course there are many possible answers for this one but the one I came up with was each student at a university has.

A unique set of courses that that student has taken so that array for example let’s say is in the heap but there’s only one owner needed for this array of courses that.

Have been taken because only one student has taken that.

Array of courses so that would be a case for unique putter overshare putter one we’re sure Twitter’s better is if we have a class of.

Students then all of these student objects are going to refer to this one class because more than one student can take a class so that’s where put her is more convenient than a.

Unique putter yeah I don’t know that’s a great question let’s see put her maximum number of owners cuz I don’t actually know I guess there is yeah so I guess.

There’s this function called use count I guess that’s like the number of owners of it yeah so I guess you can I didn’t even know that you can I learned something other questions about.

The quiz itself okay so let’s get back into the material so I wanted to highlight we talked about lambdas.

Before so someone came up to me after class is like where to use lambdas and do you have to always return a bool from lambda that sort of thing so most of the time you do return a bool because you.

Use them as predicates most of the time and so here is an example of that.

So there’s this function called count if in the standard library so it might be nice to say okay I have all this data right here and I want to count how many elements are positive or are even or our prime for example in this list of numbers well we could initialize account then loop over all the elements then increment the count if the conditions satisfied but I can do this in one statement so I can just.

Say count if from the beginning of the vector to the end and then I put this lambda right here which is going to say okay I can take anything right here we know that the.

Array can only give us integers so X is an integer but it technically could take anything and.

We return whether X is positive or not so let’s just say I have this array right here which is 1 2 negative 5 and 4 so how many elements are strictly larger than three so if we run this we should expect to get three and we do so using algorithms in the standard library is a really good thing to.

Do so I invite you to look at the algorithm header and then start using lambdas as the predicate argument for some of them so like for example.

One of them is like if I have a whole bunch of elements and I want to see if all of them satisfy a condition or if any of them satisfy a condition so this is something that I use all the time so you can use the.

Olive or any of functions so you say from the beginning to the end some condition and it will return to you true if all of the elements satisfy the condition and.

False otherwise but there are other things so we just showed I showed you count if but there are many many other ones where you can supply a lambda or some predicate there and it’ll give you some a result that allows you to not have to implement it yourself which is.

Really nice yeah there’s quite a lot here okay so that’s what.

Are there any questions about the material before we I know there’s there’s one that some student told me and I’ll get to that yeah so in the slides I can’t believe no one brought this up if we go back to this slide so there’s another one like this but this is representative what is this anyone know so uh this is the assignment operator and I’m overloading it and I’m returning star this.

But what is this so let me give you an example so suppose we had a class let’s make it CP class again and then let’s just say we had a public function let’s just say void F and it takes an INT and then privately we also have a variable called X okay so we the class has a very private variable called X and this function f takes an integer which we also called X so if I like say C out X right here which X is this referring to is it the instance.

Variable the private variable or the one that was passed in the one that was passed in so if I wanted to refer to this X the instance variable and not the one that was passed in I have to say this arrow and so that so what this is is a pointer to yourself so if you have an object then if I say this on this object is a pointer to ourselves and so when I say this arrow basically what happens there is it.

Looks up through a dereference of this pointer which is ourselves so.

And then it looks up the value X in ourselves so basically it’s looking up the instance variable.

So then if I wanted to refer to this X instead then I just put normal X without this arrow that’s the only difference so what if I then said like the slide starred this if this is a pointer to ourselves what is starve this the the value of ourselves the actual objects of ourselves okay.

It’s getting very philosophical but.

The value of ourselves but basically I can’t print this for other reasons but basically starred this is the actual object itself not the pointer okay so remember this points to ourselves and star this is dereferencing that but I’m not going to test that on the midterm.

For the final so it’s just for your benefit it this is a pointer to ourselves okay other questions before we move on to interesting things okay so we talked about predicates and all that fun stuff last time so we can pass them to like sort and then remove if if they satisfy some condition and then.

More generally we talked about lambdas so basically these are just compact ways of writing a function object so basically then I can say I can make an a lambda right here which says this lambda will take some variable X of any tightness at all and then it’ll return true if X is strictly bigger than 0 and false otherwise so I can treat is positive now like a normal function call so I can.

Say is positive parentheses 5 for example or on a user input so I don’t have to write a completely separate.

Function here I can try the lambda which is pretty cool okay but another type of operator overload here which is really cool is the type conversion operator so one thing that was kind of annoying about say strings and int.

Is that we can’t convert an int to a string unless we call some function odd on it wouldn’t it be really really nice.

To say I’m gonna have a string on the left side and I’m gonna assign the number 5 to it it will give us an error if I try to do that but why would we care about this is so that we don’t have to call a separate function to take this thing on the right side convert it to the thing that we.

Want to and set it to the thing that we want to on the left side so if I have this class here let’s just copy it over.

And I’m going to comment out the int conversion operator so if I want to say int what I call it in Val X is equal to 10 and okay yeah so I know why that doesn’t work but yeah so let’s let’s just try something yeah what does that work hmm yeah.

So the publishers didn’t really look at this too carefully so ignore what I’m what I’m about to type and you don’t have to know it but so what we wanted this to show.

Is that I can’t assign an integer to an object okay so why did it work before Oh and whereas.

Where I put this thing called explicit there it doesn’t work basically what happens here is if we go back to the one before it sees oh you have a constructor that only takes an integer then the thing on the right side is only an integer I’m gonna call that constructor for you and so you don’t have to implement anything else right here I’m going to implicitly convert it for you.

Whereas if I put the explicit here that says.

You explicitly have to call the constructor you can’t have this implicit conversion so but if I uncomment this int.

Conversion right here okay so that’s where why that doesn’t.

Work okay that is weird so let’s let’s let’s dive deep into this and see why this doesn’t work so let’s just see if we try to see out X like print out this int value it won’t work okay yeah so what do.

You think the operator int actually does so what does it actually do it doesn’t have to do anything with the construction of int value object in Val but is rather how I use it okay so if I wanted to pass over X to some function so let’s just say that I have a function f which takes an.

Question can I pass an int value into this thing normally if I didn’t have this operator int conversion operator here well is int Val and int is an object and int.

In fact if I try to call F right here it’ll give me an error because it says there’s no function which which takes in an int Val it can only take an int but if I uncomment this thing it’ll work because it says okay well I have this function called F there’s no function that takes an int valve here but I see that there is one that takes an int and I see.

In the class int Val there is a way to convert this object implicitly to an int so what happens here is when I call F right here it’ll convert the int value here into an int okay.

Through this operator so we can actually see this happening.

So how do we see it happening we can.

Put some statement in here to say yeah we’re actually doing a conversion right here to really see we’re calling this int operator so if I run this it actually prints here it.

Really is doing the conversion right here from the object itself to an INT but I’m not limited to have just in trite here I can convert things to long or double or string if I wanted to.

I can convert things from any object to any object implicitly so that the user is not burdened with converting this object to an INT himself or herself right so I don’t the user doesn’t have to do the conversion here it can do it implicitly.

Is really nice so if you have some legacy code.

For example and it takes an INT a double a string for example then I can just put this operator right here in the class it’ll do the conversion.

A lot of work from having to convert it myself and then pass it along okay which is really nice any questions on how this works it’s just a different type of operator overload there’s nothing really new here okay so yeah so here’s the example that they have is that I create an interval object then here what they’re doing is that they’re assigning the int value into an int okay so here is actually converting it to an int to assign to an it.

Normally I can’t assign an object to an int but through this conversion operator I can okay so yeah so a constructor.

Of the same idea is where I can take some type and.

Then I can convert it to another type so here’s.

So I have this CC class which stands.

For convert constructor class so we have a normal default constructor or nothing new there we have well we have a construct right here which takes two arguments so that’s.

Just a normal constructor but we have these.

Constructors down here we take a single argument okay one of them takes an INT the other one takes a string so these are called convert constructors you’re taking an int or a string or anything of one type so one argument and then converting it to another type so here we’re convert in.

This constructor we’re converting an int to a CC class and in this one we’re converting a string to a CC class so why.

Would this actually be useful so remember before I told you that.

If I put string in quotes so something between two quotes is that it.

Actually a string you can tell them tell by my tone that it actually isn’t so what is something between two double quotation marks so I mentioned this last time it’s actually a pointer to a Const car so Const care star so basically just an array of characters here so is an array of.

Whatever a string no so we have a convert constructor right here which takes a cost.

Care star or really anything like that and converts it to a string okay so.

It’s the that constructor right here takes a single argument a.

Constant care star and it converts.

String is a convert constructor but the same.

Thing with the class we just made if we pass 24 in which is an int.

A CC class that is a convert constructor to but the way that we’ve been using it.

Is through the assignment here so we can have an overloaded assignment operator which can take a different type so I mentioned the assignment operator can take any type at all and convert it to the type that we want so that’s an example of.

This I have a constant care star Oh on the right side and I want to convert it to a string on the left side that’s calling the assignment operator which converts it from a classic error star to a string but the same idea here it will convert from an int to the object that we want it does the exact same thing so convert constructors are.

Nice because I don’t have to make the object first then assign it to the object that I want I can just put the value there and then use the internals of the class to make the object myself it makes the burden on the user a lot less okay so instead of having to construct a string on the right side and then assign to a string I can just put the thing that I want to be converted to a.

String on the on the right side and then let string handle the conversion for me yeah there’s really no difference but it’s just that convert constructors are just constructors which take one argument of a different type yeah.

Yeah which is why the code that we were struggling with earlier actually worked so if I comment this.

Operator in sting out and then we get rid of the function here or two we can see that even though the int operator is commented out this still works because this.

Is a convert constructor so this is why this line of code worked but if I put of course if I.

Put explicit here then this doesn’t work but don’t worry about explicit the.

Key here is that we’re doing a conversion here from an integer to an object through this convert constructor any questions on why this works or what this is okay so yeah so this is why you would want to use this so this is basically what we were seeing before so if I have a.

Function which takes a string then I can call this function with something that can be converted to a string in this case a cost care star a C string here and so we can just pass that along it’ll do the conversion for me and everything’s good same thing here so if I pass an int along here it can be converted to a CC class which is nice okay so.

A little different so these slides are about making classes actually useful doing something that other programming languages don’t have so these convert.

Constructors and overloaded operators I don’t think many other languages even have those things which is a nice thing about C++.

But a nice thing about object-oriented languages like C++ is that they have these two things aggregation and.

Composition so what is this aggregation is when an object of some class completely owns an object of a different class so for example if we have a CC class like we just made it owns an int so that’d be an example of aggregation okay.

Composition on the other hand is where the class itself controls the lifetime of the objects inside the class so why do so what is composition here so for example for vector.

Does vector on the on the internals of vector does it do dynamic memory allocation well what does it have on the back end in integer array will an array in general so when it needs to allocate more memory it does dynamic memory allocation so when the vector exits scope.

What do you think it should.

Do to the dynamic memory it.

Allocated it should delete it right so when the vector exit scope it has no need for this integer array to hold anymore so.

What happens there in the destructor it calls delete on the array that it has so that’s what composition is it’s controlling the lifetime of the objects so here when the vector is created then we call new on this this array that we have internally and then when we go to the destructor we call delete so.

That be called composition here we’re controlling when this array comes into existence and when it finally gets killed off okay so but the real key here is this part is trying to understand okay.

I’m trying to model this thing are we having and has a relationship or a different type of relationship between different objects so if I have a class a in.

A class B is it the case that a has an object of type B or something else so here’s an example of composition so we have a student info object and it has bunch of data not really important but we have a student object for example that contains a whole object of student info okay so instead of having the student managing the first last name whatever of the student info object the student object just.

Info object all at one all in one shot which is really nice okay but something a little unrelated but it makes constructing objects a lot easier so suppose we have this student info object then what we could have for example is let’s say that this student info object has a constructor which takes two strings the first name in the last thing and then maybe the last.

Four are default initialized to something else is it’s not really important what happens here so oh there’s a bug here what we could have is something called a member initialization list so what this is is making constructors a lot easier to make so this student constructor.

Right here which I said takes two strings right here what we can say is okay I have this personal data.

Object the student info object being held by a student then I’m going to construct it right here so I’m gonna pass along the first-name and lastname to this personal data object and.

Let it do the construction of the student info object for me I’m not gonna do anything with regard to the student info object it’s just gonna do all the work itself and then it’ll tell me when it’s done and then.

I can continue from there so I’m forwarding this to a different constructor the one for student info so that I don’t have to construct it myself I don’t have to say personal data dot set F name to be the one that was passed in or dot set last name to be the one that was passed in I can let the constructor for student info handle that work for me so the way you do it.

Is you declare the normal constructor here you put a colon right here to start the emember initialization list and then you put anything you want to be initialized in that way right here so in this case we only have one.