You are on page 1of 319
Object Oriented Programming with Ctt 2nd edition q David Parsons Acknowledgements Many people have had an influence on the second edition of this book, but some deserve special mention. | would particularly like to thank my colleagues Mark Cranshaw, Rob Callan and Ian Court with whom I have taught many courses on object-orientation and C++. Their insight and experience has been invaluable. I am also grateful to Tom Kazmierski for his consistent support for my research. Thanks are also due to the hundreds of students who have endured my lectures and the thousands(!) who bought the first edition of the book. I would also like to thank the many people who provided constructive comments on the first edition, either directly or via the publisher's questionnaire. I have tried to take as many of your comments into account as possible in preparing this edition. Finally, I would like to thank my wife and daughters: Di, Jenny, Katie and Abbie, who (almost) prevent me from going insane. David Parsons Email: dave.parsons@solent.ac.uk home page: hitp: / / www-solent.ac-uk/syseng /sef9027 html ACIP catalogue record for this book is available from the British Library ISBN 0 8264 5428 3 Copyright D. Parsons © 1997 First edition 1994 Second edition 1997 Reprinted 2000, 2002 Allrights reserved No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior permission of the copyright owner. ‘Typeset by Elizabeth Bennett, St Albans, Herts Printed in Great Britain by Martins the Printers, Berwick upon Tweed Continuum The Tower Building, 11 York Road, London, SE1 7NX 370 Lexington Avenue, New York, NY 10017-6530 www.continuumbooks.com Contents Preface iv 1 What is object-orientation? 1 2 What is C++? 13 3 Getting to grips with C++ Part 1 Data types, operators, functions and I/O 20 Part2 Arrays, strings, pointers and control structures 26 4 Modelling the real world 52 5 Classes and objects 65 6 Object lifetimes and dynamic objects 7 Z_The metacla 99 8 Inheritance and classification hierarchies 110 9 Associations and aggregations 133 ‘An example program 161 10 Introduction to polymorphism V7 Il Operator overloading, 186 12 Polymorphism by parameter 203 13. Method polymorphism 215 14 Run-time polymorphism 229 15. Container classes Part1 Container types, data structures and simple containers 244 Part2 Heterogenous containers and template classes 266 16 Multiple inheritance 284 17 Persistent objects, streams and files 310 18 Object-oriented analysis and design 333 Case study: a UML design 342 Appendix: answers to exercises 360 Glossary 382 Bibliography 383 Index 387 iii

You might also like