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

Slides:



Advertisements
Similar presentations
Sub and Function Procedures
Advertisements

Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 10 Creating Classes and Objects.
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.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 7: Sub and Function Procedures
ASP.NET Programming with C# and SQL Server First Edition
Chapter 13: Object-Oriented Programming
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.
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
Microsoft Visual Basic 2005: Reloaded Second Edition
An Object-Oriented Approach to Programming Logic and Design
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
Chapter 4: The Selection Structure
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Chapter 10: Writing Class Definitions Visual Basic.NET Programming: From Problem Analysis to Program Design.
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.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 7 Sub and Function Procedures.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
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.
Advanced Object- Oriented Programming Programming Right from the Start with Visual Basic.NET 1/e 14.
Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic th Edition.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Chapter 6 OOP: Creating Object-Oriented Programs Programming In Visual Basic.NET.
Chapter 5: More on the Selection Structure
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
1.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
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.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Classes, Interfaces and Packages
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 14: Access Databases and SQL.
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
Chapter 4: The Selection Structure
CIS16 Application Development Programming with Visual Basic
Programming with Microsoft Visual Basic 2008 Fourth Edition
Object-Oriented Programming: Classes and Objects
Object-Oriented Programming: Inheritance and Polymorphism
CIS16 Application Development and Programming using Visual Basic.net
Presentation transcript:

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

Previewing the Woods Manufacturing Application Programming with Microsoft Visual Basic 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

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

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 Object-Oriented Programming Terminology

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 Object-Oriented Programming Terminology (cont.)

Programming with Microsoft Visual Basic 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

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

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

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 Example 1—A Class that Contains Public Variables Only

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

Programming with Microsoft Visual Basic 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.)

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

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

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 Example 2—A Class that Contains Private Variables, Public Properties, and Methods

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 Example 2—A Class that Contains Private Variables, Public Properties, and Methods (cont.)

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 Example 2—A Class that Contains Private Variables, Public Properties, and Methods (cont.)

Programming with Microsoft Visual Basic Figure 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.)

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

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 Example 2—A Class that Contains Private Variables, Public Properties, and Methods (cont.)

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 Example 2—A Class that Contains Private Variables, Public Properties, and Methods (cont.)

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

Programming with Microsoft Visual Basic 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

Programming with Microsoft Visual Basic Figure 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.)

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

Programming with Microsoft Visual Basic Figure 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.)

Programming with Microsoft Visual Basic Figure 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.)

Programming with Microsoft Visual Basic Figure 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.)

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 Example 3—A Class that Contains a Parameterized Constructor Figure Default and parameterized constructors

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

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

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 Example 4—Reusing a Class Figure Interface for the Pete’s Pizzeria application

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

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

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

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

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

Programming with Microsoft Visual Basic 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 Interface for the Grade Calculator application

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

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

Programming with Microsoft Visual Basic 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

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

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

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

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

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

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

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

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

Programming with Microsoft Visual Basic 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

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

Programming with Microsoft Visual Basic 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

Programming with Microsoft Visual Basic 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

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

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

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

Programming with Microsoft Visual Basic 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

Programming with Microsoft Visual Basic 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