Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Macmillan Computer Science Series

Consulting Editor: Professor F.H. Sumner, University of Manchester

A. Abdellatif, J. Le Bihan, M. Limame, Oracle- A User's Guide


Ian 0. Angell, High-resolution Computer Graphics Using C
Ian 0. Angell and Gareth Griffith, High-resolution Computer Graphics Using FORTRAN 77
Ian 0. Angell and Gareth Griffith, High-resolution Computer Graphics Using Pascal
M. Azmoodeh, Abstract Data Types and Algorithms, second edition
C. Bamford and P. Curran, Data Structures, Files and Databases, second edition
Philip Barker, Author Languages for CAL
P. Beynon-Davies, Information Systems Development, second edition
G. M. Birtwistle, Discrete Event Modelling on Simula
Richard Bornat, Understanding and Writing Compilers
Linda E. M. Brackenbury, Design of VLSI Systems- A Practical Introduction
Alan Bradley, Peripherals for Computer Systems
G. R. Brookes and A. J. Stewart, Introduction to occam2 on the Transputer
P.C. Capon and P.J. Jinks, Compiler Engineering Using Pascal
Robert Cole, Computer Communications, second edition
Eric Davalo and Patrick Nairn, Neural Networks
S. M. Deen, Principles and Practice of Database Systems
C. Delannoy, Turbo Pascal Programming
Tim Denvir, Introduction to Discrete Mathematics for Software Engineering
D. England et al., A Sun User's Guide, second edition
J. S. Florentin, Microprogrammed Systems Design
A. B. Fontaine and F. Barrand, 80286 and 90386 Microprocessors
Michel Gauthier, Ada - A Professional Course
M.G. Hartley, M. Healey and P.G. Depledge, Mini and Microcomputer Systems
J. A. Hewitt and R. J. Frank, Software Engineering in Modula-2- An Object-oriented Approach
Roger Hutty, COBOL 85 Programming
Patrick Jaulent, The 68000 - Hardware and Software
M.J. King and J.P. Pardoe, Program Design Using JSP- A Practical Introduction, second
edition
Bernard Leguy, Ada - A Programmer's Introduction
M. Leonard, Database Design Theory
David Lightfoot, Formal Specification Using Z
A.M. Lister and R. D. Eager, Fundamentals of Operating Systems, fifth edition
Elizabeth Lynch, Understanding SQL
Tom Manns and Michael Coleman, Software Quality Assurance, second edition
B. A. E. Meekings, T. P. Kudrycki and M.D. Soren, A book on C. third edition
R. J. Mitchell, C++ Object-Oriented Programming
R.J. Mitchell, Microcomputer Systems Using the STE Bus
R. J. Mitchell, Modula-2 Applied
Y. Nishinuma and R. Espesser, UNIX- First Contact
Pham Thu Quang and C. Chartier-Kastler, MERISE in Practice
A.J. Pilavakis, UNIX Workshop
E. J. Redfern, Introduction to Pascal for Computational Mathematics
Gordon Reece, Microcomputer Modelling by Finite Differences
F. D. Rolland, Programming with VDM
A. G. Sutcliffe, Human-Computer Interface Design
C. J. Theaker and G. R. Brookes, Concepts of Operating Systems
M. Thorin, Real-time Transaction Processing
M. R. Tolhurst et al., Open Systems Interconnection
A.J. Tyrell, COBOLfrom Pascal
I. R. Wilson and A.M. Addyman, A Practical Introduction to Pascal, second edition

continued overleaf
Other titles
Roy Anderson, Management, Information Systems tl1lll Computers
Ian 0. Angell and Dimitrios Tsoubelis, Advanced Graphics on VGA tl1lll XGA Cards Using
Borltlllll C++
B. V. Cordingley and D. Chamund, Advanced BASIC Scientific Subroutines
N. Frude, A Guide to SPSS/PC+, second edition
Percy Mett, Introduction to Computing
Tony Royce, COBOL- An Introduction
Tony Roryce, Structured COBOL- An Introduction
Barry Thomas, A PostScript Cookbook
C++ Object-Oriented
Programming

R. J. Mitchell
Department of Cybernetics
University of Reading

M
!50th YEAR

MACMILLAN
© R. J. Mitchell 1993
All rights reserved. No reproduction, copy or transmission of
this publication may be made without written permission.
No paragraph of this publication may be reproduced, copied or
transmitted save with written permission or in accordance with
the provision of the Copyright, Designs and Patents Act 1988,
or under the terms of any licence permitting limited copying
issued by the Copyright Licensing Agency, 90 Tottenham Court
Road, London W1P 9HE.
Any person who does any unauthorised act in relation to this
publication may be liable to criminal prosecution and civil
claims for damages.

First published 1993 by


THE MACMILLAN PRESS LTD
Houndmills, Basingstoke, Hampshire RG21 2XS
and London
Companies and representatives
throughout the world

ISBN 978-0-333-58937-3 ISBN 978-1-349-13173-0 (eBook)


DOI 10.1007/978-1-349-13173-0

A 3.5 in. DOS diskette containing the program source and answers
to the exercises is available at the price of £25.00 or $40.00 (inc.
postage/packing - airmail overseas).
Please send your personal cheque to: Globe Book Services Ltd,
Houndmills, Basingstoke, Hampshire, RD21 2XS, UK.
Access/Visa/American Express/Diners Club accepted. Please quote
number and expiry date.
Please quote ISBN 978-0-333-59862-7 (diskette) when ordering.

A catalogue record for this book is available


from the British Library

Borland C++ is a trademark of Borland International Inc.


Contents
Preface viii

1 Introduction 1
1.1 Software development 2
1.2 Algorithms and data 3
1.3 Conventional approach to programming 3
1.4 Object-oriented approach 5
1.5 Inheritance 8
1.6 Strong· typing and overloading 11
1.7 Libraries 12
1.8 Abstraction 12
1.9 Programming style and documentation 13

2 Object-Oriented Design 16
2.1 Drawing package overview 16
2.2 Object-oriented design 18
2.3 Systems design 26
2.4 The drawing package 29

3 Simple Classes 36
3.1 The WithinWindow class - requirements 36
3.2 Classes 37
3.3 The WithinWindow class - implementation 38
3.4 The Difference class 39
3.5 Bezier curves: a class with multiple constructors 43
3.6 Ellipses 48
3.7 Circles 50
3.8 Transfonnation matrices 53

4 Inheritance • Window Classes 61


4.1 Why inheritance is required 61
4.2 Syntax of inheriting classes 62
4.3 Other aspects of the TWindow class 76
4.4 More aspects of the GWindow class 78
4.5 More about the PSGWindow class 80
4.6 More on the HPGWindow class 83

v
vi Contents

s More Inheritance 8S
5.1 Requirements in the drawing package 85
5.2 The graphical hierarchy in the drawing package 86
5.3 The member functions of the hierarchy 89
5.4 Edit function and virtual functions 92
5.5 Other member functions 94
5.6 Other classes 94

6 Objects and Pointers 96


6.1 The heap and pointers 96
6.2 The operators new and delete 98
6.3 Objects, pointers, new and delete 99
6.4 Pointers and inheritance 100
6.5 Linked lists 101
6.6 Linked list class 103
6.7 Virtual destructors 106
6.8 The Userltem class 107
6.9 Using the list 109
6.10 The Group class 116

7 Multiple Inheritance and Operator Overloading 119


7.1 Input classes 119
7.2 Keyboard handling 121
7.3 Keyboard buffer 122
7.4 Handling the mouse 125
7.5 The BufferKeyMouselnput class 126
7.6 Operator overloading 129

8 Streams 132
8.1 Streams- more details 132
8.2 Streams for files 138
8.3 Streams in the drawing package 142

9 The Help System 152


9.1 Overview of the help system 152
9.2 Tree structures 153
9.3 Generic tree class 157
9.4 Random access to files 164
9.5 The help class and incomplete declarations 166
Contents vii

10 The Selection Facility 171


10.1 Selection in the drawing package 171
10.2 Two-way and sorted linked lists 171
10.3 Friends 174
10.4 The selection module 175

Afterword 190

Appendix 1 An Introduction to C 192


for Ptucal, Modula-2 fiiUl Ada progrwnmers

Al.l C Programs 192


A1.2 Simple data types, the for loop and assignment 193
A1.3 Simple variable types, while loop and if statement 194
A1.4 The do-while and switch statements 196
A1.5 Break, continue and operators 197
A1.6 Sttuctured data types: arrays, sttucts and unions 199
A1.7 Pointers 201
A1.8 Functions, type coercion and local variables 202
A1.9 Variable argument passing 204
Al.lO Libraries 205
A1.11 File handling 206
A1.12 The C preprocessor 208
A1.13 Enumeration types, register and static variables 208

Appendix 2 Algorithms 210


A2.1 Recursion 210
A2.2 Reverse Polish notation and algorithms 213

Appendix 3 mM PC Specific Details 217


A3.1 The mouse class 217
A3.2 Keyboard codes on mM PCs 220
A3.3 Directory lists 221
A3.4 Determining the path to drawing package ftles 222

Appendix 4 Answers to the Exercises 224


A4.1 Sorting class 224
A4.2 Microprocessor tutor program 225
A4.3 GraphPlot program 233
Index 250
Preface
An important aspect of any program is its design: how to approach the
development of the program. Object-oriented design is recognised as being a
powerful technique, and the language C++ has the necessary facilities to allow
object-oriented programming. Therefore the major aim of the book is to
describe object-oriented techniques and show how they can be used in the
development of a reasonably large program which is then implemented in C++.
Programming is a skill which also requires knowledge of both the basic
constructs of the computer language used and techniques employing these
constructs, both of which are needed when designing objects. How these are
used in any given application is determined intuitively, and this intuition is
based on experience of programs already written. Thus another aim of this
book is to describe such techniques and give practical examples of the
techniques in action: to provide some experience.
These aims are accomplished by describing the object-oriented design
methodology in general and the C++ language in particular, and illustrating
these by showing how they are employed in one large program, an interactive
drawing package. The book is about object-oriented programming, not the
drawing package itself; the package, in the words of W.S.Gilbert, provides
corroborative detail intended to give verisimilitude, though not, I hope, to an
otherwise bald and unconvincing narrative!
This approach has a number of advantages. First, it shows the techniques
in real applications, rather than being abstract: this includes the use of objects
in a variety of situations. Second, by the use of suitable exercises, different
applications where the techniques may be useful are described, so greater
experience is provided. Also, the associated program is itself useful. Therefore,
a disk is available providing the program with fully commented source listings,
together with answers to some of the exercises.
The program is written in Borland C++ and runs on IBM PCs or
compatibles. It should be noted, however, that the disk is not required for a
complete understanding of the book. Also, very little of the program is specific
to mM PCs, so it should be easy to port the program to a different platfonn.
An appendix describes some aspects specific to mM PCs.
The book should be suitable for students with a working knowledge of C,
or a language like Pascal, Ada or Modula-2, and therefore be appropriate for
Computer Scientists, Engineers, etc. Indeed the book is used in the Cybernetics
Department at the University of Reading in a course for students already
knowing Modula-2. For those not knowing C, an appendix is provided
describing the basic features of C, so as to allow the reader to understand the
syntax of C++. References to suitable texts are given where appropriate.

viii
Preface ix

In the book, techniques are presented in C++ code or in a pseudocode,


similar to C++, but containing English statements. So that these can be
distinguished, and stand out from the main text, different fonts are used.
This font is used for pseudocode
This font is used for C++

ONrrU!w oft1Je book


This book describes how the object-oriented design technique can be used to
produce a program which is then implemented in C++. This is achieved using
some general design principles, without doing a full formal object-oriented
analysis. Such a technique allows the design of well thought out programs. This
also requires knowledge of useful programming techniques, like dynamic data
structures, so these are described as well. Thus the design of the drawing
package is given, and the techniques for implementing the design are given in
subsequent chapters. Also, at the end of the chapters are exercises relevant to
the design of two example problems. In more detail, the topics covered in the
book are as follows.
First software development techniques are reviewed and the basic ideas of
object-oriented design are introduced. The drawing package and then its design
are considered next; this illustrates various techniques which are described in
detail in the rest of the book. First simple classes are given, and then classes
employing inheritance are described. Next pointers, the heap and objects and
linked-lists are discussed. An example of multiple inheritance is then given.
Streams, the means whereby data are input and output, are described next. The
help facility in the package is given next, in which generic data structures and
trees are described. The final chapter describes the selection facility in which
friends and two-way linked lists are given. The first appendix introduces C for
programmers knowing a language like Pascal. The other appendices describe
useful algorithms, some mM PC specific details and finally answers to some
of the exercises.
In conclusion, the book describes object-oriented design, C++ and various
useful programming techniques in action. Although shown in one application,
programmers should fmd these useful in many varied applications.

Acknowledgements
The author wishes to thank the various people who have helped in the
production of the book (known as 'daughter of book' - it is the sister of
Modula-2 Applied which is called 'son of book'): to colleagues in the
Cybernetics Department, to Chris Williams, to friends and family, and to
Malcolm Stewart and his reviewers for guiding the book through its
evolutionary process.
This book is dedicated to two objects in a class of their own, Anne-Marie
and Jacqueline.
Reading 1993

You might also like