C++ Programming
Made Simple
Second Edition
Conor Sexton
Made Simple
An imprint of Elsevier Science
Linacre House, Jordan Hill, Oxford OX2 8DP
225 Wildwood Avenue, Woburn MA 01801-2041
First published 2003
Copyright Conor Sexton 2003. All rights reserved
The right of Conor Sexton to be identified as the author of this work has been asserted in accordance with the Copyright, Designs and Patents Act 1988
No part of this publication may be reproduced in any material form (including photocopying or storing in any medium by electronic means and whether or not transiently or incidentally to some other use of this publication) without the written permission of the copyright holder except in accordance with the provisions of the Copyright, Designs and Patents Act 1988 or under the terms of a licence issued by the Copyright Licensing Agency Ltd, 90 Tottenham Court Road, London, England W1T 4LP. Applications for the copyright holders written permission to reproduce any part of this publication should be addressed to the publishers.
TRADEMARKS/REGISTERED TRADEMARKS
Computer hardware and software brand names mentioned in this book are protected by their respective trademarks and are acknowledged.
British Library Cataloguing in Publication Data
A catalogue record for this book is available from the British Library
ISBN 0 7506 5738 3
ISBN 978-0-750-65738-9 (hbk)
ISBN 978-1-136-42299-7 (ebk)
Typeset by Elle and P.K. McBride, Southampton
Icons designed by Sarah Ward 1994
Printed and bound in Great Britain
Preface
C++ Programming Made Simple Second Edition is intended as an introduction to programming in the C++ language as codified by the 1998 ISO C++ Standard. It is not a reference book and does not pretend to be in any way comprehensive. The intention is to provide an accessible starting-point to people who:
- have no programming experience
- have programmed in some other high-level language
- know C or an earlier version of C++ and need an update
- in any case need a working, practical, knowledge of C++.
This book owes a good deal to its two 1997 predecessors C Programming Made Simple and C++ Programming Made Simple. At the time I wrote these, it was still customary to present C++ as an extension of C. Because C++ was then relatively new, many people were in a position of knowing C and needing an upgrade to C++. This situation has now changed. The distinction between C and C++ has blurred if not disappeared: in the ISO Standard, C++ incorporates C. It can no longer be assumed that people will have a knowledge of C before approaching C++. What is called for and, I hope, provided by this book is an integrated coverage of C++ including the parts of C that are not obsolete.
Achieving this has involved much more than simply merging the two previous texts. First, C++ had priority: where a C idiom is replaced by a newer C++ construct, the former is no longer covered. Second, a great deal has changed in C++ in the six years since the original two Made Simples. The language has been tweaked in a thousand details. The C++ Library and the Standard Template Library in particular are largely new. A complete revision was necessary; I hope that it is evident that this is a new book and not just a rehash of two old ones.
This book does not try to take a rigorous approach to C++. Coverage of the language aims to be adequate for practical needs, not complete. Many of the dark corners of C++ (and some not-so-dark ones) are not covered. Even with this selectiveness, the book still comes out at over 300 pages. I estimate that a completely comprehensive coverage of modern C++, including the Standard Library, would weigh in at more than 1,500 pages. The objective, then, is to get you up and running with useful C++ grounded in clear, practical, program examples.
The book has 14 chapters, which I don't list here: you can see them in the Table of Contents. I think of it as having four main parts:
- In time-honoured fashion, a lightning overview of C++ essentials (Chapter 1)
- The C-heavy part of the book, with C++ syntax integrated as appropriate (Chapters 2 to 7)
- Traditional C++, involving classes and inheritance (Chapters 8 to 10)
- Modern C++, including templates and the Standard Library (Chapters 11 to 14)
At those points where a topic could grow beyond the scope of a Made Simple book, I acknowledge the fact, and make suggestions for further reading.
I have enjoyed the various aspects of writing this book: using some material from the previous Made Simples; eliminating obsolete aspects of C; updating the C++ syntax and adding completely new sections. It may be optimistic of me to ask you to enjoy reading it; I hope at least that you find it useful.
Conor Sexton
Dublin
Contents
Background to ISO C++ language
The C++ programming language is an object-oriented (OO) derivative of C. It is almost true to say that C is a subset of C++. In fact, every ISO C program written in the modern idiom (specifically, with new-style function headers) and avoiding certain C++ reserved words is also a C++ program, although it is not object-oriented.
This book owes a good deal to two of my previous publications in the Made Simple series, C Programming Made Simple (0-7506-3244-5) and C++ Programming Made Simple (0-7506-3243-7) both published by Butterworth-Heinemann in 1997. Since that time, and, particularly, since the September 1998 ratification of the ISO C++ Standard, C has been completely subsumed by C++. C as a language no longer exists in its own right; it is therefore no longer acceptable to take what was once the conventional approach and treat C and C++ separately. Accordingly, this book presents a fully-integrated treatment of the ISO C++ language incorporating C. Parts of the original C language and library are still valid but have been replaced by superior C++ facilities. Examples of such C constructs include void parameter lists and library functions including printf, malloc and others. This book does not deal with the (obsolescent) C mechanisms, but concentrates on the facilities provided by C++. Finally, from now on in this book, the simple term C++ should be taken to mean ISO C++.
It is the aim of this first chapter to get you up and running quickly with C++. The remaining chapters go into somewhat more depth on a variety of C++ constructs and programming techniques.
C++ is for technical computer programming and is suitable for development of techie software like operating systems, graphical interfaces, communications drivers and database managers. In the modern Web context, C++ is one of the three or four languages of choice for implementation of applications in the so-called