Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming with Microsoft Visual Basic 2012 Chapter 11: Classes and Objects.

Similar presentations


Presentation on theme: "Programming with Microsoft Visual Basic 2012 Chapter 11: Classes and Objects."— Presentation transcript:

1 Programming with Microsoft Visual Basic 2012 Chapter 11: Classes and Objects

2 Previewing the Woods Manufacturing Application Programming with Microsoft Visual Basic 20122 Figure 11-1 Interface showing Charika’s gross pay and informationFigure 11-2 Interface showing Chris’s gross pay and information Calculates a salary for either an hourly worker or a salaried worker

3 Lesson A Objectives 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 20123

4 Object-oriented programming language –Uses objects to accomplish a program’s goal Class –A pattern or blueprint for an object Instance –An object created from a class Instantiated –The process of creating an object from a class Attributes –Characteristics that describe an object Programming with Microsoft Visual Basic 20124 Object-Oriented Programming Terminology

5 Behaviors –Methods and events that define how the object will act or react Methods –Operations (actions) that an object is capable of performing Events –Actions to which an object can respond Encapsulates –To enclose in a capsule –A class encapsulates all attributes and behaviors of an object it instantiates Programming with Microsoft Visual Basic 20125 Object-Oriented Programming Terminology (cont.)

6 Programming with Microsoft Visual Basic 20126 Creating a Class Two types of classes used in VB applications: –Built-in classes, such as the TextBox class –Programmer-defined classes Class statement –Used to define a class –Defines attributes and behaviors of objects created from the class After a class has been defined, it can be used to instantiate objects

7 Programming with Microsoft Visual Basic 20127 Figure 11-4 Class statement entered in the TimeCard.vb class file Figure 11-3 Syntax of the Class statement Creating a Class (cont.)

8 Programming with Microsoft Visual Basic 20128 Creating a Class (cont.) Figure 11-5 Syntax and examples of instantiating an object

9 Norbert Pool & Spa Depot application from Chapter 10 –Input: Length, width, and depth of a pool –Calculates the 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 20129 Example 1—A Class that Contains Public Variables Only

10 Programming with Microsoft Visual Basic 201210 Figure 11-6 Code for the Norbert Pool & Spa Depot application (with a structure) Example 1—A Class that Contains Public Variables Only (cont.)

11 Programming with Microsoft Visual Basic 201211 Figure 11-7 Comments and Option statements entered in the class file Figure 11-8 Public variables included in the IntelliSense list Example 1—A Class that Contains Public Variables Only (cont.)

12 Programming with Microsoft Visual Basic 201212 Figure 11-9 Class statement, GetGallons function, and btnCalc_Click procedure Example 1—A Class that Contains Public Variables Only (cont.)

13 Programming with Microsoft Visual Basic 201213 Figure 11-10 Interface showing the number of gallons Example 1—A Class that Contains Public Variables Only (cont.)

14 Disadvantages of using Public variables in a class: –A class cannot control the values assigned to its Public variables –This violates the concept of encapsulation, in which class behaviors control a class’s data Programming with Microsoft Visual Basic 201214 Example 2—A Class that Contains Private Variables, Public Properties, and Methods

15 Private Variables and Property Procedures Private class variables –Can only be used within the class –Not visible to the rest of the application Public property –Used to refer to (expose) the Private variable for use by other parts of the application Property procedure –Creates a Public property Programming with Microsoft Visual Basic 201215 Example 2—A Class that Contains Private Variables, Public Properties, and Methods (cont.)

16 Private Variables and Property Procedures (cont.) ReadOnly keyword –Indicates an application can read the property’s value –Cannot set the value WriteOnly keyword –Indicates an application can set the property’s value –But it cannot retrieve the value Property procedures contain blocks of code: –Get block: Retrieves the contents of the Private variable –Set block: Used to assign a value to the Private variable –Blocks may be used individually or together Programming with Microsoft Visual Basic 201216 Example 2—A Class that Contains Private Variables, Public Properties, and Methods (cont.)

17 Programming with Microsoft Visual Basic 201217 Figure 11-13 Syntax and examples of a Property procedure Example 2—A Class that Contains Private Variables, Public Properties, and Methods (cont.) Private Variables and Property Procedures (cont.)

18 Programming with Microsoft Visual Basic 201218 Figure 11-14 Length Property procedure entered in the class Example 2—A Class that Contains Private Variables, Public Properties, and Methods (cont.)

19 Constructors Constructor –A class method whose purpose is to initialize the class’s Private variables –Processed each time an object is created –Must be coded as a Sub procedure named New A class can have more than one constructor –The names are the same, but the parameterLists must differ Default constructor –A constructor without parameters Programming with Microsoft Visual Basic 201219 Example 2—A Class that Contains Private Variables, Public Properties, and Methods (cont.)

20 Constructors (cont.) Parameterized constructor –A constructor containing one or more parameters Method’s signature –A method name combined with an optional parameterList Programming with Microsoft Visual Basic 201220 Example 2—A Class that Contains Private Variables, Public Properties, and Methods (cont.)

21 Programming with Microsoft Visual Basic 201221 Figure 11-16 Statements that invoke the constructors shown in Figure 11-15 Figure 11-15 Syntax and examples of a constructor Example 2—A Class that Contains Private Variables, Public Properties, and Methods (cont.) Constructors (cont.)

22 Programming with Microsoft Visual Basic 201222 Example 2—A Class that Contains Private Variables, Public Properties, and Methods (cont.) Methods Other than Constructors May be either Sub or Function procedures –Functions return a value; Sub procedures do not Rules for naming methods: –The name should be entered using Pascal case –The first word in a name should be a verb –Subsequent words should be nouns and adjectives

23 Programming with Microsoft Visual Basic 201223 Figure 11-17 Syntax and examples of a method that is not a constructor Example 2—A Class that Contains Private Variables, Public Properties, and Methods (cont.) Methods Other than Constructors (cont.)

24 Programming with Microsoft Visual Basic 201224 Figure 11-18 Pseudocode for the Calculate button’s Click event procedure Coding the Carpets Galore Application Figure 11-19 TryParse methods entered in the procedure Example 2—A Class that Contains Private Variables, Public Properties, and Methods (cont.)

25 Programming with Microsoft Visual Basic 201225 Figure 11-20 Rectangle class definition and btnCalc_Click procedure (continues) Example 2—A Class that Contains Private Variables, Public Properties, and Methods (cont.) Coding the Carpets Galore Application (cont.)

26 Programming with Microsoft Visual Basic 201226 Figure 11-20 Rectangle class definition and btnCalc_Click procedure Example 2—A Class that Contains Private Variables, Public Properties, and Methods (cont.) Coding the Carpets Galore Application (cont.)

27 Programming with Microsoft Visual Basic 201227 Figure 11-21 Interface showing the square yards and cost Example 2—A Class that Contains Private Variables, Public Properties, and Methods (cont.) Coding the Carpets Galore Application (cont.)

28 A parameterized constructor is simply a constructor that has parameters When a Rectangle object is created, a parameterized constructor allows an application to specify the object’s initial values Programming with Microsoft Visual Basic 201228 Example 3—A Class that Contains a Parameterized Constructor Figure 11-22 Default and parameterized constructors

29 Programming with Microsoft Visual Basic 201229 Figure 11-23 Modified Rectangle class definition and btnCalc_Click procedure (continues) Example 3—A Class that Contains a Parameterized Constructor (cont.)

30 Programming with Microsoft Visual Basic 201230 Figure 11-23 Modified Rectangle class definition and btnCalc_Click procedure Figure 11-24 Square yards and cost shown in the interface Example 3—A Class that Contains a Parameterized Constructor (cont.)

31 Rectangle class from Examples 2 and 3: –Reused here to represent a square pizza –A square is a rectangle with four equal sides Using an object for more than one purpose saves programming time and money –An advantage of object-oriented programming Programming with Microsoft Visual Basic 201231 Example 4—Reusing a Class Figure 11-25 Interface for the Pete’s Pizzeria application

32 Programming with Microsoft Visual Basic 201232 Figure 11-26 Pseudocode for the Calculate button’s Click event procedure Example 4—Reusing a Class (cont.)

33 Programming with Microsoft Visual Basic 201233 Figure 11-27 btnCalc_Click procedure Example 4—Reusing a Class (cont.) Figure 11-28 Number of pizza slices shown in the interface

34 Lesson A Summary The Class statement is used to define a class Defined classes are added to a PROJECT with a.vb extension Objects are instantiated from a defined class The Get block allows an application to retrieve the contents of the Private variable associated with the Property procedure The Set block allows an application to assign a value to the Private variable associated with the Property procedure Programming with Microsoft Visual Basic 201234

35 Lesson A Summary (cont.) A constructor initializes the variables of a class –The constructor method must be named New –The default constructor has no parameters –A class may have many parameterized constructors A class can have methods other than constructors Programming with Microsoft Visual Basic 201235

36 Lesson B Objectives 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 201236

37 Programming with Microsoft Visual Basic 201237 Example 5—A Class that Contains a ReadOnly Property ReadOnly keyword –Indicates that the property’s value can be retrieved (read) but not set (written) The ReadOnly property gets its value from the class instead of from the application Grade Calculator application –Returns a letter grade based on a numeric grade Figure 11-33 Interface for the Grade Calculator application

38 Programming with Microsoft Visual Basic 201238 Figure 11-34 ReadOnly property message Figure 11-35 CourseGrade class definition and btnDisplay_Click procedure (continues) Example 5—A Class that Contains a ReadOnly Property (cont.)

39 Programming with Microsoft Visual Basic 201239 Figure 11-35 CourseGrade class definition and btnDisplay_Click procedure Figure 11-36 Grade shown in the interface (continued) Example 5—A Class that Contains a ReadOnly Property (cont.)

40 Programming with Microsoft Visual Basic 201240 Example 6—A Class that Contains Auto-Implemented Properties Auto-implemented properties feature –Enables you to specify the property of a class in one line of code Visual Basic automatically creates a hidden Private variable that it associates with the property It also automatically creates hidden Get and Set blocks It provides a shorter syntax to use when creating a class –You don’t need to create the Private variable associated with a property, nor do you need to enter the property’s Get and Set blocks of code

41 Programming with Microsoft Visual Basic 201241 Figure 11-37 Syntax and examples of creating an auto-implemented property Example 6—A Class that Contains Auto-Implemented Properties (cont.)

42 Programming with Microsoft Visual Basic 201242 Figure 11-38 Modified CourseGrade class definition Example 6—A Class that Contains Auto-Implemented Properties (cont.)

43 Overloaded methods –Methods with the same name but different parameters Programming with Microsoft Visual Basic 201243 Example 7—A Class that Contains Overloaded Methods Figure 11-39 Attributes and behaviors of an Employee object

44 Programming with Microsoft Visual Basic 201244 Figure 11-40 Employee class definition Example 7—A Class that Contains Overloaded Methods (cont.)

45 Programming with Microsoft Visual Basic 201245 Figure 11-42 Interface for the Woods Manufacturing application Example 7—A Class that Contains Overloaded Methods (cont.)

46 Programming with Microsoft Visual Basic 201246 Figure 11-43 Pseudocode for the Calculate button’s Click event procedure Example 7—A Class that Contains Overloaded Methods (cont.)

47 Programming with Microsoft Visual Basic 201247 Figure 11-45 btnCalc_Click procedure Example 7—A Class that Contains Overloaded Methods (cont.)

48 Programming with Microsoft Visual Basic 201248 Figure 11-46 Jake’s gross pay and information shown in the interface Figure 11-47 Sherri’s gross pay and information shown in the interface Example 7—A Class that Contains Overloaded Methods (cont.)

49 Programming with Microsoft Visual Basic 201249 Lesson B Summary Use the ReadOnly keyword to create a property whose value an application can only retrieve To specify the property of a class in one line: –Create an auto-implemented property using the syntax Public Property propertyName As dataType To include a parameterized method in a class: –Enter the parameters between the parentheses that follow the method’s name To create two or more methods that perform the same task but require different parameters: –Overload the methods by giving them the same name but different parameterLists

50 Lesson C Objectives 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 201250

51 Programming with Microsoft Visual Basic 201251 Example 8—Using a Base Class and a Derived Class Inheritance –Using one class to create another Base class –The original class providing behaviors and attributes Derived class –The new class that inherits attributes and behaviors from the base class Inherits clause –Enables a derived class to inherit from a base class –Included in the derived class’s Class statement

52 Programming with Microsoft Visual Basic 201252 Example 8—Using a Base Class and a Derived Class (cont.) Overriding –Allows a derived class to replace a method inherited from its base class –The base class method header must include the Overridable keyword –The derived class method header must include the Overrides keyword MyBase keyword –Used to refer to the base class MyBase.New ( [parameterList] ) –Used in a derived class to call its base class’s constructor

53 Programming with Microsoft Visual Basic 201253 Figure 11-51 Contents of the Shapes.vb file Example 8—Using a Base Class and a Derived Class (cont.)

54 Programming with Microsoft Visual Basic 201254 Figure 11-52 Modified Square and Cube class definitions Example 8—Using a Base Class and a Derived Class (cont.)

55 Programming with Microsoft Visual Basic 201255 Figure 11-54 btnSquare_Click and btnCube_Click procedures Figure 11-53 Interface showing the square’s area Example 8—Using a Base Class and a Derived Class (cont.)

56 Programming with Microsoft Visual Basic 201256 Lesson C Summary To allow a derived class to inherit the attributes and behaviors of a base class: –Enter the Inherits clause immediately below the Public Class clause in the derived class –The Inherits clause is the keyword Inherits followed by the name of the base class Use the MyBase keyword to refer to the base class To indicate that a method in the base class can be overridden (replaced) in the derived class: –Use the Overridable keyword in the method’s header in the base class

57 Programming with Microsoft Visual Basic 201257 Lesson C Summary (cont.) To indicate that a method in the derived class overrides (replaces) a method in the base class: –Use the Overrides keyword in the method’s header in the derived class


Download ppt "Programming with Microsoft Visual Basic 2012 Chapter 11: Classes and Objects."

Similar presentations


Ads by Google