1 / 58

OBJECTIVES: How to write classes

OOP: Creating Object Oriented Programs. OBJECTIVES: How to write classes How to create an object from a class using the " New " keyword. How to communicate with an object from a Form. Students will enter a simple program's code, execute it, and trace its execution with the debugger

kalei
Download Presentation

OBJECTIVES: How to write classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. OOP: Creating Object Oriented Programs • OBJECTIVES: • How to write classes • How to create an object from a class using the "New" keyword. • How to communicate with an object from a Form. • Students will enter a simple program's code, execute it, and trace its execution with the debugger • Classes with and without inheritance.

  2. OOP: Creating Object Oriented Programs At least three times, read these notes; and write small OOP programs. If you do these things you will survive and enjoy OOP. YOU MUST LEARN THESE SLIDES.

  3. OOP: Creating Object Oriented ProgramsDesigning Your Own Classes.Using Your Own Classesto create Objects.

  4. OOP: Creating Object Oriented Programs You are about to view a very simple OOP program which is not very realistic because it is so simple. It illustrates: 1. Writing a CLASS. 2. Instantiating (creating) an OBJECT from the CLASS. 3. Using the OBJECT in a short program. Major reasons for using OOP are to create stable and reliablecode that is easy to reuse.

  5. OOP: Creating Object Oriented Programs This simple program calculates sales tax by: 1. Sending the total sales amount (100.00) to an object from Form1. 2. Next, the object calculates the sales tax (7.50) and sends it back to Form1. (The next slides show how all this works.) 3. The object’s code was created (instantiated) by the statement below: Dim objCalculateSalesTax As New clsCalculateSalesTax Object name Class name Form1 OBJECT’S CODE 100.00 7.50

  6. OOP: Creating Object Oriented Programs New New New New New To create (instantiate) an object from a class use the keyword: New. New New New New New New New New Dim objObjectName As New clsClassName After executing the above statement, objObjectName will become an object created from the class called clsClassName. New New New

  7. OOP: Creating Object Oriented Programs Dim objObjectName AsNewclsClassName What the statement above does as it is being executed: 1. First, the underlined words make an empty location in memory for an object with a type of data called clsClassName. We can get to that location using the word objObjectName. Note: This first step is exactly the same as declaring an ordinary variable such as: Dim decPayRate As Decimal; Dim strLastName As String Dim objObjectName AsNewclsClassName 4 GB memory Empty location This empty location is reserved for an object and it is accessed using the name objObjectName.

  8. OOP: Creating Object Oriented Programs Dim objObjectName AsNewclsClassName What the statement above does as it is being executed (Continued): 2. Second, the word New takes code from a file within your project called clsClassName.vb and makes a copy of it and places the copy in the location called objObjectName. Note: When you write the code for clsClassName, the file will be automatically created and placed on the disk as part of your Project. In the slides to follow, you will write the code for a class that will be placed in a file within your project. Dim objObjectName AsNewclsClassName 4 GB memory New makes a copy of the class code from the class file within your project and places that copy into the empty location called objObjectName. The object can now be used. The file called clsClassName.vb contains the Class. Object’s code A copy of clsClassName is sent to the location called objObjectName when New is executed. objObjectName

  9. QUICK AND DIRTY INTRODUCTION TO CREATING AND USING AN OBJECT The next three slides will give you a very quick introduction to creating and using an OBJECT. Assume the Sales Tax Calculator application below:

  10. HOW TO CREATE THE OBJECT AND USE IT. This statement creates a location in memory and puts the object (objCalculateSalesTax) into it. This statement will assign (SET) the object’s property (TotalSales) to 100.00. This statement will use the method (ComputeSalesTax) of the object (objCalculateSalesTax) to compute the sales tax. The sales tax value is then displayed in the label. Form1 code Method Property

  11. HOW THE OBJECT WORKS How the object code works (see numbers left of code below): 1. The object has a name: objCalculateSalesTax. 2. A module-level variable (mdecTotalSales) is declared. 3. TotalSales is the Property procedure which will give this object its only property. 4. When TotalSales (Agrument) is assigned a value (100.00) in Form1’s code, it passes ByValue 100.00 to Value (Parameter) which then assigns 100.00 to mdecTotalSales. 5. ComputSalesTax is a Method (function) which calculates the sales tax using mdecTotalSales. Note: The parameter “Value” could have been given any name, but “Value” is standard. Parameter Argument 1 Property name 2 3 4 Object’s code: objCalculateSalesTax Method name 5

  12. HOW THE OBJECT WORKS WITH THE FORM1 CODE 1. In Form1’s code, the property TotalSales is assigned 100.00 from txtTotalSales.Text. 2. This 100.00 is passed to the TotalSales Property in the object and 7.50 is returned using the method ComputeSalesTax. Form1 Code Form1 code that creates the object from the class and then uses the object. 100.00 Object’s Code Object created from the class. 7.50

  13. Lab Exercise 1. Pass out the code for this program.2. Have students enter and run the code. 3. Have students single step through the program and watch the flow of control from Form1 code to the Object's code and back again. 4. Students must understand this code COMPLETELY. If we do not do this Lab Exercise in class, you need to do it on your own.

  14. Lab Exercise Steps: 1. Create a project. Use any meaningful name you want. 2. Create a GUI for a program that inputs a decimal value and displays a tax amount in a currency format: INSTRUCTIONS ON HOW TO CREATE AND USE A CLASS

  15. INSTRUCTIONS ON HOW TO CREATE AND USE A CLASS Steps: 3. Enter this code within the button-click code.

  16. Steps: 4. Select the project name and right-click on it. INSTRUCTIONS ON HOW TO CREATE AND USE A CLASS

  17. Steps: The screen should now look like this. 5. Select Add, then Class. INSTRUCTIONS ON HOW TO CREATE AND USE A CLASS

  18. Steps: The screen should now look like this. 6. Select Class below and type in the class name. This example uses clsCalculateSalesTax.vb. INSTRUCTIONS ON HOW TO CREATE AND USE A CLASS Don’t forget to press “Add”

  19. Steps: 7. Your screen should now look like the one below. 8. Add the class code here. (See the next slide for the class code.) INSTRUCTIONS ON HOW TO CREATE AND USE A CLASS

  20. Steps: 9. After entering the class’s code below, your class should look like this. INSTRUCTIONS ON HOW TO CREATE AND USE A CLASS

  21. Steps: 10. The solution explorer should now look like this. Your class has been added to your project and is now a file within your project. INSTRUCTIONS ON HOW TO CREATE AND USE A CLASS Your class is here as a“.vb” file.

  22. Steps: 11. Congratulations. You have just written your first class. 12. Next, run your project. It should work. 13. Don’t forget to save your project. 14. Using the interactive debugger, single step thru the code and watch the values and the flow of program. INSTRUCTIONS ON HOW TO CREATE AND USE A CLASS You must understand this code to be able to do the next programming assignment.

  23. Now, what have you just done? Answers: You have coded a class called clsCalculateSalesTax and have saved the code in a file called clsCalculateSalesTax.vb in your project. SUMMARY NOTE: mdecTotalSales is a module-level variable. It can also be called a member variable as it is a member of the class clsCalculateSalesTax and can be used anywhere within the class. Note that the variable is declaredin the typical module-level section (declaration section) of the code.

  24. Now, what have you just done (continued)? Answers: (1) You have created the object (objCalculateSalesTax) from the class (clsCalculateSalesTax) by using the file called clsCalculateSalesTax.vb, and (2) assigned 100.00 to the property TotalSales, and (3) used the method called ComputeSalesTax. This method returned the value 7.50 which was displayed in lblSalesTax.Text. Note: see numbers 1,2,3 below that correspond to the above numbers. SUMMARY 1 2 3

  25. AN IN-DEPTH LOOK The next slides will give you a more detailed look at the previous example.

  26. AN IN-DEPTH LOOK objCalculateSalesTax TotalSales ComputeSalesTax OBJECT Name Property Name Method Name What this OBJECT (objCalculateSalesTax) does: 1. Receives a value (100.00) in the object's property: "TotalSales" 2. Calculates the sales tax (7.50) using the object's method: "ComputeSalesTax". 3. Next, the object sends the sales tax value (7.50) back to Form1. THE OBJECT IS CODE IN MEMORY THIS FORM1 IS CODE IN MEMORY Sends 100.00 Sends back 7.50 This CLASS DIAGRAM represents the coded object. The code in Form1 creates and uses the object.

  27. AN IN-DEPTH LOOK objCalculateSalesTax TotalSales ComputeSalesTax OBJECT FORM1 Computer's Memory 4GB RAM 4GB RAM Code Code 100.00 7.50 These two units of code communicate in the computer’s memory using: 1. Function procedures, and/or 2. General Procedures. In this program we used only Function Procedures as your will see on the next slide. >>>>

  28. AN IN-DEPTH LOOK 7.50 Computer's Memory 4GB RAM 4GB RAM FORM1'S CODE OBJECT CODE 100.00 These two units of code communicate using: 1. Function Procedures, or 2. General Procedures In this program we only used Function Procedures.

  29. AN IN-DEPTH LOOK objCalculateSalesTax TotalSales ComputeSalesTax What to look for in this slide: 1. Note the places where the object's name is used. 2. Note the places where the object's property name is used. 3. Note the places where the object's method name is used. Note: The actual code for the object is not shown below. Only a Class Diagram is shown. THE OBJECT THE FORM1 CODE THAT USES THE OBJECT OBJECT Name Property Name Method Name This code is the object. This code creates and uses the object. The OBJECT'S code and the Form's code communicate

  30. AN IN-DEPTH LOOK How the code below in Form1 creates the object and communicates with the object code. "New" keyword creates object from the class name. Object Class Name Property Object Method Note: The object's code is not show here.

  31. AN IN-DEPTH LOOK What the code from Form1 below does: 1. Creates the object “objCalculateSalesTax" using the statement below: Dim objCalculateSalesTax AsNew clsCalculateSalesTax 'The data type of the object “objCalculateSalesTax" is the class “clsCalculateSalesTax" 2. The "TotalSales" property in the object is Set using the statement below: objCalculateSalesTax.TotalSales = CDec(txtTotalSales.Text) 3. The “ComputeSalesTax" method returns the sales tax using the statement below" lblSalesTax.Text = FormatCurrency(objCalculateSalesTax.ComputeSalesTax) "New" keyword creates object from the class name. Object Class Name Property Method

  32. AN IN-DEPTH LOOK Coding the Class Module-level variable name. Name of the Class Property Name This Property procedure allows for: 1. Set: assigning a value to a property. Method Name Method procedure: 1. This is a function procedure that returns a value through its name: ComputeSalesTax.

  33. AN IN-DEPTH LOOK Coding the Class What does "Set" do? Set assigns the Value "100.00" to mdecTotalSales so mdecTotalSales can be used in the Method "ComputeSalesTax". Remember, mdecTotalSales is a module-level variable and can be used anywhere in the Class. 100.00

  34. AN IN-DEPTH LOOK Coding the Class What does "Get" do? clsCalculateSalesTax Get would do NOTHING in this program. Get, when used, allows the program that accesses the object to get the current value of a property. TotalSales = mdecTotalSales

  35. AN IN-DEPTH LOOK • WriteOnly • If you wish to use Set without Get, you must place WriteOnly here. Coding the Class Using Set without Get 100.00 • WriteOnly • If you wish to use Set without Get you must use WriteOnly. • Using WriteOnly causes the Property Procedure to be a write only procedure. That is, you can only Set a property. You cannot Get (or read) a property from TotalSales.

  36. AN IN-DEPTH LOOK Coding the Class What do module-level variables do? • Module-level variables • These variables can be used in ANY procedure within the class. • The “m” means module. You may think of these variables as module-level variables, as they are recognized anywhere within the Class. • In this program, "mdecTotalSales" allows for communication between the two procedures below: • 1. The Property Procedure • 2. The Function Procedure. Note: This module-level variable is often called a member variable, because it is a member of the class and can be used anywhere within the class.

  37. AN IN-DEPTH LOOK Form1 Code Property Method 100.00 7.50 100.00 Communication 7.50 Class code

  38. AN IN-DEPTH LOOK This slide and the next one illustrate how to use SET and GET with a very simple program. Construct this GUI and enter the code on the next slide. Run the program and step thru it by setting breakpoints. Slide 1/2

  39. AN IN-DEPTH LOOK Enter the following code for the Form1 code and the class code. Run the program and step thru it by setting breakpoints. Slide 2/2

  40. AN IN-DEPTH LOOK Two ways to create an object There are two ways to create an object but only one will be covered in these notes. I think the one-step method is the easiest method to understand. One-statement method Dim objObjectName As New clsClassName 1. Dim objObjectName As …. .clsClassName makes a location for the object called objObjectName with a clsClassName type of data. 2. New creates the object and places it into the location called objObjectName Two-statement method Dim objObjectName As clsClassName objObjectName = NEW clsClassName The two-statement method will not be covered in these notes.

  41. INHERITANCE As previously mentioned, a major goal of OOP is reusable code. OOP allows for this through inheritance; that is, previously written classes can be used again as part of a new application. In the next slides we will use inheritance by writing a new class that will inherit the single property and single method of our previously written “clsCalculateSalesTax" class. (Note: We will change the name of our base class from “clsCalculateSalesTax” to just “clsSalesTax.” This will make the code easier to read and clearer.)

  42. clsSalesTax TotalSales ComputeSalesTax() Derived Class clsSalesTaxAndSurtax ComputeSurtax() ComputeTotalTax() Inheritance Slide topic: Two Class Diagrams showing inheritance Our user interface can now use one property and three methods using the class clsSalesTaxAndSurtax: One property and three methods: 1. clsSalesTaxAndSurtax.TotalSales 1. clsSalesTaxAndSurtax.ComputeSalesTax() 2. clsSalesTaxAndSurtax.ComputeSurtax() 3. clsSalesTaxAndSurtax.ComputeTotalTax() CLASS Name Base Class Property Name Method Name CLASS Name User interface No Property Methods Names What gets inherited from clsSalesTax? clsSalesTaxAndSurtax inherits: 1. TotalSales 2. ComputeSalesTax()

  43. clsSalesTax TotalSales ComputeSalesTax() Derived Class clsSalesTaxAndSurtax TotalSales ComputSalesTax() ComputeSurtax() ComputeTotalTax() Derived Class clsSalesTaxAndSurtax ComputeSurtax() ComputeTotalTax() Inheritance Slide topic: What "seems" to be the affect of this inheritance. Although the Derived Class still only has two methods, it acts like it has one property and three methods because we can now use these: 1. clsSalesTaxAndSurtax.TotalSales 1. clsSalesTaxAndSurtax.ComputeSalesTax() 2. clsSalesTaxAndSurtax.ComputeSurtax() 3. clsSalesTaxAndSurtax.ComputeTotalTax() Base Class The derived class reaches up for the properties and methods of the base class. IMPORTANT: clsSalesTaxAndSurtax does NOT contain "TotalSales" or "ComputeSalesTax()". It only seems that way.

  44. Base Class clsSalesTax TotalSales ComputeSalesTax() Inheritance Slide topic: Code for the base class "clsSalesTax" The Code for the BaseClass You must use Get so that the DerivedClass can Get the value of TotalSales when the Derived Class inherits the Property "TotalSales".

  45. Derived Class clsSalesTaxAndSurtax ComputeSurtax() ComputeTotalTax() Inheritance Slide topic: Code for the derived class "clsSalesTaxAndSurtax" The Code for the DerivedClass IMPORTANT KEYWORD Because of inheritance, clsSalesTaxAndSurtax can use: 1. TotalSales 2. ComputeSalesTax() These two are Inherited from the Base Class.

  46. Inheritance Slide topic: Code for the derived class "clsSalesTaxAndSurtax" The Code for the DerivedClass Note: When objSalesTaxAndSurtax is created from clsSalesTaxAndSurtax (Derived Class), objSalesTax (Base Class) is also created. Thus, Dim objSalesTaxAndSurtax As New clsSalesTaxAndSurtax will cause the creation of these two objects in memory. 1. objSalesTaxAndSurtax 2. objSalesTax Derived Class Code IMPORTANT KEYWORD

  47. Inheritance Slide topic: Code for the User Interface. The Code for the User Interface Our user interface can now use one property and three methods using the class clsSalesTaxAndSurtax: Property 1. clsSalesTaxAndSurtax.TotalSales Methods 1. clsSalesTaxAndSurtax.ComputeSalesTax 2. clsSalesTaxAndSurtax.ComputeSurtax 3. clsSalesTaxAndSurtax.ComputeTotalTax One Property Three Methods .ComputeSalesTax .ComputeSurtax .TotalSales .ComputeTotalTax

  48. Inheritance Slide topic: Use obj because after instantiation, SalesTaxAndSurtax is an object. The Code for the User Interface BEFORE INSTANTIATON AFTER INSTANTIATON Inherited from clsSalesTax 1. clsSalesTaxAndSurtax.TotalSales 2. clsSalesTaxAndSurtax.ComputeSalesTax From clsSalesTaxAndSurtax 3. clsSalesTaxAndSurtax.ComputeSurtax 4. clsSalesTaxAndSurtax.ComputeTotalTax Inherited from clsSalesTax 1. objSalesTaxAndSurtax.TotalSales 2. objSalesTaxAndSurtax.ComputeSalesTax From clsSales AndTaxSurtax 3. objSalesTaxAndSurtax.ComputeSurtax 4. objSalesTaxAndSurtax.ComputeTotalTax Use obj

  49. Inheritance Slide topic: Use obj because after instantiation, SalesTaxAndSurtax is an object. The Code for the User Interface • We are using cls and obj to HELP you as a beginning program distinguish between a class and an object of the class in your code. • In the real world, it is not likely that the programmer will use cls and obj, but should.

  50. Lab Exercise 1. Pass out the code for this program. See Directory: LABS-FALL 2007 for handout 2. Have students enter and run the code. 3. Have students single step through the program and watch the flow of control from Form1 code to the Objects’ code and back again. 4. Students must understand this code COMPLETELY. If we do not do this Lab Exercise in class, you need to do it on your own.

More Related