Is2215 lecture4 student (1)

488 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
488
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • The deposit sub procedure accepts a string value (from our text box) and converts it to a Decimal using the Decimal parse methodThis amount is then added to the balance
  • In this example we are getting an amount from the textbox and subtracting it from our balance figureWe could have used the decimal parse method here.If we do not provide a numeric value, we will get a .NET exception error (Unexpected Data Type)
  • See: http://visualbasic.about.com/od/learnvbnet/a/sharedinstance.htm
  • The SavingsAccount class annotates the Balance method with the Shadows keyword to indicate the Balance method redeclares and hides the Balance property in the base class
  • Is2215 lecture4 student (1)

    1. 1. DOING IT IN CODESOME MORE EXAMPLES
    2. 2. EXAMPLE 1ATM
    3. 3. The Problem You are required to create an ATM application using OOP Requirements:  User can deposit money  User can withdraw money  User can view balance
    4. 4. Example #1
    5. 5. Creating the Class In Code Add a class to the project Provide appropriate name for the class Declare properties Property Procedures Create Methods Add constructors/destructors if needed
    6. 6. Creating Classes in Code Add a class to the project Provide appropriate name for the class Declare properties Declare Methods Create constructors as needed Create a destructor, if appropriate
    7. 7. 1. Add Class to the Project
    8. 8. Creating Classes in Code Add a class to the project Provide appropriate name for the class Declare properties Property Procedures Create Methods Add constructors/destructors if needed
    9. 9. 2. Provide Appropriate Name
    10. 10. Creating Classes in Code Add a class to the project Provide appropriate name for the class Declare properties Property Procedures Create Methods Add constructors/destructors if needed
    11. 11. 3. Declare Properties
    12. 12. 3. Declare Properties
    13. 13. Creating Classes in Code Add a class to the project Provide appropriate name for the class Declare properties Property Procedures Create Methods Add constructors/destructors if needed
    14. 14. 4. Property Procedures This property is ReadOnly
    15. 15. Creating Classes in Code Add a class to the project Provide appropriate name for the class Declare properties Property Procedures Create Methods Add constructors/destructors if needed
    16. 16. 3. Create Methods
    17. 17. 5. Deposit
    18. 18. 5. Withdraw
    19. 19. Creating Classes in Code Add a class to the project Provide appropriate name for the class Declare properties Property Procedures Create Methods Add constructors/destructors if needed
    20. 20. EXAMPLE 2BANK ACCOUNT
    21. 21. Declare propertiesProperty ProceduresCreate MethodsAdd constructors/destructors if needed
    22. 22. 1.Declare Properties
    23. 23. Declare propertiesProperty ProceduresCreate MethodsAdd constructors/destructors if needed
    24. 24. 2. _name Retrieve Value Store Value
    25. 25. 2. _balance
    26. 26. 2. _MaximumWithdrawal
    27. 27. Static (Shared) Members Data and behaviours that pertain to class as a whole Not just an individual instance of a class You can access static members even if no class exists
    28. 28. 2. Shared _Rate
    29. 29. Declare propertiesProperty ProceduresCreate MethodsAdd constructors/destructors if needed
    30. 30. Deposit()
    31. 31. Overloading Methods A method might need different parameter in different circumstances  The solution is to define overloaded methods Methods have the same name – common intent Each method can have a unique signature In previous example method takes parameter as a decimal number
    32. 32. Deposit ()
    33. 33. Overloading Methods The overloaded method had the same name The parameter list was different Accepts an amount as a String (text)  Converts it to a decimal
    34. 34. Withdraw ()
    35. 35. CalculateInterest()
    36. 36. IncreaseRate ()
    37. 37. Declare propertiesProperty ProceduresCreate MethodsAdd constructors/destructors if needed
    38. 38. 4. Overloading Constructors
    39. 39. In UseDim account as New _ BankAccount()
    40. 40. Overloading Constructors
    41. 41. In UseDim account as New _ BankAccount(“Joe”)
    42. 42. Overloading Constructors
    43. 43. In UseDim account as New _ BankAccount(“Joe”, 1000)
    44. 44. 4. Overriding Destructor
    45. 45. Using the ClassInstantiating the Object
    46. 46. Create Object
    47. 47. Withdrawing Cash
    48. 48. Method InputBox
    49. 49. Withdrawing Cash
    50. 50. Method
    51. 51. Depositing Cash
    52. 52. Calculate Interest
    53. 53. Method Val(txtRate.Text) / 100).ToString( "C"))
    54. 54. Extending our ExampleAdvanced OOP
    55. 55. Accessibility LevelsAccess Level Visual Basic Access ModifierUnrestricted accessibility PublicAccessible by containing PrivateclassAccessible in current FriendassemblyAccessible by containing Protectedclass and derived classAccessible in current Protected Friendassembly and by derivedclasses
    56. 56. Inheritance Advantages  Developer Productivity  Real-world modelling  Consistency
    57. 57. Base classDerived Classes
    58. 58. Inheritance A base class can define overridable methods and properties  Base class provides the default implementation  Derived class provides alternative implementations A derived class can:  Inherit base class members  Override base class members  Shadow base class members
    59. 59. Polymorphism enables an application to use instances ofderived classes interchangeably Deposit A SavingsAccount Client application Deposit A CheckingAccountBenefits of polymorphism: Consistency and simplicity Extensibility and resilience
    60. 60. Base Class Properties
    61. 61. Base Class Properties
    62. 62. Base Class Properties
    63. 63. Base Class Methods
    64. 64. Base Class Methods
    65. 65. Derived Class
    66. 66. Derived Class
    67. 67. Derived Class
    68. 68. Shadowing To indicate a derived class member shadows a base class member  Use the Shadows Keyword
    69. 69. ExamplePublic Class BankAccount Protected ReadOnly Property _ Balance() As Decimal End PropertyPublic Class SavingsAccountPublic Shadows Sub Balance( _ ByVal amount As Decimal) „ImplementationEnd Sub
    70. 70. Combining and Overloading
    71. 71. Combining and Overloading
    72. 72. Implement the Class

    ×