Programming with Microsoft Visual Basic 2008 Fourth Edition

Slides:



Advertisements
Similar presentations
Sub and Function Procedures
Advertisements

Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 10 Creating Classes and Objects.
Programming with Microsoft Visual Basic 2008 Fourth Edition
Chapter 3: Using Variables and Constants Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 11: Classes and Objects
Chapter 7: Sub and Function Procedures
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
1.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Chapter 6 OOP: Creating Object-Oriented Programs Programming In Visual Basic.NET.
Chapter 7: Sub and Function Procedures
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter One An Introduction to Visual Basic 2010.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures.
Programming with Microsoft Visual Basic th Edition CHAPTER SEVEN SUB AND FUNCTION PROCEDURES.
Programming with Microsoft Visual Basic 2012 Chapter 7: Sub and Function Procedures.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.
Chapter 3: Using Variables and Constants
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
Microsoft Visual Basic 2005: Reloaded Second Edition
An Object-Oriented Approach to Programming Logic and Design
Chapter 4: The Selection Structure
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Programming with Microsoft Visual Basic 2008 Fourth Edition
Programming Pillars Introduction to Object- Oriented Programming.
Chapter 10: Structures and Sequential Access Files
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
1 Classes and Controls CE-105 Spring 2007 By: Engr. Faisal ur Rehman.
Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1.
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Advanced Object- Oriented Programming Programming Right from the Start with Visual Basic.NET 1/e 14.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic th Edition.
Chapter 6 OOP: Creating Object-Oriented Programs Programming In Visual Basic.NET.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Programming with Microsoft Visual Basic 2012 Chapter 11: Classes and Objects.
1.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Microsoft Visual Basic 2012 CHAPTER ELEVEN Multiple Classes and Inheritance.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
Programming with Microsoft Visual Basic th Edition
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter One An Introduction to Visual Basic 2008.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects.
Chapter 11 An introduction to object-oriented design.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
Programming with Microsoft Visual Basic 2012 Chapter 3: Using Variables and Constants.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Object-Oriented Programming: Classes and Objects.
Creating Your Own Classes
IS 350 Application Structure
Chapter 1: An Introduction to Visual Basic 2015
Multiple Classes and Inheritance
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 4: The Selection Structure
Object-Oriented Programming: Classes and Objects
CIS16 Application Development Programming with Visual Basic
Object-Oriented Programming: Classes and Objects
Tonga Institute of Higher Education
Object-Oriented Programming: Inheritance and Polymorphism
CIS16 Application Development and Programming using Visual Basic.net
Presentation transcript:

Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eleven Classes and Objects

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

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

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

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

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

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

Creating a Class (continued) Figure 11-2: Syntax and an example of the Class statement Programming with Microsoft Visual Basic 2008, Fourth Edition

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

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

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

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

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

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

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

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

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

Private Variables and Property Procedures (continued) Figure 11-13: Completed Length Property procedure Programming with Microsoft Visual Basic 2008, Fourth Edition

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

Constructors (continued) Figure 11-14: Syntax and an example of a constructor Programming with Microsoft Visual Basic 2008, Fourth Edition

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Example 5—Using a Class that Contains Two Constructors (continued) Figure 11-32: FormattedDate class definition Programming with Microsoft Visual Basic 2008, Fourth Edition

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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