OOP in VB. OOP Principles An object – in memory – has –Some data values members –Pieces of executable code methods Objects usually come and go during.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 14.
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
CS 211 Inheritance AAA.
12-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN AK - IT:
More about classes and objects Classes in Visual Basic.NET.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Creating Object Oriented Programs Object oriented (OO) terminology Class vs. object Instantiate an object in a project Understand Class_Initialize / Terminate.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
ASP.NET Programming with C# and SQL Server First Edition
VB Classes ISYS 512. Adding a Class to a Project Project/Add Class –*** MyClass is a VB keyword. Steps: –Adding properties Declare Public variables in.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Object-Oriented Programming in Visual Basic.NET. Overview Defining Classes Creating and Destroying Objects Inheritance Interfaces Working with Classes.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
OOP Languages: Java vs C++
To define a class in Visual Basic.NET, you can follow this general procedure: 1. Add a class to the project. 2. Provide an appropriate file name for.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Programming Languages and Paradigms Object-Oriented Programming.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
Module 7: Object-Oriented Programming in Visual Basic .NET
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
JavaScript, Fourth Edition
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
Object Oriented Programming Concepts. Object Oriented Programming Type of programming whereby the programmer defines the data types of a data structure.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Programming in Java CSCI-2220 Object Oriented Programming.
Chapter 6 OOP: Creating Object-Oriented Programs Programming In Visual Basic.NET.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
MS Visual Basic 6 Walter Milner. VB 6 0 Introduction –background to VB, A hello World program 1 Core language 1 –Projects, data types, variables, forms,
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
VB Classes ISYS 512/812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 5 Introduction to Defining Classes
Object-Oriented Programming Chapter Chapter
JAVA INTRODUCTION. What is Java? 1. Java is a Pure Object – Oriented language 2. Java is developing by existing languages like C and C++. How Java Differs.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Object-Oriented Programming: Inheritance and Polymorphism.
OOP Basics Classes & Methods (c) IDMS/SQL News
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
CSC 243 – Java Programming, Fall, 2008 Tuesday, September 30, end of week 5, Interfaces, Derived Classes, and Abstract Classes.
Object-Oriented Programming: Classes and Objects.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Introduction to Object-oriented Programming
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
7. Inheritance and Polymorphism
Inheritance and Polymorphism
Object Based Programming
Inheritance Basics Programming with Inheritance
Java Programming Language
Computer Programming with JAVA
Object-Oriented Programming: Classes and Objects
Lecture 10 Concepts of Programming Languages
Presentation transcript:

OOP in VB

OOP Principles An object – in memory – has –Some data values members –Pieces of executable code methods Objects usually come and go during execution – dynamic not static Objects belong to a class – the type of the object An object is an instantiation of the class

OOP Principles- Encapsulation Contrast with structured programming, where global data is processed by functions Encapsulation = enclosing the data in an object Other code (in other classes) cannot change the data in an object No global data in pure OOP High level of modularity Data members should be private Accessor methods of the class control access Changes are (should be) validated

Typical classes (objects are nouns) Application-specific classes – –employee, invoice, contract, purchase order –dust particle in graphics for simulation System classes –UI – window, form, button, slider –System – thread, file, socket

Inheritance A class can be designed as a variation on another class (the base or ancestor class) The subclass inherits all the members and methods of the base class Supports code re-use – do not have to start from scratch

Polymorphism Sub-classes can have new or different data members of methods So can have variations on a theme eg a button is a sub-class of a window

Object lifetime An object is 'made' at run-time Using the keyword new Good OOP languages have constructors which make new objects and destructors to end objects and free up memory

References In VB (and Java) classes are reference types References are (hidden) pointers No (exposed) pointers in VB or Java In VB use Set eg- Dim myObject as myClass ' say what type it is Set myObject = new myClass ' actually make one Dim object2 as myClass ' another reference Set object2 = myObject ' to the same object

Garbage collection GC is when memory manager reclaims memory occupied by objects no longer needed – reused as free memory In C++ can explicitly destroy objects – memory reclaimed then In VB – system counts the number of references – when =0, GC happens Only important for large arrays of large objects

Classes in VB Class defined in a class module (name = name of class) Inside the class module you –declare private data members –methods as public or private sub or functions –Property Get or Let to access private data members

Add Class module to project In Project.. Add class module Change name..

Example – particle object Idea – simulate physical system containing many particles Each particle needs data members – position and velocity And methods – initialise, move, and draw An array of these objects represents a bunch of particles

Look at the OOPParticle project Run it Look through the code Try changing the number of particles, the viscous drag and so on. Develop it – –All the particles are green –Add data members for their red green and blue colour components –Initialise them as random –Change the draw method to use the colour

Interfaces In VB, an interface is a single way of 'talking to' different classes Interface written just like a class, but with empty methods different classes can implement methods in different ways gives polymorphism Interface Method A Method B Method C Class 1 Method A (version 1) Method B Method C Class 2 Method A (version 2) Method B Method C Class 3 Method A (version 3) Method B Method C Calling code – Class1.methodA Class2.methodA Class3.methodA

Example interface - personnel Dealing with Employees All have name, rateOfPay properties All need a pay method Managerial employees are monthly paid Clerical employees are hourly paid

The Employee interface Private nameStr As String Private payRate As Double Public Property Let name(val As String) End Property Public Property Get name() As String End Property Public Property Get rateOfPay() As Double End Property Public Property Let rateOfPay(val As Double) End Property Public Sub pay() End Sub

The Manager Class Implements Employee Private nameStr As String Private payRate As Double Public Property Get Employee_name() As String Employee_name = nameStr End Property Public Property Let Employee_name(val As String) nameStr = val End Property Public Property Get Employee_rateOfPay() As Double Employee_rateOfPay = payRate End Property Public Property Let Employee_rateOfPay(val As Double) payRate = val End Property Public Sub Employee_pay() MsgBox (nameStr & " gets paid " & payRate) End Sub

Testing the Manager class Dim emp1 As Manager Set emp1 = New Manager emp1.Employee_name = "John (manager)" emp1.Employee_rateOfPay = 2010 emp1.Employee_pay

The Clerical Class (interface example) Implements Employee Private nameStr As String Private payRate As Double ' extra data member Private hours As Integer 'extra properties Public Property Let hoursWorked(val As Integer) hours = val End Property Public Property Get hoursWorked() As Integer hoursWorked = hours End Property..rest same as Manager class except.. Public Sub Employee_pay() MsgBox (nameStr & " gets paid " & payRate * hours) End Sub

Using the Clerical Class Dim emp2 As Clerical Set emp2 = New Clerical emp2.Employee_name = "Luke (clerical)" emp2.Employee_rateOfPay = 8.5 emp2.hoursWorked = 100 emp2.Employee_pay

Interfaces are not class hierarchies There is no inheritance in VB6 There are no derived classes in VB In the above, Clerical is not a sub-class of Employee Members must be re-coded