In this lecture we're going to explore classes a little bit more. In particular, we're going to write a class to represent a cash register. Before we get there, I wanted to show you a Canadian coin, this is $1.00 dollar, it's called a loonie, I don't know if you can see but, there's a picture of a loon on it, that's why it's called a loonie. We also have a two dollar coin called a toonie, which has a bear on it on one side and the queen on the other. So, loonys and toonies are going to play a part in the lecture that we're going to do today. Here is code that uses a type called cash register. Here we created a new cash register, and it has 5 loonies, 5 toonies, 5 five dollar bills, 5 ten dollar bills, and 5 twenty dollar bills. That comes out to $190.00 dollars. So, we're going to print whatever the total value is in the register. Then we're going to add three toonies to our cash register, and then remove two twenties. Finally, we'll print the total amount of money in the register. $190.00 dollars, plus three $2.00 dollar coins is $196.00, take away $40.00 dollars, and I end up with $156.00 dollars in my register, so that is hopefully the answer we will see when we print the total after we have done these modifications to the cash register. The problem is that when I try to run this, I'm told, hey, cash registered is not defined, it's a name error, there's not such thing yet. We're going to have to define this new type ourselves. In order to define this type, we'll write a class cash register. The init method with underscores, is the method that is called in order to initialize new cash register object. Because it's a method by convention, the first parameter is called self. Self will refer to the cash register object that's being initialized. We also are going to be passing in the number of loonies, toonies, fives tens and $20 dollar bills. Here's an example. This creates a cash register object, and then calls the inet method, passing that object in as the first argument. After this object has been initialized, we hope that it has a variable loonies that refers to the number of loonies in the cash register. Similarly, we hope that it has toonies, fives, tens, and twenties, as variables inside the object. The first parameter, self, refers to a cash register. The rest of the parameters refer to ints. Method init has no return statement, and so it returns none. If I have self.loonies on the left hand side of an assignment statement, that assignment statement is going to create a variable loonies, inside the cash register object that self refers to. This assignment statement is what creates variable loonies so that register.loonies is valid and refers to 5. We create the rest of these variables in the same way. When we create a cash reister object, we call that an instance of cash register. We also call all these variables instance variables, because they exist inside an instance. Here's a quick description. Let's try running this. Here we get a different error. We have created a cash register object successfully. But now we're told that that object has no attribute yet total. Even though we get this error, our class cash register has been defined. In fact, we've created a cash register instance, that instance has variables loonies, toonies, fives, tens and 20s. Here are examples of examining those instance variables. When I type register and a dot and I wait a second, up pops the list of items that are inside the object that register refers to. So all of these variables exist inside that object. I can actually just pick one of them in order to examine it. Now let's make another cash register and have variable, register 2, refer to it. The cash register that registered 2 refers to, also has all those instance variables. Register 2's number of 20s is 7. We can reassign the number of 20's. Our first cash register still has it's original number of 20's, we have not modified the first one, we've only changed the contents of the second one. Let's take a look at the memory addresses. As you can see, the two cash register objects, exist in different places in computer memory. Let's go back and continue working on our class so that we have methods get_total, add, and remove defined. Here's the header for method get_total. Again, the first parameter is self, it will refer to the cash register whose total is being asked for. We create a cash register with these values. We expect the total to be $190.00 dollars. Self refers to an instance of cash register, yet total returns an int. This function will return the total amount of cash in the register. Each loonies is worth $1.00 dollar, each toonies is worth $2.00 dollars. Each five dollar bill is worth five dollars, ten dollar bill is worth ten, and every 20 dollar bill is worth 20 dollars. When we learn our program again, register.gettotal produces 190 just like we wanted. And then we're told there is no attribute Add, in a cash register object, let's define one. Method add has self as the first parameters, as always, and then the number of items to be added and the denomination. For example, we might call register.add, and ask to add two Toonies to the cash register. Means that when we examine Toonies, we should see seven. We can also add one to the tens, and then when we examine that we should see six. Self will refer to a cash register object, count to inet and denomination to a string. This method doesn't return anything. And then we'll add count items of denomination to the register. And we're going to spell out that the dem, denomination must be one of loonies, toonies, fives, tens, and twenties. We're going to write an int statement to determine the new kind of denomination that is being added to the cache register. If we're adding loonies, then we'll add count to self.loonies. Likewise, if we've been told to add toonies, then we will increment toonies by count. Copying and pasting to save time, we will deal with fives, tens, and twenties in the same way. Method removed is going to look remarkably like method add. So we will copy and paste it. We need to fix the name, the description and the doc tests. We start with five to reason, remove two of them. We expect there to be three. When we start with five ten's and remove one of them. We expect there to be four. Rather than adding to the appropriate instance variable, we want to subtract. Scrolling to the top, we see that we're defining a class. The indentation indicates that underscores init is a method inside this class. We also have a getTotal method, and an add method and a remove method. Looking at our main program, that's all the methods that we use. When we run this, we see that we get exactly the values that we were hoping for. We've defined a new type, and we can use it just like any other type that comes with Python.