Chapter 10: Writing Class Definitions Visual Basic.NET Programming: From Problem Analysis to Program Design.

Slides:



Advertisements
Similar presentations
Chapter 11: Classes and Objects
Advertisements

Chapter 7: Sub and Function Procedures
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Chapter 11: Implementing Inheritance and Association Visual Basic.NET Programming: From Problem Analysis to Program Design.
More about classes and objects Classes 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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
Object-Oriented Application Development Using VB.NET 1 Chapter 7 Adding Responsibilities to Problem Domain Classes.
Chapter 12: Advanced Topics: Exception Handling Visual Basic.NET Programming: From Problem Analysis to Program Design.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm – – –Web Site: websupport1.citytech.cuny.edu.
Guide to Programming with Python
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
Module 7: Object-Oriented Programming in Visual Basic .NET
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Chapter 9: Writing Procedures Visual Basic.NET Programming: From Problem Analysis to Program Design.
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.
Object-Oriented Application Development Using VB.NET 1 Chapter 6 Writing a Problem Domain Class Definition.
Object-Oriented Application Development Using VB.NET 1 Chapter 6 Writing a Problem Domain Class Definition.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Classes and Multiform Projects.
Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic th Edition.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
1 Working with Objects. 2 Defining a Class in VB.NET A class is a user-defined data type You can declare it as part of a module, but usually in a separate.
Dr. Azeddine Chikh IS444: Modern tools for applications development.
CSCI 3327 Visual Basic Chapter 9: Object-Oriented Programming: Classes and Objects UTPA – Fall 2011.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Programming with Microsoft Visual Basic 2012 Chapter 11: Classes and Objects.
VB Classes ISYS 512/812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 5 Introduction to Defining Classes
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object-Oriented Programming: Classes and Objects.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
 2007 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Object-Oriented Programming: Classes and Objects
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 3: Using Methods, Classes, and Objects
Corresponds with Chapter 7
Programming with Microsoft Visual Basic 2008 Fourth Edition
Chapter 9 Objects and Classes
The Object-Oriented Thought Process Chapter 04
Defining Classes and Methods
CIS16 Application Development and Programming using Visual Basic.net
Classes and Objects CGS3416 Spring 2019.
Presentation transcript:

Chapter 10: Writing Class Definitions Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic.NET Programming: From Problem Analysis to Program Design2 Objectives Examine the three-tier design model Write a class definition Design and write overloaded constructors Create polymorphic methods Write properties Create shared attributes and methods

Visual Basic.NET Programming: From Problem Analysis to Program Design3 Examining the Three-Tier Design Model Three-tier design –Places objects in OO system into three categories of classes: Problem domain GUI Data access –Developers define categories of classes when designing and building system

Visual Basic.NET Programming: From Problem Analysis to Program Design4

5 Writing a Class Definition Class definition –VB.NET code representing a class Attributes –Variables populated with data describing object Methods –Model object’s behavior

Visual Basic.NET Programming: From Problem Analysis to Program Design6 Drawing a Class Diagram Class diagram –UML diagram –Represents class –Rectangular shape –Contains: Class name Attributes Methods

Visual Basic.NET Programming: From Problem Analysis to Program Design7

8 Class Definition Syntax Class header Attribute definitions Method code End Class keyword

Visual Basic.NET Programming: From Problem Analysis to Program Design9

10 Defining Attributes Define attributes by declaring variables Similar to variable declaration –Replace Dim keyword with access keyword: Public Private –Example: Public name As String

Visual Basic.NET Programming: From Problem Analysis to Program Design11 Example 10-1: Class Definition for Customer 1. Public Class Customer 2. Public name As String 3. Public address As String 4. Public phoneNo As String 5. End Class

Visual Basic.NET Programming: From Problem Analysis to Program Design12 Naming Conventions Class names –Begin with capital letter –Examples: Customer Student

Visual Basic.NET Programming: From Problem Analysis to Program Design13 Naming Conventions (continued) Attribute names –Begin with lowercase character –Subsequent words in start with capital letter –Examples: address phoneNo

Visual Basic.NET Programming: From Problem Analysis to Program Design14 Naming Conventions (continued) Method names –Begin with capital letter –Subsequent words also start with capital letter –Usually contain imperative verb describing what method does followed by noun –Examples: GetPhoneNo SetAddress ComputeLease

Visual Basic.NET Programming: From Problem Analysis to Program Design15 Instantiating a Class Create instances of class –Each instance represents specific occurrence of class Use New keyword: –aCustomer = New Customer

Visual Basic.NET Programming: From Problem Analysis to Program Design16 Example 10-2: Client Module to Instantiate Customer (excerpt) 4. Sub Main() 5. Dim aCustomer As Customer 6. ' create a Customer instance 7. aCustomer = New Customer 8. ' populate the attributes 9. aCustomer.name = “Eleanor” 10. aCustomer.address = “Atlanta” 11. aCustomer.phoneNo = “ ”

Visual Basic.NET Programming: From Problem Analysis to Program Design17

Visual Basic.NET Programming: From Problem Analysis to Program Design18

Visual Basic.NET Programming: From Problem Analysis to Program Design19 Writing Accessor Methods Encapsulation –Requires that internal data and structure of class be hidden from outside objects –Requires that class have methods providing access to attributes Attributes should be Private

Visual Basic.NET Programming: From Problem Analysis to Program Design20 Writing Accessor Methods (continued) Accessor methods –Provide access to attributes –Often called standard methods –Not shown on class diagram Assumed to exist Custom methods –Perform functions for class –Shown on class diagram

Visual Basic.NET Programming: From Problem Analysis to Program Design21 Writing Accessor Methods (continued) Accessor method types: –Get Also called Getter Named Get + attribute name Function procedure –Set Also called Setter Named Set + attribute name Sub procedure

Visual Basic.NET Programming: From Problem Analysis to Program Design22

Visual Basic.NET Programming: From Problem Analysis to Program Design23 Example 10-3: Customer Class Definition with Accessor Methods (excerpt) 7. 'get accessor methods 8. Public Function GetName() As String 9. Return name 10. End Function 'set accessor methods 21. Public Sub SetName(ByVal aName As String) 22. name = aName 23. End Sub

Visual Basic.NET Programming: From Problem Analysis to Program Design24 Writing a Parameterized Constructor Constructor –Special method –Automatically invoked whenever you create an instance of class using keyword New –Always named New –Written as Sub procedure Cannot return value VB.NET creates default constructor –Invoked when you instantiate class

Visual Basic.NET Programming: From Problem Analysis to Program Design25 Writing a Parameterized Constructor (continued) Parameterized constructor –Can contain parameter list –Arguments populate instance attributes

Visual Basic.NET Programming: From Problem Analysis to Program Design26 Example 10-5: Customer Class Definition with Parameterized Constructor (excerpt) 5. ' parameterized constructor 6. Public Sub New(ByVal aName As String, ByVal anAddress As String, ByVal aPhoneNo As String) 7. SetName(aName) 8. SetAddress(anAddress) 9. SetPhoneNo(aPhoneNo) 10. End Sub

Visual Basic.NET Programming: From Problem Analysis to Program Design27 Example 10-6: Client invoking Parameterized Constructor in Customer (excerpt) 5. Dim aCustomer As Customer 6. ' instantiate Customer 7. aCustomer = New Customer(“Eleanor”, “Atlanta”, “ ”)

Visual Basic.NET Programming: From Problem Analysis to Program Design28 Example 10-7: Creating Multiple Customer Instances (excerpt) 5. Dim customer1, customer2, customer3 As Customer 6. ' create 3 instances of Customer 7. customer1 = New Customer(“Eleanor”, “Atlanta”, “ ”) 8. customer2 = New Customer(“Emily”, “St. Louis”, “ ”)

Visual Basic.NET Programming: From Problem Analysis to Program Design29

Visual Basic.NET Programming: From Problem Analysis to Program Design30 Designing and Writing Overloaded Constructors Procedure signature: –Name –Parameter list –Used to identify procedure Can overload constructor –Must use different parameter list –Can use Me keyword to call another constructor

Visual Basic.NET Programming: From Problem Analysis to Program Design31 Creating Polymorphic Methods Polymorphism –Means “many forms” –Different methods in different classes with same signature do different things

Visual Basic.NET Programming: From Problem Analysis to Program Design32

Visual Basic.NET Programming: From Problem Analysis to Program Design33 Writing Properties Property –Similar to accessor methods –Appears as public attribute to client objects –Retrieve contents of property as if it were a variable –Begins with header –Ends with End Property

Visual Basic.NET Programming: From Problem Analysis to Program Design34 Example 10-16: Customer Class Definition with Properties (excerpt) 11. ' property named CustomerName 12. Public Property CustomerName() As String 13. Get 14. Return name 15. End Get

Visual Basic.NET Programming: From Problem Analysis to Program Design35 Example 10-16: Customer Class Definition with Properties (continued) 16. Set(ByVal aName As String) 17. name = aName 18. End Set 19. End Property

Visual Basic.NET Programming: From Problem Analysis to Program Design36 Creating Shared Attributes and Methods Attributes and methods can be shared by all instances of class Called class methods and attributes Non-shared methods and attributes: –Called instance methods and attributes

Visual Basic.NET Programming: From Problem Analysis to Program Design37 Example 10-18: Class Definition for Circle with Shared Attribute and Method (excerpt) 4. ' shared attribute 5. Private Shared PI As Double = ' shared method 22. Public Shared Function GetPI() As Double 23. Return PI 24. End Function

Visual Basic.NET Programming: From Problem Analysis to Program Design38 Programming Example: Electricity Billing Redesign of Electricity Billing Programming Example at end of Chapter 9 –Includes class named ElectricBill to represent electricity bills

Visual Basic.NET Programming: From Problem Analysis to Program Design39 Programming Example: Electricity Billing (continued) Invokes Client methods to: –Compute: Electricity charge Sales tax –Display: Electricity consumed Electricity charge Sales tax Total bill

Visual Basic.NET Programming: From Problem Analysis to Program Design40

Visual Basic.NET Programming: From Problem Analysis to Program Design41 Summary Three-tier design places objects into three categories of classes: –Problem domain –Graphical user interface (GUI) –Data access Draw class diagram to model classes Create instances of class using keyword New

Visual Basic.NET Programming: From Problem Analysis to Program Design42 Summary (continued) Accessor methods –Methods that provide access to attributes –Get –Set Constructor –Invoked whenever you create an instance of class using keyword New

Visual Basic.NET Programming: From Problem Analysis to Program Design43 Summary (continued) Polymorphism –Different methods in different classes with same signature that do different things Property –Similar to accessor method –Appears as a public attribute Shared attributes and methods –Shared among all class instances