Locked lesson.
About this lesson
Constructors provide a simple way to create new objects within our code.
Exercise files
Download this lesson’s related exercise files.
45 - Intro To Classes Constructors.docx57.6 KB 45 - Intro To Classes Constructors SOLUTION.docx
59.1 KB
Quick reference
Intro To Classes: Constructors
Constructor methods allow us to pass things to our Class whenever an object is created.
When to use
Use this to pass parameters to your class in order to create an object with fewer lines of code.
Instructions
{
// Create Book Object
Book book1 = new Book("C# Programming", "John Elder", 309);
Console.WriteLine(book1.title);
}
class Book
{
public string title;
public string author;
public int pages;
public Book(string myTitle, string myAuthor, int myPages)
{
title = myTitle;
author = myAuthor;
pages = myPages;
}
}
Hints & tips
- Constructor Methods make creating objects easier
- 00:03 Okay, in this video, I want to talk about constructors and constructor methods.
- 00:08 And a constructor method is just a special method that gets called any time you
- 00:12 create a new object.
- 00:13 Any time you create an instance of your class, it gets called automatically.
- 00:17 So this is very cool because when we create an object,
- 00:20 we often want certain things to happen automatically.
- 00:24 Right here, in order to create an object, we needed four lines of code.
- 00:29 We needed to very specifically type out book1.title, book1.author, book1.pages.
- 00:35 It would be much easier if we could do this automatically from a constructor
- 00:38 method, and that's what we're going to look at in this video.
- 00:40 So, to create one of these things, you just come down here to your class.
- 00:43 And underneath here, we want to create a method.
- 00:46 This is going to be a public method, and it's going to be of our class, Book.
- 00:50 And now we just create parentheses.
- 00:52 And inside of here, we can do anything we want,
- 00:55 whenever the object first gets created.
- 00:57 So, we can Console.WriteLine out, if we wanted to.
- 01:02 And we could say, Book Created..., right?
- 01:05 I don't know why we would want to do this, but if we save this,
- 01:08 now remember back in our program, we're creating two books, right?
- 01:11 So, if we run this, we will have created two books.
- 01:14 We see the first book is created, the next book is created.
- 01:17 We also ask who the author is and we get Jeff Elder.
- 01:19 But these two things get called because they're in the constructor method.
- 01:24 So, that's interesting, but it's not all that useful.
- 01:27 What we really want to do is pass in things about our book when we create
- 01:32 the object, so
- 01:33 that we don't have to specifically define each thing with a line of code.
- 01:39 because we could have thousands of books, and that's a lot of code.
- 01:43 It would be 4,000 lines of code for every 1,000 books.
- 01:47 Instead of just 1,000 lines, if we could do it all in one line.
- 01:51 So, let me just comment out these things.
- 01:56 And how would we add these to our class without specifically doing all these lines
- 02:00 of code?
- 02:01 Well, notice when we create an object, right here,
- 02:04 we're creating a new Book object, and we have these parentheses.
- 02:09 And just like when we're calling any sort of method, we can pass in parameters.
- 02:14 So here, I could just grab, this is the title, so I'll put that in there.
- 02:19 Then we have this as the author, separate each of them by commas.
- 02:24 We do that, and the page numbers is that, right?
- 02:28 So we can do the same thing down here for this guy.
- 02:32 So I'll just do that real quick.
- 02:35 Author is Jeff Elder, who does not exist.
- 02:41 And the number of pages is 145.
- 02:43 So now, we can get rid of all of this stuff.
- 02:47 We don't have to explicitly do this.
- 02:49 And now, we have just two lines of code to create a new object.
- 02:54 Well, we're not quite done yet.
- 02:55 We have to make some changes to our class here.
- 02:58 So we need to account for those things.
- 03:00 So here, we need to define them,
- 03:01 like we always would when we're bringing in data types into our methods.
- 03:05 So, string, now we need to name this.
- 03:07 Now, title is the name up here.
- 03:11 But I want to differentiate, you could use the same name, but
- 03:13 I like to differentiate these.
- 03:15 So, I'm going to call this myTitle, right?
- 03:18 We also have a string of myAuthor, and we also have an int of myPages.
- 03:26 Now, once inside, we have to tell the program, hey, make this, this.
- 03:30 Make this, this, and assign this to that.
- 03:34 So we do that in here by calling our title and
- 03:38 setting it equal to myTitle, like that.
- 03:41 And author, we'll set that equal to myAuthor.
- 03:44 And pages we'll set equal to myPages.
- 03:47 So what happens here is, we're defining our object right here.
- 03:53 We're passing in three things, this, this, and this.
- 03:58 We're taking those, and we're saying, hey, define them as this, this, and this.
- 04:03 And then assign each of those things back into our regular attributes right there.
- 04:09 So, okay, that should do the trick.
- 04:11 Let's go ahead and save this, and run this guy.
- 04:13 And when we do it, we get the same Jeff Elder as before.
- 04:17 Because our program down here, we're saying, hey, call book2.author, right?
- 04:21 So, we could change this to book2.title, whatever, save this and run it.
- 04:25 And again, Python Programming For Fun, very cool.
- 04:30 We could also come back here, and just like we did earlier,
- 04:35 we could say Console.WriteLine.
- 04:38 And here we could say, what?
- 04:40 Let's go (title) Book Created..., and we need a semicolon.
- 04:45 If we save this, come back over here, we don't really need this guy.
- 04:50 So I'll comment out that Console.WriteLine.
- 04:53 Now if we save this and run it, every time we create a new object,
- 04:57 it will print out on the screen the title of the book, C# Programming For
- 05:01 Everyone Book Created, Python Programming For Fun Book Created.
- 05:05 That's a nice little prompt it gives us that sort of proves that the thing has
- 05:09 been created.
- 05:10 And again, as always, we could always, still from our mMain program,
- 05:14 call anything we want from our object.
- 05:16 So we can get the title of anything, book2, book1, anything we want.
- 05:20 If we run this, we get this Book Created, that Book Created, and
- 05:24 the title of this book, book1.
- 05:27 So those are constructor methods,
- 05:30 just a simpler way to create our objects with less lines of code.
- 05:35 We don't have to write four lines of code every time we want to create our object.
- 05:40 We could just do it all in one line just by then passing those things into our
- 05:44 constructor method like this, defining each thing here, and
- 05:47 then just assigning those things back to our original attributes that we defined in
- 05:52 the last video.
- 05:53 And that's all there is to it.
- 05:54 So that's all for this video.
- 05:58 In the next video, we'll look at object methods.
Lesson notes are only available for subscribers.