 Welcome to the lecture on objects and classes. So far, every time we learned something new, all we did was build upon what we learned before. And this new topic of objects and classes is no different. If you think of it in that terms, you're just building on what you already know. Make sure you know what we've done so far and this additional piece of knowledge and the additional tool in your arsenal of writing programs using the C++ language will not be difficult to learn. Data types, we know about data types, we know how to use them, we know how to declare variables to be of a particular data type and we know what that does for us. Objects and classes are no different, literally no different because the fact of the matter is that in C++ a data type is just a class and a variable is just an object. The difference is that for the most part, up until now, we've been using classes that are built in or have been predefined for us instead of creating our own. We've been declaring variables to be on those data types when really what we've been doing after we learned this lesson and we'll know that what we've really been doing is creating objects to be instant associations of these classes. Yes, instant associations, very big word. I'm going to leave that in here so you can see that it's okay not to say things exactly right. The same way we've been working with data types in float care, we're creating objects on those data types like declaring N to be of type integer. Now what we're going to be doing is creating our own data types. We call them classes and we're going to learn how to do that. And then once we've done that, when we write main or some other function in our program, we're going to be creating or declaring objects to be of those new types. So if you just think of everything you already know about data types and transfer that knowledge now to classes and everything you already know about variables and transfer that to objects you'll realize that you already know 50% of what we're learning in this lecture. Most of the rest of it is just syntax. The beauty of this is just like the wonderful thing about being able to create our own functions. We're extending the language. We can customize the language for our own particular application. Create classes that are unique that are specific to our application instead of only having to rely on the built-in classes for the primitive types or even for the more structured types like strings and arrays. We're going to see how that works very soon. When we learned about data types, we learned that a data type has two portions, a static portion and a dynamic portion. The data type describes what the data looks like, that's the static part and it also describes how the data behaves, what operations that we can do, that we can perform on data of this type. That's the dynamic part. And of course, classes, since classes are data types, are exactly the same. In a class, when we define our own class, we're going to define what objects in your head think variables for now, what objects of this class will look like. In other words, what characteristics does it have? And also, what operations can we perform on objects of this class? Or in other words, what are the methods that objects of this class have to work with? So the characteristics, the what does it look like part, are called attributes. Attributes are member data of a class. And that's very similar to a record and it's very, very similar to a struct, which is the way we implement records in C++. But then in addition to just the static data, the way the object of this class looks, we've also got the dynamic portion, how it behaves. Those are the methods and methods are just functions that belong to a class. Every time we declare an object to be of a particular class, there is space set aside for the attributes, the data that it should contain, and for the member functions. It has all of the member functions that it needs in order to behave as a member of this class. One way of looking at this is that a class is just a description of something that might be called into existence at some time. We didn't think of it that way, but a data type is exactly the same thing. A data type like int might describe integers and what we can do with integers, but they don't exist until we have a variable declaration to be of type integer. And that's exactly the same thing with classes, because classes and data types are, as we know, exactly the same. So the class is just a description. It's like a template. It's like a blueprint. Nothing really exists until a declaration, until we declare an object to be of that type, to be of that class. An object does exist. It takes up space and memory. It has attributes. It has behavior. It maintains a state. The state is the collection of values in the variables, in the attributes, the member data. We say an object is an instance of a class, which is just another way of saying the same thing. An object is real. It takes up space. A class is not real. It's an idea. It's a notion. It's like a blueprint of a building as opposed to the building itself. As we saw, a method is just a member function of a class, and all objects of the class will have those functions. That's the way objects of that class behave. That's the behavior of the class. How do these functions get called? How do they get invoked? Actually, it's called sending a message. A message is a request sent to an object saying, please call that function that you own. That's a part of your definition. I'll give you an example. If you were writing a payroll program, you might have specified an employee class. You might have an object, an employee of that class, call that variable employee1 or employeecurrent, and that's a variable or an object. That object has all the data attributes, potentially, that any object of that class could have if they have values. Whatever the values of those attributes, that's the state of that object, and it has several functions. Now, let's say one of those functions is a Calculate Pay function. When some other part of the program, let's say Maine, has a call to that method using, we'll see the syntax shortly, to that particular method of a particular object. It's the same as telling this employee object, hey, employee, calculate your own pay. Over the next few slides, we're going to develop what we need for a new class that doesn't exist yet unless we put it into our program called Fraction. What does a fraction have to have? Well, a fraction has to have two integers, one for the numerator, one for the denominator. A fraction might have to have several functions to work with that, with the fraction. It's kind of nice to have a data type called Fraction at your disposal. We've got integers, we've got float, we've got double, we've got characters, we've got strings, we've got arrays, structure types. Well, how about a type fraction? That's what this class does for us. There you see the class definition, that's the interface, it's the what. We don't see the details of how this was implemented. We don't see exactly the code in the different functions, in the member functions, but we shouldn't have to. If the class was designed well enough, including good names for these methods, we should be able to figure out what they do and how to use them. So let's take a look at that. There's a private section and a public section. When you first start writing programs with objects in classes, you may want to start out by putting everything in public just to see if it works. But that's when you're really a newbie. What we really typically tend to do is we start out putting all the data, the attributes, into private. We declare them as private. Private and public are access specifiers. What kind of access do other programmer users have to these pieces of the class? And functions are considered public. The third access specifier that we're not going to get into, and that's protected, that's only used for inheritance, and we're going to do inheritance later on in the advanced course. In fact, this slide, this section is titled defining a base class. A base class just means this is a class on its own. It's not created to inherit from anything else. We don't have to worry about inheritance. At this stage, if you don't know anything about inheritance anyway, saying defining a class would be about right. So let's see what we have. We've got two private data objects that are members of the class called numerator and denominator shortened to nom and den, and they're both integers. We always try to call our data private to build a wall around it. This is the principle of encapsulation and information hiding or data hiding that we're going to learn more about later on, but we're trying to protect the data from inadvertent change. Well, sometimes we want to work with the data, and that's where the member functions comes in, the methods. We get these methods can be called from other functions, from elsewhere, from other functions, from other objects, because they're public. The data can't be accessed. The only way anything private can be accessed is by objects of this class. But anything that's in public can be accessed by other objects and by other functions such as main. Let's take a look at main and see how that works. Okay, now we're playing with this class. Here's main. We still don't see the implementation of the class, the actual code of the functions that belong to members of this class, to objects of this class. That's okay. We shouldn't really need it just yet, but we will take a look at it pretty soon. So the first thing that happens in main is an object x is declared to be of class fraction. It looks exactly like any other variable declaration you've ever used, except that we haven't seen a data type fraction yet. The reason we haven't seen it is we just created it. A fraction is a class, x is an object, and the declaration of an object to be of a certain class type is exactly like the declaration of a variable to be of a certain data type. Once we create x, x exists, it has everything that any object of the fraction class will have. It'll have two pieces of private data, a numerator and a denominator, and it'll have those methods that we saw before, one of which is a sign. Now, since the data is private, we're using a public function in order to assign values to the numerator and to the denominator. And we're using the dot operator, like we saw in structs, in order to burst open the x object, the structured x object, and be able to get to to access one piece of it, in this case the assign function. So we've got x dot assign, and then the parameters and parentheses, constants, so they're obviously value parameters, the constant 22 for the numerator and 7 for the denominator. We send something to C out. You can see your output, x equal, and then just the display, x dot print will just display the fraction, 22 slash 7. But then the next thing we do is actually convert. So the x dot convert method converts the 22 divided by 7 into decimal. Oh, that looks familiar. Maybe we should have called it something other than x, like, oh, let's say pi. And then a little bit later on, we invert the fraction, which we wouldn't be doing with pi, and now it's 7 over 22. And here's the code for those methods that we saw before in the class definition. We've got the assign function. It has two parameters. They are indeed value parameters. And all they do is take in values and assign those values to the private data of the object. We've got the convert function, which returns the result of the expression numerator divided by denominator. That's what we thought it would be. We've got the invert function, which is just a swap. It swaps numerator and denominator. And then we've got the print function that sends things to C out. As we've seen, we can specify pieces of the class in the class definition as public or private, or, as we didn't see yet, as protected. For now, we're just going to be using public and private. And private members are only accessible by functions that are part of the class. So only objects that are declared on this class can access anything that was declared as private with the private access specifier. We'll continue this lecture in the next set of slides.