ebook img

Concepts of Object-Oriented Programming with Visual Basic PDF

196 Pages·1997·6.346 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Concepts of Object-Oriented Programming with Visual Basic

Concepts of Object Oriented Programming with Visual Basic Springer New York Berlin Heidelberg Barcelona Budapest Hong Kong London Milan Paris Santa Clara Singapore Tokyo Concepts of Object Oriented Programming with Visual Basic Steven Roman With 24 Illustrations " Springer Steven Roman Department of Mathematics California State University Fullerton, CA 92634 USA Library of Congress Cataloging-in-Publication Data Roman, Steven. Concepts of object-oriented programming with Visual Basic / Steven Roman. p. cm. Includes index. 1. Object-oriented programming (Computer science) 2. Microsoft Visual BASIC. I. Title. QA76.64.R65 1997 005.265 -dc20 96-43296 Printed on acid-free paper. © 1997 Steven Roman All rights reserved. This work may not be translated or copied in whole or in part without the written permission of the publisher (Springer-Verlag New York, Inc., 175 Fifth Avenue, New York, NY 10010, USA), except for brief excerpts in connection with reviews or scholarly analysis. Use in connection with any form of information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed is forbidden. The use of general descriptive names, trade names, trademarks, etc., in this publication, even if the former are not especially identified, is not to be taken as a sign that such names, as understood by the Trade Marks and Merchan dise Marks Act, may accordingly be used freely by anyone. Production managed by Victoria Evarretta; manufacturing supervised by Johanna Tschebull. Camera-ready copy prepared from author's Microsoft Word files. 98765432 ISBN-13: 978-0-387-94889-8 e-ISBN-13: 978-1-4612-2280-4 SPIN 10574700 DOl: 10.1007/978-1-4612-2280-4 To Donna Preface This book is about object-oriented programming and how it is implemented in Microsoft Visual Basic. Accordingly, the book has two separate, but inter twined, goals. The first is to describe the general concepts of object orientation and the second is to describe how to do object-oriented programming in Visual Basic. I intend this to be a short, no-nonsense book that can be read through once and then easily referred to at later times. (Long stories about a mythical company and its programming problems are seldom interesting the first time through, let alone the second, third or fourth times!) To read this book, you need only a minimal acquaintance with Visual Basic. In particular, I assume you can construct a simple Visual Basic program and have some rudimentary knowledge of fundamental programming tech niques. This book does not teach Visual Basic - it teaches you how to pro gram Visual Basic in an object-oriented way. The issue here is not how much Visual Basic you already know - it is that you want to learn about object oriented programming techniques. With regard to the first goal of the book, it is my feeling that a discussion of the underlying concepts of object orientation Oust what is an object, a class, encapsulation, abstraction, and so on) is essential in order to take full advan tage of any object-oriented language. Simply put, it will help you understand what you are doing if you know why you are doing it. Let me put this issue in perspective with my own experience. I had been programming in earlier versions of Visual Basic for some time when I plunged into Visual Basic 4.0. As I usually do with a new application, or new version of an old application, I began by reading the documentation, turning to other books only when I felt that I needed some clarification - although this was seldom helpful. After spending a good deal of time poring over the manuals, I concluded that I could now follow the syntax and create an object-oriented program in Visual Basic. However, I was left with some very annoying ques tions, such as • Why bother? • What is the point of using object-oriented techniques? viii Concepts of Object-Oriented Programming with Visual Basic • Yes, but what is an object, anyway? • And what are these strange concepts like abstraction and encapsulation? My only recourse for answering these questions was to dive into some more academic treatises on object orientation (which I enjoy doing). After con sulting several books, I began to see some light. It felt as though a curtain had been raised, and I fmally began to understand the raison d' etre of object ori entation - if not all of its subtleties. I can't resist the temptation to share with you one example of the frustra tions associated with trying to learn the basic issues of object orientation. Here is a quote from an excellent book (really) on the subject: The class Class is a subclass of the class Object; and thus, the object Class points to the object Object as its superclass. On the other hand, the object Object is an instance of the class Class; and thus, Object points back to Class. Class Class is itself a class, and thus an instance of itself. To be entirely fair, this is not as confusing as it looks when taken out of con text. But it does illustrate some of the difficulties in learning about object ori entation. I wrote this book to help the reader get some insight into object oriented programming without having to suffer through such frustrations. I should also hasten to add, lest you think this book might be too academic, that this is definitely a book about Visual Basic. Thus, we will not dwell on abstract concepts in great depth, nor will we spend much time on aspects of object orientation that do not apply to Visual Basic. The second goal of this book probably needs less motivation than the first, since I would guess that most people are more interested in how than in why. In any case, a concrete context helps clarify the abstract notions and provides a starting place for building object-oriented applications. As the title of the book suggests, we will use Visual Basic (version 4.0 or later) for this context. Let me say a few words about my writing style: I will try to use only as many words as necessary in explaining the concepts and as few words as pos sible in dealing with the examples. With regard to the examples, I fmd that I can learn much more quickly and easily by tinkering with and tracing through short program segments than by studying a long, detailed example. The diffi<..ulty in tinkering with a long pro gram is that changing a few lines can affect other, perhaps irrelevant, portions Preface Ix of the code, to the point where the program will no longer run. Instead of wasting time trying to figure out why it will not run, I usually just give up. On the other hand, if, for example, I want to experiment with how to break a circular reference in Visual Basic, I write a short program, of perhaps one or two dozen lines, with which to experiment. This is a great way to learn and also shows off the advantages of an interpreted language. I often save these little code snippets, for future tinkering. Accordingly, this book is filled with short code snippets. I encourage you to key them in and experiment with them yourselves. Many books on object-oriented programming devote a great deal of space to program design issues, which are certainly important, especially for large projects involving lots of programmers. I shall leave this topic for others to write about. The complete source code for the Turing Machine application (in Chapter 3) should be available on the Springer-Verlag Web site, under http://www.springer-ny.com!supplements/sroman Thanks for looking at my book. I hope you enjoy it. Irvine, California Steven Roman Contents Preface ...................................................................................................... vii Introduction ............................................................................................... 1 1 The Basics of Object-Oriented Programming .......................................... 5 Data Types ............................................................................................... 5 Encapsulation ........................................................................................... 7 Abstract Data Types ................................................................................. 9 Classes ................................................................................................... 12 Defining a Class in Visual Basic ...................................................... 12 Defining a Property in Visual Basic. ................................................ 14 Defining a Method in Visual Basic .................................................. 14 Exposing Properties Through the Property LetlS etlGet Procedures ................................................................................... 15 Objects ................................................................................................... 17 Explicit Object Creation .................................................................. 17 Instance Variables and Member Variables ....................................... 18 The As Object Syntax ...................................................................... 20 Implicit Object Creation .................................................................. 21 Referencing Public Variables and Procedures ................................. 22 The Object/Message Model .................................................................... 23 The Me Keyword .................................................................................... 24 Object Properties and Object Hierarchies ............................................... 25 Creating and Destroying Objects ............................................................ 31 Creating Objects .............................................................................. 32 Destroying Objects .......................................................................... 33 Reference Counts ............................................................................. 35 Circular References ......................................................................... 36 The Notorious End Statement. .......................................................... 42 How to Control a Circular Reference-A Linked List Example ...... 43 Fonn Modules ........................................................................................ 48 Properties and Methods in a Form Module ...................................... 49 xii Concepts of Object-Oriented Programming with Visual Basic Collection Classes .................................................................................. 50 Constructing a Collection Class ...................................................... 52 Polymorphism and Overloading ............................................................. 65 Overloading ..................................................................................... 66 Polymorphism .................................................................................. 66 Inheritance ............................................................................................. 68 2 Handling Object Errors ........................................................................... 71 Error Detection and Error Handling ........................................................ 71 Types of Errors ....................................................................................... 72 The Error Object .................................................................................... 73 Dealing with Run-Time Errors ............................................................... 74 Where to Handle a Run-Time Error ................................................. 76 Dealing with Logical Errors ................................................................... 80 Detecting Logical Errors ................................................................. 80 Where to Handle a Logical Error .................................................... 81 Handling the Error in the Calling Procedure ........................................... 86 Errors Occurring in Events ..................................................................... 87 An Error-Handling Example ................................................................... 88 3 Turing Machines - A Simple Object-Oriented Application ................. 97 What Is an Algorithm? ........................................................................... 97 What Is a Turing Machine? .................................................................... 99 Informal Definition of a Turing Machine ......................................... 99 Describing a Turing Machine ........................................................ 102 Coding a Turing Machine ..................................................................... 104 Error Handling .............................................................................. 106 The Symbol Class .......................................................................... 108 The Symbols Collection Class ........................................................ 108 The State Class .............................................................................. 110 The States Collection Class ........................................................... 111 The Transition Class ...................................................................... 113 The Transitions Collection Class ................................................... 115 The Machine Class ........................................................................ 118 The User Interface ......................................................................... 128 The Standard Module .................................................................... 133 A Final Comment. .......................................................................... 134

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.