7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial
FREELessons: 30Length: 4.8 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

6.3 Classes vs. Structures

Now that you know how to create both classes and structures, let's look at some of the fundamental differences between them. In this lesson, we'll also look at some basic scenarios that illustrate why you might choose one over the other.

6.3 Classes vs. Structures

Sometimes it differs a little bit from language to language and compiler to compiler. But at the very basics, at the very fundamentals, the difference here is that a struct is a value type. And a class is a reference type. Now whoa,, whoa wait a second. What in the world is he talking about? Well, it's a very simple concept and you've kind of been dealing with value types quite a bit. And a value type is nothing more than a very simple type that when you create instances of it and you assign it to other instances, then you get copies of that particular type. So what does that mean? Well, let's go ahead and we're gonna comment this out really quick just so I can show you a very simple example. So if I were to comment all of this out. And we're gonna start with one of them, the most basic and simplest value types to work with, and that is gonna be the integer. So let's say we wanted to create some variable that we'll call x, and we're gonna set that equal to 5, and then we'll create another variable called y, and we're going to set this equal to x. So, by definition, a value type, every time that you create an instance here. Which I've done here, x is gonna contain a value type. I have actually created a copy of the value five and have assigned it to the x variable. Now the fact that I've come down here and I've assigned a variable called y equal to x, I've actually created a copy of x and put it into y. Which means that if I were to come down here, and say I wanted to print y. And then if I were to change x and said x is equal to 10 now. And then I print y again. Because I've created copies, because these are value types they create copies. Even though I've changed x which ultimately you would think that maybe y was maybe pointing to. You'll see here that it isn't. So the basic idea there is that if I were to create a function that was called say like change x and if I were to pass x into my function and change it within the body here, so when I call change x, It's actually going to put a copy of x into the body in here so that if I were to change x to be something else, x is equal to 5,000. And I were to come back outside of the function and check again the value of x. It would still be five. So that everything that happens within here is happening to a copy of x and not necessarily the value x itself. Now the difference between a value type and a reference type, this being a value type, if I were to take a reference type and do a similar operation and I were to say in this case up here, if I were to change this back to be a class. And if I were to now get rid of all these x and y bits down here. So I'm going to create another var here, we're going to call this z and I'm going to set that equal to c. Now the difference here between the value in the reference type is that now instead of getting a copy of c put into z, I'm actually having two variables, c and z pointing to the exact same object. So now let's see how that's going to work. So I can come down here and I'll just print c.prop1. And then if I were to change that, say c.prop1 is now gonna be equal to 1000. And then if I were to print z.prop1. Go ahead and save this and run. You're going to see here that the first print here printed exactly what I would expect because I passed in 20. So then I change the prop one value that's assigned and associated with c. Then I change the value of that property to be 1,000 and then I print z. Now I didn't touch z. I only touched c. But because these are reference types because these are classes and I do this type of an operation, I'm only dealing with a single object here of my class and I have a bunch of different variables pointing to it. Now if I were to make this change and say that this is going to be a struct, which like I said is a value type very similar to say like an integer. If I were to save this, and I were to run it, you're gonna see that both are gonna spit out 20 because I did not explicitly change the value of z, directly or indirectly. So kind of an interesting concept there. So once again, you see that that might be relatively interesting at least. But what's the difference and why should I care and maybe when do I choose one over the other? Well when you're trying to choose the difference between using a struct or a class, I'm going to say typically speaking under normal circumstances for me. I'll say it's a judgment call, but I'm gonna say about 90% of the time I'm using classes. Not because I like them more. But because I'd simply typically lend myself more towards using object oriented programming, and one of the foundational characteristics or things that you do when you're creating these objects in the realm of object oriented programming is using inheritance. And we'll get to that in just a second but generally speaking for a structure, if you wanted to choose a structure then you're probably going to be creating some sort of type that has very simple data values. Maybe you're only dealing with a couple of properties and a function or a method and maybe that's just about it. Or maybe you're only going to be using this, and you want that functionality of this being a value type. You want every time you use this for it to have copied values, and you don't want to accidentally change one instance value because you've modified some other instance value or something along those lines. And those would be situations where you'd like to use a struct. And as far as classes go like I said, I feel like when I'm starting to create larger pieces of functionality. And typically things when I'm going to start to use the role of inheritance. That I'm going to lean towards a class on the world of classes I have this opportunity to create these inheritance structures that I might want to do and so what does that mean what does this inheritance bit mean. And then the rest of the whole world of object oriented programming was a let you take a look around and see what you can as well as we're gonna start to work on another course that will touch on object oriented programming in Swift a little bit more deeply. So let's just say I wanted to start to create a very simple payroll system, and I was going to have a bunch of different objects or classes within my system that are going to have some very common characteristics. So let's say I wanted to model the concept of an employee, and I want to model the concept of a manager, and so on and so forth, that individually could have some very different properties and functionality but at their base have some very common functionality or common properties. So let's just say one of those things could be the concept of maybe a first name and a last name. So if you create a simple class it will just call this that has no superclass. And it's simply going to have a first name. That we'll just set to nothing and will save our last name is gonna be set to nothing. And so now this is gonna define the basic characteristics or the basic functionality of a person. So now I can create another class, we could call this employee, which, once again, is going to be of a type of person. It's going to contain some information here. And I will have access to the FirstName and LastName, but not actually have to define it within the employee class and create a bunch of duplication. So within here all I could worry about maybe is an identifier that would identify an employee and we could say that this is going to be equal to a string. And then when I create a new instance, so I could say varEMP Is going to be equal to an employee, you're now going to see that I can go into employee and I get access to first name, last name, and identifier. So I get access to the properties and the functionality defined in this base class, or this superclass, even though I didn't have to define them in my employer when I created that way and so I could continue to create different layers of inheritance here and maybe break these things out into different pieces of functionality that are encapsulated in different classes. And I could really get very elaborate here. But the basic concept is just that. So if I feel like I'm going to be creating some sort of application with some depth of functionality that I'm probably going to want to utilize this process of inheritance to cut down on some duplications then classes are gonna be my way to go. So there you have it, that's the basic concept behind classes and functions, and like I said, there's a lot more information out there on the developer library at developer.apple.com. So you can definitely get a lot more there, and this is just to kind of whet your appetite and give you the basic concepts behind classes and structures.

Back to the top