{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Objects and Classes\n", "\n", "Watch the full [C# 101 video](https://www.youtube.com/watch?v=TzgxcAiHCWA&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=16) for this module." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Object Oriented Programming\n", "\n", "Objects are a way to mimic the real world in coding. If you take the concept of a person, they can have a name, address, height, all of these properties that change from person to person. Object oriented coding packages that type of information, so that you can easily make a person with all those details. There are lots of stuff you can do with objects, but for now you can start with the basics." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Making a Bank\n", "\n", "Over the next few modules, you'll make a bank account object with these attributes:\n", "\n", "> It has a 10-digit number that uniquely identifies the bank account.\n", ">\n", "> It has a string that stores the name or names of the owners.\n", ">\n", "> The balance can be retrieved.\n", ">\n", "> It accepts deposits.\n", ">\n", "> It accepts withdrawals.\n", ">\n", "> The initial balance must be positive.\n", ">\n", "> Withdrawals cannot result in a negative balance.\n", ">\n", "\n", "You can categorize these goals:\n", "\n", "- **Properties**: details about the object (how much money it has, the name of the account).\n", "- **Actions**: things the object can do (accept deposits and withdrawals).\n", "- **Rules**: guidelines for the object so that it doesn't try to do impossible things (make sure that the account can never go negative)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Make it yourself\n", "\n", "Below is a blank `BankAccount` object that you're going to create. You'll add in code step by step." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## #1: Properties\n", "\n", "Properties are a nice little list of values each object holds.\n", "`get` and `set`: sometimes you want to only want a user to see a variable but not change it. Other times you want the user to be able to change a variable. `get` lets you see the variable, `set` lets you change it. (right?)\n", "\n", "> Copy the code below and paste it into the `BankAccount` object under `//Properties`\n", "\n", "```csharp\n", " public string Number { get; }\n", " public string Owner { get; set; }\n", " public decimal Balance { get; }\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "public class BankAccount\n", "{\n", " // Properties (paste under here)\n", "\n", " // Constructor\n", "\n", " // Functions\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## #2: Constructor\n", "\n", "This method is what creates a specific instance of an object. Making a `BankAccount` class, like you're doing now, is like making a template for all bank accounts. It's not a singular individual account. The constructor is what will make a singular account, with all the persons actual details. You give the constructor all the details you want for a specific account, and it assigns the details to the new object's properties.\n", "\n", "`this` is a styling choice. It makes explicit that the variable \"Owner\" is the variable of that specific instance. In the future, you'll have two instances of an object to interact, and `this` will become a bit more explicitly helpful. You can also write `Owner` instead of `this.Owner` if you want!\n", "\n", "You're taking the variables `name` and `initialBalance` and creating a bank account that contains these variables.\n", "\n", "> Copy paste the constructor into the `BankAccount` below, under `//Constructor`\n", "\n", "```csharp\n", "public BankAccount(string name, decimal initialBalance)\n", "{\n", " this.Owner = name;\n", " this.Balance = initialBalance;\n", "}\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "public class BankAccount\n", "{\n", " // Properties\n", " public string Number { get; }\n", " public string Owner { get; set; }\n", " public decimal Balance { get; }\n", "\n", " // Constructor (Paste here!)\n", "\n", " // Functions\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## #3: Making an Instance\n", "\n", "Now that you have the code written out, see what happens if you make a `BankAccount`!\n", "\n", "> Run the two code cells code below to create a specific bank account. Does it do what you expected?\n", ">\n", "> Change the code to make a bank account for yourself. How much money do you want in your bank account?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "public class BankAccount\n", "{\n", " // Properties\n", " public string Number { get; }\n", " public string Owner { get; set; }\n", " public decimal Balance { get; }\n", "\n", " // Constructor\n", " public BankAccount(string name, decimal initialBalance)\n", " {\n", " this.Owner = name;\n", " this.Balance = initialBalance;\n", " }\n", " // Functions\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Account was created for Kendra with 1000 dollars\r\n" ] } ], "source": [ "var account = new BankAccount(\"Kendra\", 1000);\n", "Console.WriteLine($\"Account{account.Number} was created for {account.Owner} with {account.Balance} dollars\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What about the `account.Number`?\n", "\n", "You may have noticed that the code didn't print out anything for `account.Number`. That's okay! You haven't put anything in it yet. You'll learn about it in the next module." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## #4: Functions\n", "\n", "Functions exist to do actions with an object or change the object variables. These two functions will make a deposit (add money) and make a withdrawal (take out money). You'll be adding stuff in the methods later, but for now you just want to add the empty versions.\n", "\n", "> copy the functions below and add them to `BankAccount` under `//Functions`\n", "\n", "```csharp\n", "public void MakeDeposit(decimal amount, DateTime date, string note)\n", "{\n", "}\n", "\n", "public void MakeWithdrawal(decimal amount, DateTime date, string note)\n", "{\n", "}\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "public class BankAccount\n", "{\n", " // Properties\n", " public string Number { get; }\n", " public string Owner { get; set; }\n", " public decimal Balance { get; }\n", "\n", " // Constructor\n", " public BankAccount(string name, decimal initialBalance)\n", " {\n", " this.Owner = name;\n", " this.Balance = initialBalance;\n", " }\n", "\n", " // Functions (paste here!)\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Review\n", "\n", "Here's the version of `BankAccount` you end up with in this module. You'll be adding more to it in the next module, but why don't you try out stuff, just to see what you need to learn?\n", "\n", "> Can you add a 10-digit code? What would your object need to know to make sure the code was unique?\n", ">\n", "> Try out adding to the deposit function! What do you want it to do?\n", ">\n", "> How might you check that the initial balance is positive?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "public class BankAccount\n", "{\n", " // Variables (#1)\n", " public string Number { get; }\n", " public string Owner { get; set; }\n", " public decimal Balance { get; }\n", "\n", " // Constructor (#2)\n", " public BankAccount(string name, decimal initialBalance)\n", " {\n", " this.Owner = name;\n", " this.Balance = initialBalance;\n", " }\n", "\n", " // Functions (#4)\n", " public void MakeDeposit(decimal amount, DateTime date, string note)\n", " {\n", " }\n", "\n", " public void MakeWithdrawal(decimal amount, DateTime date, string note)\n", " {\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Account was created for Kendra with 1000 dollars\r\n" ] } ], "source": [ "//Make an instance (#3)\n", "var account = new BankAccount(\"Kendra\", 1000);\n", "Console.WriteLine($\"Account{account.Number} was created for {account.Owner} with {account.Balance} dollars\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Continue learning\n", "\n", "There are plenty more resources out there to learn!\n", "> [⏩ Next Module - Methods and Members](https://raw.githubusercontent.com/dotnet/csharp-notebooks/main/csharp-101/14-Methods%20and%20Members.ipynb)\n", ">\n", "> [⏪ Last Module - Lists of Other Types](https://raw.githubusercontent.com/dotnet/csharp-notebooks/main/csharp-101/12-Lists%20of%20Other%20Types.ipynb)\n", ">\n", "> [Watch the video](https://www.youtube.com/watch?v=TzgxcAiHCWA&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=16)\n", ">\n", "> [Documentation: Object Oriented Coding in C#](https://docs.microsoft.com/dotnet/csharp/fundamentals/tutorials/classes?WT.mc_id=Educationalcsharp-c9-scottha)\n", ">\n", "> [Start at the beginning: What is C#?](https://www.youtube.com/watch?v=BM4CHBmAPh4&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Resources\n", "\n", "Here's some more places to explore:\n", "> Start at the beginning: [What is C#?](https://www.youtube.com/watch?v=BM4CHBmAPh4&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=1)\n", ">\n", "> [Other 101 Videos](https://dotnet.microsoft.com/learn/videos?WT.mc_id=csharpnotebook-35129-website)\n", ">\n", "> [Microsoft Learn](https://docs.microsoft.com/learn/dotnet/?WT.mc_id=csharpnotebook-35129-website)\n", ">\n", "> [C# Documentation](https://docs.microsoft.com/dotnet/csharp/?WT.mc_id=csharpnotebook-35129-website)" ] } ], "metadata": { "kernelspec": { "display_name": ".NET (C#)", "language": "C#", "name": ".net-csharp" }, "language_info": { "file_extension": ".cs", "mimetype": "text/x-csharp", "name": "C#", "pygments_lexer": "csharp", "version": "8.0" } }, "nbformat": 4, "nbformat_minor": 4 }