Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic 2010 5 th Edition.

Similar presentations


Presentation on theme: "Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic 2010 5 th Edition."— Presentation transcript:

1 Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic 2010 5 th Edition

2 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

3 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

4 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

5 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

6 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

7 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

8 Programming with Microsoft Visual Basic 2010, 5 th Edition Creating a Class (cont’d.) 8 Figure 11-2 Syntax of the Class statement

9 Programming with Microsoft Visual Basic 2010, 5 th Edition 9 Figure 11-4 Syntax and examples of instantiating an object from a class

10 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

11 Programming with Microsoft Visual Basic 2010, 5 th Edition 11 Figure 11-5 Code for the Willow Pools application (with a structure) (continues)

12 Programming with Microsoft Visual Basic 2010, 5 th Edition 12 Figure 11-5 Code for the Willow Pools application (with a structure) (cont’d.)

13 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

14 Programming with Microsoft Visual Basic 2010, 5 th Edition 14 Figure 11-8 Class definition, GetGallons function, and btnCalc control’s Click event procedure

15 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

16 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

17 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

18 Programming with Microsoft Visual Basic 2010, 5 th Edition 18 Figure 11-12 Syntax and examples of a Property procedure (continues)

19 Programming with Microsoft Visual Basic 2010, 5 th Edition 19 Figure 11-12 Syntax and examples of a Property procedure (cont’d.)

20 Programming with Microsoft Visual Basic 2010, 5 th Edition Private Variables and Property Procedures (cont’d.) 20 Figure 11-13 Length Property procedure entered in the class

21 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

22 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

23 Programming with Microsoft Visual Basic 2010, 5 th Edition 23 Figure 11-14 Syntax and examples of a constructor

24 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

25 Programming with Microsoft Visual Basic 2010, 5 th Edition Methods Other Than Constructors (cont’d.) 25 Figure 11-15 Syntax and examples of a method that is not a constructor

26 Programming with Microsoft Visual Basic 2010, 5 th Edition Coding the Carpet Haven Application 26 Figure 11-16 Pseudocode for the Calculate button’s Click event procedure

27 Programming with Microsoft Visual Basic 2010, 5 th Edition Coding the Carpet Haven Application (cont’d.) 27 Figure 11-17 TryParse methods entered in the procedure

28 Programming with Microsoft Visual Basic 2010, 5 th Edition 28 Figure 11-18 Rectangle class definition and btnCalc control’s Click event procedure (continues)

29 Programming with Microsoft Visual Basic 2010, 5 th Edition 29 Figure 11-18 Rectangle class definition and btnCalc control’s Click event procedure (cont’d.)

30 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

31 Programming with Microsoft Visual Basic 2010, 5 th Edition Example 3—A Class that Contains a Parameterized Constructor (cont’d.) 31 Figure 11-20 Default and parameterized constructors

32 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

33 Programming with Microsoft Visual Basic 2010, 5 th Edition Example 4—Reusing a Class (cont’d.) 33 Figure 11-22 Pizza Roma application’s interface

34 Programming with Microsoft Visual Basic 2010, 5 th Edition 34 Figure 11-23 Pseudocode for the btnCalc control’s Click event procedure

35 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

36 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

37 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

38 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

39 Programming with Microsoft Visual Basic 2010, 5 th Edition Example 5—A Class that Contains a ReadOnly Property (cont’d.) 39 Figure 11-30 Interface for the Grade Calculator application

40 Programming with Microsoft Visual Basic 2010, 5 th Edition Example 5—A Class that Contains a ReadOnly Property (cont’d.) 40 Figure 11-31 ReadOnly property message

41 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

42 Programming with Microsoft Visual Basic 2010, 5 th Edition Example 6—A Class that Contains Auto- Implemented Properties (cont’d.) 42 Figure 11-34 Syntax and examples of creating an auto-implemented property

43 Programming with Microsoft Visual Basic 2010, 5 th Edition 43 Figure 11-35 Modified CourseGrade class definition

44 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

45 Programming with Microsoft Visual Basic 2010, 5 th Edition Example 7—A Class that Contains Overloaded Methods (cont’d.) 45 Figure 11-36 Attributes and behaviors of an Employee object

46 Programming with Microsoft Visual Basic 2010, 5 th Edition 46 Figure 11-37 Employee class definition

47 Programming with Microsoft Visual Basic 2010, 5 th Edition Example 7—A Class that Contains Overloaded Methods (cont’d.) 47 Figure 11-38 First of the ToString method’s signatures

48 Programming with Microsoft Visual Basic 2010, 5 th Edition Example 7—A Class that Contains Overloaded Methods (cont’d.) 48 Figure 11-39 Interface for the ABC Company application

49 Programming with Microsoft Visual Basic 2010, 5 th Edition 49 Figure 11-40 Pseudocode for the Calculate button’s Click event procedure

50 Programming with Microsoft Visual Basic 2010, 5 th Edition Example 7—A Class that Contains Overloaded Methods (cont’d.) 50 Figure 11-41 Additional comment and code entered in the false path

51 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

52 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

53 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

54 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

55 Programming with Microsoft Visual Basic 2010, 5 th Edition 55 Figure 11-48 Contents of the Shapes.vb file

56 Programming with Microsoft Visual Basic 2010, 5 th Edition 56 Figure 11-49 Modified Square class and Cube class definitions

57 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


Download ppt "Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic 2010 5 th Edition."

Similar presentations


Ads by Google