Hi, for this segment we will be talking about classes in C++. So a class. Is really just a group of data objects and functions. And so we've actually been using classes already in these examples that we've been looking at. The vectors are a class in C++. And you'll notice it's a data object that can store information and it also has several functions that go with it. All right, so what we will be showing you in this segment how to create a class in C++. All right, so there is a basic structure to a class and you would start defining the class in the program before the main function. All right, and here's the basic structure you actually write the word type in the word class and then the name of the class. And then open curly brace and close. Now, as opposed to function we actually put a semicolon at the end of that last curly bracket, okay? Now, within here we're simply declaring what objects and what functions are going to be included in this class, okay? And some of those might be public. So by public, I mean that someone can access those variables from within the main function itself. Some of them could be private which means that you can only access those variables within class functions, all right. So these would be variables, class variables. And functions. Functions can also be public or private. So if a function is private, it means you can only call that function within the class, within other functions of the class, okay. All right, so let's look at that in our code and as an example, so a class like we said a class could be a vectors, it could be matrices could be a class. But it doesn't have to be a mathematical object again. And so in fact it can be pretty abstract. So for an object for an example, class here I'll use the example of triangles, okay. So my class name will be Triangle. And. In my public, some public variables that I would have there or I would have a double defining holding the value of it's space and the value of it's height, okay. Now, what are some functions that I might want like the example we used before, I might want a function that would calculate the area. And so it's the same set appears that I declare the function that here's the output is a double. The function name is area. And as input I actually don't need any inputs because since area is a class function, it already has access to all of the class variables, okay. So it has access to base and height already. All right, so I don't need any inputs. There is a another function that we need, and it's called the class constructor. So a class is just a general description or a group or a type, right? But remember for example, with vectors we would create a particular vector object. So when you actually instantiate an object of a particular class, you call what's called a constructor function. And for the vectors, the constructor it could either take nothing, in which case it created a vector of size zero. The constructor also accepted a size. So we could say vec open parenthesis closed parenthesis of ten and that would create a vector of size ten. The other constrictor for vectors was to specify size and the value in each element, okay. So the constrictor always has the same name as the class itself. So for the constructor for a triangle, we actually want to input the base. And. The height, okay? I'm going to do b and h just to distinguish it from my class variables, base and height, okay? There's also something called a destructor and it has a tilde followed by the class name. And it's used to perform any operations once the class object goes out of scope. So for example, if you declared the object within a four loop at the end of the four loop the destructor will be called. Now, usually we don't need to put anything in the destructor. Occasionally, if you're dealing with pointers you have to make sure all the pointers are cleaned up so you don't have the memory leak. But in this case we're not going to be actually using the destructor, okay. All right, so now there I've declared my class. But now, I still need to define these two functions, triangle, and area. I'm going to rearrange this just a little bit and group all of my functions together just to be clean, get my functions together and the variables together. You'll notice I made them all public. So now, let's declare, let's define these functions, all right? So defining a function for a class is really similar to defining a regular function. But we need to specify that the function belongs to the class that we’re talking about, okay. So to do that first, we start with the name of the class which is Triangle. Then use the double colon and then the name of the function itself. So the constructor has the same name as the class, we have Triangle. Notice that the constructor doesn't have any return arguments, all right. So that's a little bit different. But we still have all the inputs there when we're defining it. And open and close curly bracket, okay? Now in here, what I want to do is assign the value of b, which is an input, to base so I simply say base = b. B is in the scope because it was one of the inputs. Base is in the scope because it's in the scope of the class, okay? And same with height. So I'll say height is equal to h. Now, the constructor is complete. That is all we needed to construct a triangle and to set up all the variables in the class. Now, I will define. The area function. So again, I do triangle :: area, to specify that this is the area function related to the triangle class. We output a double. Notice that this should match the function declaration that we had earlier, okay? Sorry, this function declaration up here. So I'm returning the double of the functioning of area and no inputs. And as before, I return 0.5 or one half times base times height. Again, base and height are class variables so their scope includes all class functions, all right. So I don't need base and height as an input, okay? So that's enough to demonstrate this simple triangle class. All right? So now, let's actually define a triangle object. And just like with doubles ints and vectors, we can declare more than one at a time. So we have tri1, and I'm going to construct triangle one with a base of 1 and a height of 5. And triangle two, I'll do a base of 3 and the height of 2, as an example. So there I've created two triangle object, they're each instantiation of this triangle class. And now, I can use the functions. How do we use it? We use these class functions in the same way we did with vectors. So if I want to calculate the area of triangle one, I would do tri1.area ( ) to let it know that it's a function. Okay. Standard end line. All right. Now, let's run this and make sure I didn't make any mistakes. Make run. And it says, new types may not be defined in return type. And that's because remember when I was on the slide when I wrote out the template for a class, I stressed that there was a semicolon at the end of the brackets and I hadn't put that in here. Let's see if that picks it up and it does. So again, the class declaration. It's a little bit different from function declarations because we need the semicolon afterwards, all right. And so we see that it did indeed print to the screen, the area of triangle one, half the base times the height is 2.5, all right. Now, what does it mean that the variables are public? How do we access those within the main function? Actually do it in a similar way. We do tri1 for example dot and I'm going to say base now, I'm now going to put the parenthesis on remember I said before the parenthesis showed that it was a function without the parenthesis it's looking for a class variable of that name and so I am going to redefine the base. I will let it be 3, okay. And I will call again, after that the same triangle function, the area function. Let's see if that does what we want. Yup, and so in the first it still gave us the area of 2.5, but then when we change the base of triangle one to be three then of course that changes the area. Now, maybe there is a class variable or a class object that you don't want someone in the main function to be changing. And if that's the case, then you would make those private. So let's say we only want the triangles base and height to be defined when the triangle object itself is defined. We don't want it to be changed later on by a user. So we would make base and height private, okay? And so now, we can still use area. Area is public but base and height are private, so if I try to run this, it should give me an error. And what does it say? It says error within this context with tri1.base. There's an error there and above that it says, triangle base is private. Okay, see we're not allowed to access that within the function itself. Okay, now there are a lot of other topics related with class. There's friendship, there's inheritance, there's in addition to being public and private there's protected. I'm not going to go into all of these details with classes. But if you are interested in that again go to c++.com and their tutorials they have some really good information on classes. Now, in our In our coding template for the coding assignments, we actually will be creating a finite element method class that has specific functions related to the finite element method and data objects. So we would have objects such as a global force factor or global stiffness matrix. We would have Class functions excuse me, such as our basis function. Or you might have a function for the assembly process or a function for outputting results. Okay, so this class structure, the structure itself, will be fairly simple. But we will use several variables and functions that led into the finite variable method. So now, we have our class structure here, all in the same dot cc structure here. We can actually separate the class structure into a separate header file and then just include that header file within our main.cc. So let's do that. Create a new File, and I will save it as triangle.h. And I'll save it in the same folder example. And all I'm going to do is copy this class declaration, including the function descriptions. I'm actually going to cut those from n.cc and paste them into triangle.h and save that. And n.cc I'll just do pound include triangle .h. I use the quotes just to specify it's a header file that I created myself, as opposed to a standard header file. All right, so now we go back to our terminal, and we should be able to make run. And it will run correctly just as before giving the exact same outputs and running in the exact same way. So by doing the pound included as if that whole .h file were just written within the main.cc where I included it. Okay? So this will actually be how we give you the coding templates for the homework assignments. You'll have a main.cc and a fem.h header file for each of your assignments. The bulk of the coding will be within the fem.h where you're defining this fem class structure and all the data objects and functions associated with it. And so we'll stop here and this ends these segments on our review and introduction to C++.