Object Oriented Programming: Objects and Classes | C# 101 [16 of 19]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
>> We're learning about C Sharp 101. I think we've reached your favorite part. >> We have. Object oriented programming. >> There it is. >> Yeah. This is the time in your C Sharp curriculum where you know you've made it. Now we get into the serious bits of C Sharp. We're going to go over inheritance, we're going go over objects. It's going to be great. >> Awesome, cool. So what I want to do is I've got my exploring object oriented programming documentation up and I'm going to again, snap that off to one side. We just finished our Fibonacci sequence. You could go and check that video if you want to. But I think we should just start from scratch. It's file new project time right now; don't you think? >> Let's do it, yeah. >> Okay. So in Visual Studio, I'm going to say File, New, Project and we're going to basically get rid of the previous one and start from scratch with another console apps. So I'm going to get ConsoleApp and we'll call it MySuperBank. All right. We're going to make a new bank, and we'll hit Create, and then it's going to ask us if we want to save our old project. I don't care about that old project. We're going to toss that. We're starting from scratch and we're back at Hello World and the only thing that's changed, that it's MySuperBank which is awesome. >> Better than other banks. >> It's way better. Other banks suck. Our bank is the best bank. So what we want to do is think about object-oriented programming and make a bank account. Now what is object-oriented program? What are objects and why are we orienting around them? >> So objects are basically a way that all computer languages can learn how to mimic the real world. So in the real world, we organize a lot of information in various different ways. With object-oriented programming, we can say, "Hey, a person can have a name, and an address, and a hair color, and that kind of thing." So people can have different properties and it's just a way of grouping all information. >> Right. I mean, we're orienting around objects. Like I can make a mouse object or a bank object. Then when you talk about inheritance, we can have animals and there are types of animals. >> Yeah. >> You can have a hierarchy just like you have in reality. >> Yeah, definitely. >> So it's intended I think to be intuitive. So hopefully, you won't find this too intimidating. Okay? So according to this tutorial, they want us to make a bank account. So then presumably, we need a bank account and it has certain requirements. What is this list of requirements? >> Right. So it looks like we need a 10-digit number that will uniquely identify the bank account. So this is one of the properties that will be in our bank account. It need this string for the name or the name of its owners. It needs a balance. It needs to be able to accept deposits and withdrawals. So these are I guess capabilities that'll be able to have, so they're not like a single unit of data. It's actually like a function that will be attached to that. >> That's a good point. These first three are really characteristics or properties. >> Right. >> These seem like methods or things that it can do verbs and then what are these two? These are kind of like rules. >> Yeah. So it looks like the initial not balanced must be positive. So I guess when we're creating not balanced number, we need to make sure that it's above zero and withdrawals can not result in a negative balance, which makes a lot of sense. That would not be a good bank system. >> This is very typical of object-orientation, right? There's going to be some properties like things, cars have color and banks have balances. Then there are the stuff they can do. Cars can drive, banks can take deposits, and then there's rules that like well, you're doing those things, don't exceed this speed, don't crash the car. >> Yeah. >> So we need to go and start thinking about our bank account type. >> Yeah. >> Okay. >> So a lots of things that sort of describe the behavior and the design of what this object will look like. >> Now so far, we've been putting everything in program.cs. I feel like bank account maybe should go on another file? >> Yeah. Let's make a bank account class. >> On the right-hand side of Visual Studio here, there are these little fly out tabs and when you click on them, different things fly out. Like I can go to GitHub or whatnot. I'm going to click on Solution Explorer and push this little pin and this is going to let me see the files inside of my program. I have one called program.cs and in fact, I can open it up and see my Main. >> Oh yeah. >> So that's actually not a file, is it? >> Yeah. >> That's showing me the structure of my program. >> Yeah. So to list out the classes that you have within that file as well as the methods in each of those classes. >> So I'm want to right click using the right mouse button and say, "Add". I could say New Item and get a whole list of item or I can say Add Class. >> Let's do that one. >> All right. >> Add a class. >> Let's do Add class. So I need to add bank account. This might be scary but this is just a list all the great stuff that you can add in the future. >> We're just going to say, "Add Bank Account." That seems like a fair thing to do and you look, it went into another file and it has the same namespace, that same bucket. >> Right. >> All right. So look at this. We could copy paste or I could type in public because it's available to the public, string number and numbers only gettable. This is called a property. I can get numbers but I can't. Like once it's been assigned, I can't set the number. >> Right. >> But I don't want to keep typing that. That's tedious. >> Okay. >> That's tedious. I'm going to do this. Remember how before we typed a [inaudible] and hit Tab? >> Uh-huh. >> Check this out. Prop tab, tab. >> A code snippet. >> Code snippets. Tab, tab. >> Okay. >> Owner, enter, enter. Prop, tab, tab. Decimal, tab, tab. Balance, enter, enter. Look at that. >> Man. >> That great. >> You could type it the old fashioned way. >> No. Nonsense. I want the computer to do the work. >> Tab, tab is your friend. >> Tab is great thing to remember. Anytime you're in Visual Studio, tabbing will probably get you something, in worse case scenario, it gets you whitespace. >> Right. >> Then let's grab these two functions and then maybe you can talk about those. >> So these were sort of the actions that were listed out in their requirements. So it needed to accept deposits and make withdrawals as well. So instead of properties, these are going to be methods. >> Sure. >> Everything that you see in this class, these properties and these methods, all of these are members that are just one part of this class. >> Right. When we made them all public, public, public, public, that means that other applications and other classes within our application can look at those and talk to those. >> Yeah. > If they were private, they would basically be secret just to bank account and no one else would know about them. >> So right now your class doesn't have public in front of it and we could also change that accessibility level. >> That's a very good point. Then you should look on the right here. You're going to get used to seeing these symbols as you move through Visual Studio and C Sharp. The number, and the owner, and a balance, you can see their types and their names and that's a nice view of what's going on. >> Yeah. >> All right. Let's go back over here and see what's going on. We've got our members, and our properties, and our methods. To open a new account, we need to make one. We need to construct a bank account. >> Right. So far, we've designed what a bank account should look like in different properties it should have. But we haven't actually made one. >> Right. So if someone were going to make a bank account, they would have to go and say, "Bank Account, it's called a constructor." Now I could make a constructor that doesn't really do anything, but you can't open a bank account with nothing. >> Yeah. Like you at least need a number. That's one of the requirements. >> You need a name, you need an amount. Exactly. >> Since the number doesn't have a setter, we should probably create it at the constructor alarm. >> Yeah. So then let me think here. We've got what? We have the name of the person. >> Yeah, that'll be one of the input things. >> Okay. Then we were doing datatypes before integers and logs, decimals. What is the right number for a bank account? I mean, I have pennies but I also have fractions of pennies. It's not an inch. >> Interesting. So we declared the number up at the top is this string, right? >> No. That's a great point actually. This number is the bank account number, which might have letters in it. So it's actually a string but the balance is a decimal. So why don't we make the initial balance? >> That's right. >> A decimal as well. >> Because you need to have cents. >> Yes. >> So it needs to have two decimal places. >> You have to have cents or whatever your fraction on currency is. >> Right. >> Okay. So here's where it gets interesting. Those are arguments. How do I get those into these properties? I basically needed to tell it about this object that I'm in this object, the one that I'm working on. >> Right. So the perimeters are being input into the constructor but nothing's actually been assigned. >> Right. This is where I think object-oriented programming gets confusing to people because this is a description of what a bank account could be like but it isn't a bank account. It's like a template for a bank account or a description of a bank account. >> Or like a form like we will require all of these things but we haven't filled out the form yet. >> It is a form that we're filling out. That's a great way to look at it. >> So these are the form fields and I need to make sure that this information in the constructor when I make the form for the first time goes into the form fields of whatever this current account that we're on is. >> Yeah. >> So I'll say this. The owner of this account will be the name that we just passed in and then this balance will be the initial balance. After I've done that, that goes away and it's thrown away because I copied it into here. Does that makes sense? >> Yeah. >> But look at this. It says you don't even need to say this. I like to save this because it means this object not that object. >>> Okay. >> But they're saying it doesn't really matter. It's kind of like var. It's one of those things where if you like the way it looks, use it and if you don't, that's cool too. >> Cool. >> Okay. That's not the awesomeness Bank Account but at least I could make it and test it. So how would we do that? What would be the Bank Account Hello World? >> Yeah. How do we call this? So now that we have an object, when we have the template for it and everything, we need to actually input data. So let's call this, how about in our program? >> So back over in Hello World. >>.cs. Yeah. We can initialize. Remember, Main is like the main entry point for any application. So when we run this application, we'll just create one bank account. That's the goal. >> Okay. So var account equals and then we knew something and we'd never done this before, have we? >> No, we haven't. >> Because we have always been saying like zero. >> Yeah, something like that. >>> We've never knew something. Some people call it like newing it up. Like as if newing is a verb. >> Interesting. >> Yeah, you've ever heard that? You new up something? >> New it up. >> So let's new up a bank account and then when we pass in, we'll say Kendra's money and look at that. It actually gave you the shape of that. It gave you the help and if I had comma. >> Yeah, because we're calling the constructor right now. >> Yeah, look. >> That we just defined as needing the parameters of a string and the decimal. >>> Right. That's why you use a Visual Studio. If I use Notepad, I could do this but I wouldn't get all of this help. >> The hints. >> I wouldn't get all the spell-checking, right? >> Yeah. So Kendra has a $1000. >> Maybe just add a few zeros to that [inaudible] >> Congratulations, $10,000. >> Excellent. >> Very nicely done. Then Let's do a Console.WriteLine just like before. So this Hello World would be different. This is the Hello Bank Account world, isn't it? >> Right. So let's go into our string and I'm going to put that string interpolation thing there, that's important. >> Yeah. >> So we'll say Account and then something was created for Kendra with some money. Okay. So let's go and dig our information out of our object there. So what does it account., see how I didn't get my IntelliSense unless I spelled it right? >> Right. >> So account number was created for account owner with account balance. That's cool. Does that look like a decent banking Hello World? >> Yeah, I think we can call that. >> All right. Let's give that a try and see what happens. Look at that. >> Account blank. >> Account blank was created for Kendra with $10,000. So when the next video, we're going to figure out what a good account number would be, how to make one because we're going to have lots of different accounts and we never even figured that part out. We only did my names and balances. >> Right. >> So that's just us defining an object and making an object.
Info
Channel: dotNET
Views: 88,166
Rating: undefined out of 5
Keywords: .NET, dotNET, dotNET Core, .NET Core, C#, object oriented programming, objects, classes, dotNET core, C Sharp, tutorial, getting started, learn C#, learn csharp, C# for beginners, learn C# programming, C# course, .NET developers, hello world, programming languages, C# training, how to learn C#, C# interface, C# basics, C# programming, learning C#, getting started with C#, what is C#, dotNET developers, .NET C#
Id: TzgxcAiHCWA
Channel Id: undefined
Length: 12min 56sec (776 seconds)
Published: Mon Sep 23 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.