Upload
sabrina-beryl-caldwell
View
216
Download
1
Tags:
Embed Size (px)
Citation preview
Microsoft Visual Basic 2012
CHAPTER ELEVEN
Multiple Classesand Inheritance
11
Chapter 11: Multiple Classes and Inheritance 2
Objectives
►Use the TabIndex Property►Edit input, including MaskedTextBox,TextBox, and
ComboBox objects►Describe the three-tier program structure►Understand a class►Create a class
11
Chapter 11: Multiple Classes and Inheritance 3
Objectives
►Instantiate an object►Pass arguments when instantiating an object►Write a class constructor►Call a procedure in a separate class
11
Chapter 11: Multiple Classes and Inheritance 4
Objectives
►Code a base class and a subclass to incorporate inheritance
►Call procedures found in a base class and a subclass
►Write overridable and overrides procedures►Create and write a comma-delimited text file
11
Chapter 11: Multiple Classes and Inheritance 5
Use the TabIndex Property in the User Interface
►Select the object that will be selected when program execution begins. In the example, this is the txtStudentID MaskedTextBox object. Scroll in the Properties window until the TabIndex property is visible and then double-tap or double-click in the right column of the TabIndex property
►Type 1 and then press the ENTER key►Select the object which should be selected when the user
presses the TAB key. In the sample program, the txtStudentName TextBox object should be selected. Double-tap or double-click the right column of the TabIndex property for the txtStudentName TextBox object, type 2 and then press the ENTER key
11
Chapter 11: Multiple Classes and Inheritance 6
Use the TabIndex Property in the User Interface
11
Chapter 11: Multiple Classes and Inheritance 7
Editing Input Data
► Student ID: The student ID object is a masked text box and the mask is for the Social Security number, so the mask ensures that the user can enter only numbers. However, the Social Security mask does not ensure the user enters all nine numbers. Therefore, a check must be included in the program to require the user to enter all 9 numeric digits.
► Student Name: The program must ensure the user enters characters in this TextBox object. In addition, spaces cannot be entered instead of actual alphabetic characters.
► Number of Units: The user must enter a numeric value from 1 through 24 for the number of units the student is taking.
► Major: The user must select a major from the list in the Major ComboBox object.
11
Chapter 11: Multiple Classes and Inheritance 8
Editing Input Data
11
Chapter 11: Multiple Classes and Inheritance 9
Program Structure Using Classes
►The concept of separating processing and hiding data within specific classes is called encapsulation
►When developing programs with multiple classes, a starting point for determining what classes should appear in a program is the three-tier program structure
11
Chapter 11: Multiple Classes and Inheritance 10
Program Structure Using Classes
►The presentation tier contains the classes that display information for the user and accept user input
►The business tier contains the logic and calculations that must occur in order to fulfill the requirements of the program
►The persistence tier, sometimes called the data access tier, contains the code required to read and write data from permanent storage
11
Chapter 11: Multiple Classes and Inheritance 11
Sample Program Classes
►Presentation tier: The presentation tier contains the frmCollege class. This class displays the user interface in a Windows Form object and also edits the user input data to ensure its validity
►Business tier: The business tier contains two classes: the Student class and the OnCampusStudent class. The Student class contains data for each registered student and calculates the registration costs for some students. The OnCampusStudent class is used for registered students who live in on-campus residence halls
►Persistence tier: The persistence tier consists of one class, StudentCostsFile, which creates and writes the Student Costs File
11
Chapter 11: Multiple Classes and Inheritance 12
Creating a Class
►With Visual Studio open and a Window Application project started, press and hold or right-click the project name in the Solution Explorer window and then point to Add on the shortcut menu
►Tap or click Class on the Add submenu►In the Name text box, type Student as the name
of the class and then tap or click the Add button►Using the same techniques, create the
OnCampusStudent class and the StudentCostsFile classes
11
Chapter 11: Multiple Classes and Inheritance 13
Creating a Class
11
Chapter 11: Multiple Classes and Inheritance 14
Instantiating a Class and Class Communication
►A class acts as a template for an object
11
Chapter 11: Multiple Classes and Inheritance 15
Constructors in New Instantiated Classes
►When a class is instantiated into an object using the New keyword, a special procedure called a constructor is executed in the class
►The constructor prepares the object for use in the program
11
Chapter 11: Multiple Classes and Inheritance 16
Passing Arguments when Instantiating an Object
►Often when instantiating an object, data must be passed to the object
►In the Student class, the New statement must be written with corresponding arguments; that is, the “signature” of the instantiating statement must be the same as the constructor heading in the class
11
Chapter 11: Multiple Classes and Inheritance 17
Passing Arguments when Instantiating an Object
11
Chapter 11: Multiple Classes and Inheritance 18
Calling a Procedure in a Separate Class
►Most of the time, separate classes in a program contain procedures that must be executed
11
Chapter 11: Multiple Classes and Inheritance 19
Inheritance
► Inheritance allows one class to inherit attributes and procedures from another class
11
Chapter 11: Multiple Classes and Inheritance 20
Inheritance
11
Chapter 11: Multiple Classes and Inheritance 21
Inheritance
11
Chapter 11: Multiple Classes and Inheritance 22
Constructors
11
Chapter 11: Multiple Classes and Inheritance 23
Constructors
11
Chapter 11: Multiple Classes and Inheritance 24
Inheritance and Procedures
►When using inheritance, the subclass can use the procedures within the base class as well as its variables
►Between the base class and the subclass, five different techniques can be used for referencing and calling a procedure from an outside class such as a Form class
►After the base class and the subclass have been instantiated, the following techniques are available:
11
Chapter 11: Multiple Classes and Inheritance 25
Inheritance and Procedures
►Base Class• Call a named procedure in the base class• Call an Overridable procedure in the base
class►Subclass• Call an Overridable Procedure in the subclass• Call a named procedure in the subclass• Call a base class procedure in the subclass
11
Chapter 11: Multiple Classes and Inheritance 26
Call a Named Procedure in the Base Class
11
Chapter 11: Multiple Classes and Inheritance 27
Calling an Overridable Procedure in a Base Class
11
Chapter 11: Multiple Classes and Inheritance 28
Calling an Overridable Procedure in a Subclass
11
Chapter 11: Multiple Classes and Inheritance 29
Calling a Named Procedure in the Subclass
11
Chapter 11: Multiple Classes and Inheritance 30
Calling a Base Class Procedure in the Subclass
11
Chapter 11: Multiple Classes and Inheritance 31
Persistence Classes
►The persistence tier in an application, sometimes called the data access tier, contains classes that are involved in saving and retrieving data that is stored on a permanent storage medium such as a hard disk or a USB drive
11
Chapter 11: Multiple Classes and Inheritance 32
Persistence Classes
11
Chapter 11: Multiple Classes and Inheritance 33
Comma-Delimited Text File
11
Chapter 11: Multiple Classes and Inheritance 34
Comma-Delimited Text File
11
Chapter 11: Multiple Classes and Inheritance 35
Program Design
11
Chapter 11: Multiple Classes and Inheritance 36
Program Design
11
Chapter 11: Multiple Classes and Inheritance 37
Program Design
11
Chapter 11: Multiple Classes and Inheritance 38
Event Planning Document
11
Chapter 11: Multiple Classes and Inheritance 39
Event Planning Document
11
Chapter 11: Multiple Classes and Inheritance 40
Event Planning Document
11
Chapter 11: Multiple Classes and Inheritance 41
Event Planning Document
11
Chapter 11: Multiple Classes and Inheritance 42
Event Planning Document
11
Chapter 11: Multiple Classes and Inheritance 43
Event Planning Document
11
Chapter 11: Multiple Classes and Inheritance 44
Summary
►Use the TabIndex Property►Edit input, including MaskedTextBox,TextBox, and
ComboBox objects►Describe the three-tier program structure►Understand a class►Create a class
11
Chapter 11: Multiple Classes and Inheritance 45
Summary
►Instantiate an object►Pass arguments when instantiating an object►Write a class constructor►Call a procedure in a separate class
11
Chapter 11: Multiple Classes and Inheritance 46
Summary
►Code a base class and a subclass to incorporate inheritance
►Call procedures found in a base class and a subclass
►Write overridable and overrides procedures►Create and write a comma-delimited text file
Microsoft Visual Basic 2012
CHAPTER ELEVEN COMPLETE
Multiple Classesand Inheritance