chapter eleven classes and objects programming with microsoft visual basic 2010 5 th edition

Click here to load reader

Upload: bertram-parker

Post on 04-Jan-2016

217 views

Category:

Documents


1 download

TRANSCRIPT

Programming with Microsoft Visual Basic 2010 Fifth Edition

Chapter ElevenClasses and ObjectsProgramming with Microsoft Visual Basic 2010 5th Edition1Previewing the ABC Company Application2Open the ABC.exe file ABC Company application calculates and displays gross pay for salaried and hourly employeesSalaried employees are paid twice per monthHourly employees are paid weekly

Programming with Microsoft Visual Basic 2010, 5th Edition2Previewing the ABC Company Application (contd.)3Figure 11-1 Interface showing Sarahs gross pay and information

Programming with Microsoft Visual Basic 2010, 5th Edition3Lesson A Objectives4After studying Lesson A, you should be able to: Explain the terminology used in object-oriented programmingCreate a classInstantiate an objectAdd Property procedures to a classInclude data validation in a classCreate a default constructorCreate a parameterized constructorInclude methods other than constructors in a class

Programming with Microsoft Visual Basic 2010, 5th Edition4Object-Oriented Programming Terminology5Object-oriented programming languageUses objects to accomplish programs goalClass: Pattern or blueprint for objectInstance: Object created from classInstantiation: Process of creating an object from a classAttributes: Characteristics that describe the objectBehaviors: Methods and events that define how the object will act or react

Programming with Microsoft Visual Basic 2010, 5th EditionObject-Oriented Programming Terminology (contd.)6MethodsOperations (actions) that object is capable of performingEventsActions to which an object can respondEncapsulate: To enclose in a capsuleA class encapsulates all attributes and behaviors of object it instantiatesProgramming with Microsoft Visual Basic 2010, 5th EditionCreating a Class7Two types of classes used in VB applicationsBuilt-in classes, such as TextBox class Programmer-defined classes Class statementUsed to define a classDefines attributes and behaviors of objects created from the classAfter class has been defined, it can be used to instantiate objectsProgramming with Microsoft Visual Basic 2010, 5th Edition7Creating a Class (contd.)8Figure 11-2 Syntax of the Class statement

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

Programming with Microsoft Visual Basic 2010, 5th Edition9Example 1A Class that Contains Public Variables Only10Willow Pools application from Chapter 10Input: Length, width, and depth of poolCalculates volume of water requiredThis program was coded with a structure in Chapter 10The structure will be replaced with a classProgramming with Microsoft Visual Basic 2010, 5th Edition1011Figure 11-5 Code for the Willow Pools application (with a structure) (continues)

Programming with Microsoft Visual Basic 2010, 5th Edition1112Figure 11-5 Code for the Willow Pools application (with a structure) (contd.)

Programming with Microsoft Visual Basic 2010, 5th Edition12Example 1Using a Class that Contains Public Variables Only (contd.)13Figure 11-6 Comments and Option statements entered in the class file

Programming with Microsoft Visual Basic 2010, 5th Edition1314Figure 11-8 Class definition, GetGallons function, and btnCalc controls Click event procedure

Programming with Microsoft Visual Basic 2010, 5th Edition14Example 2A Class that Contains Private Variables, Public Properties, and Methods 15Disadvantages of Public variables in classClass cannot control values assigned to Public variablesViolates concept of encapsulation, in which class behaviors control classs dataProgramming with Microsoft Visual Basic 2010, 5th Edition15Private Variables and Property Procedures16Private class variablesCan only be used within classNot visible to rest of the applicationPublic propertyUsed to refer to (expose) Private variable for use by other parts of applicationProperty procedureCreates a Public property

Programming with Microsoft Visual Basic 2010, 5th EditionPrivate Variables and Property Procedures (contd.)17ReadOnly keywordIndicates application can read propertys valueCannot set the valueWriteOnly keywordIndicates application can set the propertys valueBut cannot retrieve the valueProperty procedure contains blocks of codeGet block: Retrieves contents of Private variable Set block: Used to assign value to Private variable Blocks may be used individually or togetherProgramming with Microsoft Visual Basic 2010, 5th Edition1718

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

Figure 11-12 Syntax and examples of a Property procedure (contd.)Programming with Microsoft Visual Basic 2010, 5th EditionPrivate Variables and Property Procedures (contd.)20Figure 11-13 Length Property procedure entered in the class

Programming with Microsoft Visual Basic 2010, 5th EditionConstructors21ConstructorClass method whose purpose is to initialize classs Private variablesProcessed each time an object is created Must be coded as Sub procedure named NewClass can have more than one constructorNames are same, but parameter lists must differ Default constructorConstructor without parameters Programming with Microsoft Visual Basic 2010, 5th Edition21Constructors (contd.)22Parameterized constructorConstructor containing one or more parametersMethods signatureMethod name combined with optional parameter listProgramming with Microsoft Visual Basic 2010, 5th Edition2223Figure 11-14 Syntax and examples of a constructor

Programming with Microsoft Visual Basic 2010, 5th Edition23Methods Other Than Constructors24A class may contain methods other than constructorsMay be either Sub or Function proceduresFunctions return value; Sub procedures do notRules for naming methodsName should be entered using Pascal caseFirst word in name should be verbSubsequent words should be nouns and adjectivesProgramming with Microsoft Visual Basic 2010, 5th Edition24Methods Other Than Constructors (contd.)25Figure 11-15 Syntax and examples of a method that is not a constructor

Programming with Microsoft Visual Basic 2010, 5th Edition25Coding the Carpet Haven Application26Figure 11-16 Pseudocode for the Calculate buttons Click event procedure

Programming with Microsoft Visual Basic 2010, 5th Edition26Coding the Carpet Haven Application (contd.)27Figure 11-17 TryParse methods entered in the procedure

Programming with Microsoft Visual Basic 2010, 5th Edition28Figure 11-18 Rectangle class definition and btnCalc controls Click event procedure (continues)

Programming with Microsoft Visual Basic 2010, 5th Edition29Figure 11-18 Rectangle class definition and btnCalc controls Click event procedure (contd.)

Programming with Microsoft Visual Basic 2010, 5th EditionExample 3A Class that Contains a Parameterized Constructor30Add a parameterized constructor to the Rectangle class from Example 2Programming with Microsoft Visual Basic 2010, 5th Edition30Example 3A Class that Contains a Parameterized Constructor (contd.)31Figure 11-20 Default and parameterized constructors

Programming with Microsoft Visual Basic 2010, 5th Edition31Example 4Reusing a Class32Rectangle class from Examples 2 and 3:Reused here to represent a square pizzaSquare is rectangle with four equal sidesAbility to use object for more than one purpose:Saves programming time and moneyAdvantage of object-oriented programmingProgramming with Microsoft Visual Basic 2010, 5th EditionExample 4Reusing a Class (contd.)33Figure 11-22 Pizza Roma applications interface

Programming with Microsoft Visual Basic 2010, 5th Edition34Figure 11-23 Pseudocode for the btnCalc controls Click event procedure

Programming with Microsoft Visual Basic 2010, 5th EditionLesson A Summary35Class statement is used to define classDefined classes are saved in files with a .vb extensionObjects are instantiated from defined classAccess to Private class variables should be done with Public propertiesPublic properties are created using Property procedure

Programming with Microsoft Visual Basic 2010, 5th Edition35Lesson A Summary (contd.)36Constructor initializes variables of a classConstructor method must be named NewDefault constructor has no parametersClass may have many parameterized constructorsClass can have methods other than constructorsProgramming with Microsoft Visual Basic 2010, 5th Edition36Lesson B Objectives37After studying Lesson B, you should be able to:Include a ReadOnly property in a classCreate an auto-implemented propertyOverload a method in a class

Programming with Microsoft Visual Basic 2010, 5th Edition37Example 5A Class that Contains a ReadOnly Property38ReadOnly keywordIndicates that propertys value can be retrieved (read) but not set (written)ReadOnly property gets its value from class instead of from applicationGrade Calculator applicationReturns letter grade based on numeric gradeProgramming with Microsoft Visual Basic 2010, 5th EditionExample 5A Class that Contains a ReadOnly Property (contd.)39Figure 11-30 Interface for the Grade Calculator application

Programming with Microsoft Visual Basic 2010, 5th EditionExample 5A Class that Contains a ReadOnly Property (contd.)40Figure 11-31 ReadOnly property message

Programming with Microsoft Visual Basic 2010, 5th EditionExample 6A Class that Contains Auto-Implemented Properties41Auto-implemented propertiesNew feature of Visual Basic 2010Enables specifying property of a class in one line of codeProgramming with Microsoft Visual Basic 2010, 5th Edition41Example 6A Class that Contains Auto-Implemented Properties (contd.)42

Figure 11-34 Syntax and examples of creating an auto-implemented propertyProgramming with Microsoft Visual Basic 2010, 5th Edition43

Figure 11-35 Modified CourseGrade class definitionProgramming with Microsoft Visual Basic 2010, 5th EditionExample 7A Class that Contains Overloaded Methods44Overloaded methodsMethods with same name but having different parametersCan overload any methods in a classNot just constructorsProgramming with Microsoft Visual Basic 2010, 5th Edition44Example 7A Class that Contains Overloaded Methods (contd.)45

Figure 11-36 Attributes and behaviors of an Employee objectProgramming with Microsoft Visual Basic 2010, 5th Edition4546

Figure 11-37 Employee class definitionProgramming with Microsoft Visual Basic 2010, 5th EditionExample 7A Class that Contains Overloaded Methods (contd.)47Figure 11-38 First of the ToString methods signatures

Programming with Microsoft Visual Basic 2010, 5th Edition47Example 7A Class that Contains Overloaded Methods (contd.)48Figure 11-39 Interface for the ABC Company application

Programming with Microsoft Visual Basic 2010, 5th Edition4849Figure 11-40 Pseudocode for the Calculate buttons Click event procedure

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

Programming with Microsoft Visual Basic 2010, 5th Edition50Lesson B Summary51Use ReadOnly keyword to create property that can be retrieved but not set by applicationTo 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 methods nameOverloaded methods share one name but have different parameter listsProgramming with Microsoft Visual Basic 2010, 5th Edition51Lesson C Objectives52After studying Lesson C, you should be able to:Create a derived classRefer to the base class using the MyBase keywordOverride a method in the base class

Programming with Microsoft Visual Basic 2010, 5th Edition52Example 8Using a Base Class and a Derived Class53InheritanceUsing one class to create anotherBase classOriginal class providing behaviors and attributesDerived classNew class that inherits attributes and behaviors from base classInherits clauseEnables derived class to inherit from base classIncluded in derived classs Class statementProgramming with Microsoft Visual Basic 2010, 5th Edition53Example 8Using a Base Class and a Derived Class (contd.)54OverridingAllows derived class to replace method inherited from its base classBase class method header must include Overridable keywordDerived class method header must include Overrides keywordMyBase keyword: Used to refer to base classMyBase.New ([parameterlist]): Used in derived class to call its base classs constructorProgramming with Microsoft Visual Basic 2010, 5th Edition5455Figure 11-48 Contents of the Shapes.vb file

Programming with Microsoft Visual Basic 2010, 5th Edition56

Figure 11-49 Modified Square class and Cube class definitionsProgramming with Microsoft Visual Basic 2010, 5th EditionLesson C Summary57Use Inherits clause to allow derived class to inherit attributes and behaviors of base classUse the MyBase keyword to refer to the base classOverridable keyword in base class methodIndicates that derived class can replace itOverrides keyword in derived class methodIndicates that a method in the derived class replaces a method in the base classProgramming with Microsoft Visual Basic 2010, 5th Edition57