Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic th Edition
Programming with Microsoft Visual Basic 2010, 5 th Edition Previewing the ABC Company Application 2 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 2010, 5 th Edition Previewing the ABC Company Application (cont’d.) 3 Figure 11-1 Interface showing Sarah’s gross pay and information
Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson A Objectives 4 After studying Lesson A, you should be able to: Explain the terminology used in object-oriented programming Create a class Instantiate an object Add Property procedures to a class Include data validation in a class Create a default constructor Create a parameterized constructor Include methods other than constructors in a class
Programming with Microsoft Visual Basic 2010, 5 th Edition Object-Oriented Programming Terminology 5 Object-oriented programming language Uses objects to accomplish program’s goal Class: Pattern or blueprint for object 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 2010, 5 th Edition Object-Oriented Programming Terminology (cont’d.) 6 Methods Operations (actions) that object is capable of performing Events Actions to which an object can respond Encapsulate: To enclose in a capsule A class encapsulates all attributes and behaviors of object it instantiates
Programming with Microsoft Visual Basic 2010, 5 th Edition Creating a Class 7 Two types of classes used in VB applications Built-in classes, such as TextBox class Programmer-defined classes Class statement Used to define a class Defines attributes and behaviors of objects created from the class After class has been defined, it can be used to instantiate objects
Programming with Microsoft Visual Basic 2010, 5 th Edition Creating a Class (cont’d.) 8 Figure 11-2 Syntax of the Class statement
Programming with Microsoft Visual Basic 2010, 5 th Edition 9 Figure 11-4 Syntax and examples of instantiating an object from a class
Programming with Microsoft Visual Basic 2010, 5 th Edition Example 1—A Class that Contains Public Variables Only 10 Willow Pools application from Chapter 10 Input: Length, width, and depth of pool Calculates volume of water required This program was coded with a structure in Chapter 10 The structure will be replaced with a class
Programming with Microsoft Visual Basic 2010, 5 th Edition 11 Figure 11-5 Code for the Willow Pools application (with a structure) (continues)
Programming with Microsoft Visual Basic 2010, 5 th Edition 12 Figure 11-5 Code for the Willow Pools application (with a structure) (cont’d.)
Programming with Microsoft Visual Basic 2010, 5 th Edition Example 1—Using a Class that Contains Public Variables Only (cont’d.) 13 Figure 11-6 Comments and Option statements entered in the class file
Programming with Microsoft Visual Basic 2010, 5 th Edition 14 Figure 11-8 Class definition, GetGallons function, and btnCalc control’s Click event procedure
Programming with Microsoft Visual Basic 2010, 5 th Edition Example 2—A Class that Contains Private Variables, Public Properties, and Methods 15 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 2010, 5 th Edition Private Variables and Property Procedures 16 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 2010, 5 th Edition Private Variables and Property Procedures (cont’d.) 17 ReadOnly keyword Indicates application can read property’s value Cannot set the value WriteOnly keyword Indicates application can set the property’s value But cannot retrieve the value 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 2010, 5 th Edition 18 Figure Syntax and examples of a Property procedure (continues)
Programming with Microsoft Visual Basic 2010, 5 th Edition 19 Figure Syntax and examples of a Property procedure (cont’d.)
Programming with Microsoft Visual Basic 2010, 5 th Edition Private Variables and Property Procedures (cont’d.) 20 Figure Length Property procedure entered in the class
Programming with Microsoft Visual Basic 2010, 5 th Edition Constructors 21 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 2010, 5 th Edition Constructors (cont’d.) 22 Parameterized constructor Constructor containing one or more parameters Method’s signature Method name combined with optional parameter list
Programming with Microsoft Visual Basic 2010, 5 th Edition 23 Figure Syntax and examples of a constructor
Programming with Microsoft Visual Basic 2010, 5 th Edition Methods Other Than Constructors 24 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 2010, 5 th Edition Methods Other Than Constructors (cont’d.) 25 Figure Syntax and examples of a method that is not a constructor
Programming with Microsoft Visual Basic 2010, 5 th Edition Coding the Carpet Haven Application 26 Figure Pseudocode for the Calculate button’s Click event procedure
Programming with Microsoft Visual Basic 2010, 5 th Edition Coding the Carpet Haven Application (cont’d.) 27 Figure TryParse methods entered in the procedure
Programming with Microsoft Visual Basic 2010, 5 th Edition 28 Figure Rectangle class definition and btnCalc control’s Click event procedure (continues)
Programming with Microsoft Visual Basic 2010, 5 th Edition 29 Figure Rectangle class definition and btnCalc control’s Click event procedure (cont’d.)
Programming with Microsoft Visual Basic 2010, 5 th Edition Example 3—A Class that Contains a Parameterized Constructor 30 Add a parameterized constructor to the Rectangle class from Example 2
Programming with Microsoft Visual Basic 2010, 5 th Edition Example 3—A Class that Contains a Parameterized Constructor (cont’d.) 31 Figure Default and parameterized constructors
Programming with Microsoft Visual Basic 2010, 5 th Edition Example 4—Reusing a Class 32 Rectangle class from Examples 2 and 3: Reused here to represent a square pizza Square is rectangle with four equal sides Ability to use object for more than one purpose: Saves programming time and money Advantage of object-oriented programming
Programming with Microsoft Visual Basic 2010, 5 th Edition Example 4—Reusing a Class (cont’d.) 33 Figure Pizza Roma application’s interface
Programming with Microsoft Visual Basic 2010, 5 th Edition 34 Figure Pseudocode for the btnCalc control’s Click event procedure
Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson A Summary 35 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 2010, 5 th Edition Lesson A Summary (cont’d.) 36 Constructor initializes variables of a class Constructor method must be named New Default constructor has no parameters Class may have many parameterized constructors Class can have methods other than constructors
Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson B Objectives 37 After studying Lesson B, you should be able to: Include a ReadOnly property in a class Create an auto-implemented property Overload a method in a class
Programming with Microsoft Visual Basic 2010, 5 th Edition Example 5—A Class that Contains a ReadOnly Property 38 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 Calculator application Returns letter grade based on numeric grade
Programming with Microsoft Visual Basic 2010, 5 th Edition Example 5—A Class that Contains a ReadOnly Property (cont’d.) 39 Figure Interface for the Grade Calculator application
Programming with Microsoft Visual Basic 2010, 5 th Edition Example 5—A Class that Contains a ReadOnly Property (cont’d.) 40 Figure ReadOnly property message
Programming with Microsoft Visual Basic 2010, 5 th Edition Example 6—A Class that Contains Auto- Implemented Properties 41 Auto-implemented properties New feature of Visual Basic 2010 Enables specifying property of a class in one line of code
Programming with Microsoft Visual Basic 2010, 5 th Edition Example 6—A Class that Contains Auto- Implemented Properties (cont’d.) 42 Figure Syntax and examples of creating an auto-implemented property
Programming with Microsoft Visual Basic 2010, 5 th Edition 43 Figure Modified CourseGrade class definition
Programming with Microsoft Visual Basic 2010, 5 th Edition Example 7—A Class that Contains Overloaded Methods 44 Overloaded methods Methods with same name but having different parameters Can overload any methods in a class Not just constructors
Programming with Microsoft Visual Basic 2010, 5 th Edition Example 7—A Class that Contains Overloaded Methods (cont’d.) 45 Figure Attributes and behaviors of an Employee object
Programming with Microsoft Visual Basic 2010, 5 th Edition 46 Figure Employee class definition
Programming with Microsoft Visual Basic 2010, 5 th Edition Example 7—A Class that Contains Overloaded Methods (cont’d.) 47 Figure First of the ToString method’s signatures
Programming with Microsoft Visual Basic 2010, 5 th Edition Example 7—A Class that Contains Overloaded Methods (cont’d.) 48 Figure Interface for the ABC Company application
Programming with Microsoft Visual Basic 2010, 5 th Edition 49 Figure Pseudocode for the Calculate button’s Click event procedure
Programming with Microsoft Visual Basic 2010, 5 th Edition Example 7—A Class that Contains Overloaded Methods (cont’d.) 50 Figure Additional comment and code entered in the false path
Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson B Summary 51 Use ReadOnly keyword to create property that can be retrieved but not set by application To specify the property of a class in one line: Create an auto-implemented property To include a parameterized method in a class: Enter parameters between parentheses following the method’s name Overloaded methods share one name but have different parameter lists
Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson C Objectives 52 After studying Lesson C, you should be able to: Create a derived class Refer to the base class using the MyBase keyword Override a method in the base class
Programming with Microsoft Visual Basic 2010, 5 th Edition Example 8—Using a Base Class and a Derived Class 53 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 2010, 5 th Edition Example 8—Using a Base Class and a Derived Class (cont’d.) 54 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 2010, 5 th Edition 55 Figure Contents of the Shapes.vb file
Programming with Microsoft Visual Basic 2010, 5 th Edition 56 Figure Modified Square class and Cube class definitions
Programming with Microsoft Visual Basic 2010, 5 th Edition Lesson C Summary 57 Use Inherits clause to allow derived class to inherit attributes and behaviors of base class Use the MyBase keyword to refer to the base class Overridable keyword in base class method Indicates that derived class can replace it Overrides keyword in derived class method Indicates that a method in the derived class replaces a method in the base class