Download presentation
Presentation is loading. Please wait.
Published byGregory Bradford Modified over 6 years ago
1
Programming with Microsoft Visual Basic 2008 Fourth Edition
Chapter Eleven Classes and Objects
2
Previewing the ABC Company Application
Open the ABC.exe file ABC Company application calculates and displays gross pay for salaried and hourly employees Salaried employees are paid twice per month Hourly employees are paid weekly Programming with Microsoft Visual Basic 2008, Fourth Edition
3
Previewing the ABC Company Application (continued)
Figure 11-1: Interface showing the gross pay for an hourly employee Programming with Microsoft Visual Basic 2008, Fourth Edition
4
Lesson A Objectives After studying Lesson A, you should be able to:
Define a class Instantiate an object from a class that you define Add Property procedures to a class Include data validation in a class Create a default constructor Include methods in a class Programming with Microsoft Visual Basic 2008, Fourth Edition
5
Object-Oriented Programming Terminology
Class: Pattern or blueprint for object OOP: Object-oriented programming Instance: Object created from class Instantiation: Process of creating an object from a class Attributes: Characteristics that describe the object Behaviors: Methods and events that define how the object will act or react Programming with Microsoft Visual Basic 2008, Fourth Edition
6
Object-Oriented Programming Terminology (continued)
Methods: Operations (actions) that object is capable of performing Events: Actions to which an object can respond Encapsulate: To contain A class encapsulates all attributes and behaviors of object it instantiates Programming with Microsoft Visual Basic 2008, Fourth Edition
7
Creating a Class Class statement: Used to define class
Includes attributes and behaviors of objects created from the class Two types of classes used in VB applications: Built-in classes, such as Button class Programmer-defined classes After class has been defined, it can be used to instantiate objects Programming with Microsoft Visual Basic 2008, Fourth Edition
8
Creating a Class (continued)
Figure 11-2: Syntax and an example of the Class statement Programming with Microsoft Visual Basic 2008, Fourth Edition
9
Creating a Class (continued)
Figure 11-6: Syntax and examples of instantiating an object from a class Programming with Microsoft Visual Basic 2008, Fourth Edition
10
Example 1—Using a Class that Contains Public Variables Only
Willow Pools application from Chapter 10 Input: Length, width, and depth of pool Calculates volume of water required This program was coded with structure in Chapter 10 The structure will be replaced with a class Programming with Microsoft Visual Basic 2008, Fourth Edition
11
Figure 11-4: Code for the Willow Pools application (with a structure)
Example 1—Using a Class that Contains Public Variables Only (continued) Figure 11-4: Code for the Willow Pools application (with a structure) Programming with Microsoft Visual Basic 2008, Fourth Edition
12
Figure 11-5: Add New Item dialog box
Example 1—Using a Class that Contains Public Variables Only (continued) Figure 11-5: Add New Item dialog box Programming with Microsoft Visual Basic 2008, Fourth Edition
13
Example 1—Using a Class that Contains Public Variables Only (continued)
Figure 11-6: Comments and Option statements entered in the RectangularPool.vb file Programming with Microsoft Visual Basic 2008, Fourth Edition
14
Example 1—Using a Class that Contains Public Variables Only (continued)
Figure 11-8: Class definition, GetGallons function, and btnCalc Click event procedure Programming with Microsoft Visual Basic 2008, Fourth Edition
15
Example 2—Using a Class that Contains a Private Variable, a Property Procedure, and Two Methods
Disadvantages of Public variables in class: Class cannot control values assigned to Public variables Violates concept of encapsulation, in which class behaviors control class’s data Programming with Microsoft Visual Basic 2008, Fourth Edition
16
Private Variables and Property Procedures
Private class variables: Can only be used within class Not visible to rest of the application Public property: Used to refer to (expose) Private variable for use by other parts of application Property procedure: Creates a Public property Programming with Microsoft Visual Basic 2008, Fourth Edition
17
Private Variables and Property Procedures (continued)
Used to create public property Data type of Property must match Private variable Can be set to ReadOnly or WriteOnly if necessary Property procedure contains blocks of code: Get block: Retrieves contents of Private variable Set block: Used to assign value to Private variable Blocks may be used individually or together Programming with Microsoft Visual Basic 2008, Fourth Edition
18
Private Variables and Property Procedures (continued)
Figure 11-13: Completed Length Property procedure Programming with Microsoft Visual Basic 2008, Fourth Edition
19
Constructors Constructor: Class method whose purpose is to initialize class’s Private variables Processed each time an object is created Must be coded as Sub procedure named New Class can have more than one constructor Names are same, but parameter lists must differ Default constructor: Constructor without parameters Programming with Microsoft Visual Basic 2008, Fourth Edition
20
Constructors (continued)
Figure 11-14: Syntax and an example of a constructor Programming with Microsoft Visual Basic 2008, Fourth Edition
21
Methods Other Than Constructors
A class may contain methods other than constructors May be either Sub or Function procedures Functions return value; Sub procedures do not Rules for naming methods: Name should be entered using Pascal case First word in name should be verb Subsequent words should be nouns and adjectives Programming with Microsoft Visual Basic 2008, Fourth Edition
22
Methods Other Than Constructors (continued)
Figure 11-15: Syntax and an example of a method that is not a constructor Programming with Microsoft Visual Basic 2008, Fourth Edition
23
Coding the Carpet Haven Application
Figure 11-16: Pseudocode for the Calculate button’s Click event procedure Programming with Microsoft Visual Basic 2008, Fourth Edition
24
Coding the Carpet Haven Application (continued)
Figure 11-17: Rectangle class definition and btnCalc Click event procedure Programming with Microsoft Visual Basic 2008, Fourth Edition
25
Coding the Carpet Haven Application (continued)
Figure 11-17: Rectangle class definition and btnCalc Click event procedure (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition
26
Example 3—Reusing a Class
Rectangle class from Ex. 2 will be reused to represent square pizza for Pizza Roma application Square is rectangle with 4 equal sides Reusing class saves programming time and money Reuse is one advantage of OOP Programming with Microsoft Visual Basic 2008, Fourth Edition
27
Example 3—Reusing a Class (continued)
Figure 11-19: Pseudocode for the btnCalc control’s Click event procedure Programming with Microsoft Visual Basic 2008, Fourth Edition
28
Example 3—Reusing a Class (continued)
Figure 11-20: Code entered in the btnCalc control’s Click event procedure Programming with Microsoft Visual Basic 2008, Fourth Edition
29
Lesson A Summary Class statement is used to define class
Defined classes are saved in files with a .vb extension Objects are instantiated from defined class Access to Private class variables should be done with Public properties Public properties are created using Property procedure Programming with Microsoft Visual Basic 2008, Fourth Edition
30
Lesson A Summary (continued)
Constructor initializes variables of a class Constructor method must be named New Default constructor has no parameters Class can have methods other than constructors Programming with Microsoft Visual Basic 2008, Fourth Edition
31
Lesson B Objectives After studying Lesson B, you should be able to:
Include a ReadOnly property in a class Include a parameterized method in a class Overload a method in a class Programming with Microsoft Visual Basic 2008, Fourth Edition
32
Example 4—Using a Class that Contains a ReadOnly Property
ReadOnly keyword: Indicates that property’s value can be retrieved (read) but not set (written) ReadOnly property gets its value from class instead of from application Grade Solution application: Will return letter grade based on numeric grade Programming with Microsoft Visual Basic 2008, Fourth Edition
33
Example 4—Using a Class that Contains a ReadOnly Property (continued)
Figure 11-28: Property procedure for the ReadOnly LetterGrade property Programming with Microsoft Visual Basic 2008, Fourth Edition
34
Example 4—Using a Class that Contains a ReadOnly Property (continued)
Figure 11-29: DetermineGrade method entered in the class definition Programming with Microsoft Visual Basic 2008, Fourth Edition
35
Example 4—Using a Class that Contains a ReadOnly Property (continued)
Figure 11-30: The message indicates that the property is ReadOnly Programming with Microsoft Visual Basic 2008, Fourth Edition
36
Example 5—Using a Class that Contains Two Constructors
A class can have more than one constructor Parameterized constructor: Constructor that requires parameters FormattedDate constructor with parameters: Requires values for two attributes in order to create the object: Month number and day number Method’s signature = method name + optional parameter list Programming with Microsoft Visual Basic 2008, Fourth Edition
37
Example 5—Using a Class that Contains Two Constructors (continued)
Figure 11-32: FormattedDate class definition Programming with Microsoft Visual Basic 2008, Fourth Edition
38
Example 5—Using a Class that Contains Two Constructors (continued)
Figure 11-32: FormattedDate class definition (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition
39
Example 5—Using a Class that Contains Two Constructors (continued)
Figure 11-33: Click event procedures for the btnDefault and btnParameterized controls Programming with Microsoft Visual Basic 2008, Fourth Edition
40
Example 6—Using a Class that Contains Overloaded Methods
Basic features of an Employee object: Two attributes: Employee number and name Public properties to get and set attributes Default constructor and parameterized constructor Method to return gross pay of salaried employee Method to return gross pay of hourly employee Overloaded methods: Methods with same name, but having different parameters Programming with Microsoft Visual Basic 2008, Fourth Edition
41
Example 6—Using a Class that Contains Overloaded Methods (continued)
Figure 11-36: Box displaying one of the signatures of the ToString method Programming with Microsoft Visual Basic 2008, Fourth Edition
42
Example 6—Using a Class that Contains Overloaded Methods (continued)
Figure 11-37: Interface for the ABC Company application Programming with Microsoft Visual Basic 2008, Fourth Edition
43
Example 6—Using a Class that Contains Overloaded Methods (continued)
Figure 11-38: Pseudocode for the Calculate button’s Click event procedure Programming with Microsoft Visual Basic 2008, Fourth Edition
44
Example 6—Using a Class that Contains Overloaded Methods (continued)
Figure 11-39: Code entered in the selection structure’s false path Programming with Microsoft Visual Basic 2008, Fourth Edition
45
Lesson B Summary Use ReadOnly keyword to create property that can be retrieved but not set by application To create parameterized method, enter parameters between parentheses in method definition Overloaded methods share one name, but have different parameter lists Programming with Microsoft Visual Basic 2008, Fourth Edition
46
Lesson C Objectives After studying Lesson C, you should be able to:
Create a derived class using inheritance Override a method in the base class Refer to the base class using the MyBase keyword Programming with Microsoft Visual Basic 2008, Fourth Edition
47
Example 7—Using a Base Class and a Derived Class
Inheritance: Using one class to create another Base class: Original class providing behaviors and attributes Derived class: New class that inherits attributes and behaviors from base class Inherits clause: Enables derived class to inherit from base class Included in derived class’s Class statement Programming with Microsoft Visual Basic 2008, Fourth Edition
48
Example 7—Using a Base Class and a Derived Class (continued)
Overriding: Allows derived class to replace method inherited from its base class Base class method header must include Overridable keyword Derived class method header must include Overrides keyword MyBase keyword: Used to refer to base class MyBase.New ([parameterlist]): Used in derived class to call its base class’s constructor Programming with Microsoft Visual Basic 2008, Fourth Edition
49
Example 7—Using a Base Class and a Derived Class (continued)
Figure 11-44: Code for the Employee base class and Salaried derived class Programming with Microsoft Visual Basic 2008, Fourth Edition
50
Example 7—Using a Base Class and a Derived Class (continued)
Figure 11-44: Code for the Employee base class and Salaried derived class (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition
51
Example 7—Using a Base Class and a Derived Class (continued)
Figure 11-46: Pseudocode for the Calculate button’s Click event procedure Programming with Microsoft Visual Basic 2008, Fourth Edition
52
Lesson C Summary Use Inherits clause to allow derived class to inherit attributes and behaviors of base class Use Overridable keyword in base class method to indicate that derived class can replace it Use Overrides keyword in derived class method to indicate that method has been replaced Programming with Microsoft Visual Basic 2008, Fourth Edition
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.