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

Slides:



Advertisements
Similar presentations
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 10 Creating Classes and Objects.
Advertisements

Programming with Microsoft Visual Basic 2008 Fourth Edition
Programming with Microsoft Visual Basic th 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.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Chapter 6 OOP: Creating Object-Oriented Programs Programming In Visual Basic.NET.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
Chapter 7: Sub and Function Procedures
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
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
Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Programming with Microsoft Visual Basic 2008 Fourth Edition
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Enhancing the Wage Calculator Application Introducing Function Procedures and.
Chapter 4: The Selection Structure
Programming Pillars Introduction to Object- Oriented Programming.
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.
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.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Chapter 6 OOP: Creating Object-Oriented Programs Programming In Visual Basic.NET.
Programming with Microsoft Visual Basic th Edition
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Programming with Microsoft Visual Basic 2012 Chapter 11: Classes and Objects.
1.
Microsoft Visual Basic 2012 CHAPTER ELEVEN Multiple Classes and Inheritance.
Chapter Four The Selection Structure Programming with Microsoft Visual Basic th Edition.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
Introduction to Object-Oriented Programming Lesson 2.
Programming with Microsoft Visual Basic th Edition
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.
Object-Oriented Programming: Inheritance and Polymorphism.
An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
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
Chapter 1: An Introduction to Visual Basic 2015
Microsoft Visual Basic 2005: Reloaded Second Edition
CIS16 Application Development Programming with Visual Basic
Programming with Microsoft Visual Basic 2008 Fourth Edition
Object-Oriented Programming: Classes and Objects
CIS16 Application Development and Programming using Visual Basic.net
CIS 199 Final Review.
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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