This page intentionally left blank Copyright © 2009, New Age International (P) Ltd., Publishers Published by New Age International (P) Ltd., Publishers All rights reserved. No part of this ebook may be reproduced in any form, by photostat, microfilm, xerography, or any other means, or incorporated into any information retrieval system, electronic or mechanical, without the written permission of the publisher. All inquiries should be emailed to [email protected] ISBN (13) : 978-81-224-2710-3 PUBLISHING FOR ONE WORLD NEW AGE INTERNATIONAL (P) LIMITED, PUBLISHERS 4835/24, Ansari Road, Daryaganj, New Delhi - 110002 Visit us at www.newagepublishers.com C++ is an excellent programming language both for procedural as well as object oriented programming and lends support to more diverse applications compared to any other computer programming language. This one reason that C++ now is used in more than 80% of all the softwares that is being produced these days. Naturally, those with sound knowledge of C++ have access to many job-opportunities. However, many students feel that programming with C++ is a difficult task. In this book, the presentation of the subject has been designed keeping in view the difficulties of the students. Almost every topic is followed by illustrative examples, so that the readers may easily grasp the content. In programming, one learns much more quickly through actual live programs than by simply reading volumes about how to program. Every chapter of the book contains illustrative examples in the form of well designed live programs which the reader may use for learning as well as for his/her applications. The programs are designed to bring out the salient features of the underlying principle or coding technique. The book contains more than 450 live programmes. This feature of the book is highly useful for those doing self study of C++ language. The book also contains an extensive coverage of STL and other recent additions to C++ Standard Library. STL contains function templates and class templates which are meant to be reused by professionals in their application programmes. In fact, the reuse of well established software is at the heart of object oriented programming. The book contains a large number of live programs for illustrating the applications of the STL functions and algorithms. Thus the book is highly useful to professionals as a ready reference. The first few chapters of the book deal extensively with the procedural programming. This style of programming is still used extensively for small length programs. Also, a good grounding in procedural programming prepares the students for more difficult topics pertaining to classes, operator overloading, inheritance, object oriented programming (OOP), template functions and template classes as well as container classes such as vector, list, deque, sets, multisets, maps, multimaps, stacks, queues, etc. All these topics are dealt in sufficient detail as well as a large number of illustrative live programs are included. This part is highly useful to students as well as software professionals. These templates are meant to be reused in professional application software. ❖ vi ❖ Programming with C++ Almost all the topics generally used in C++ programming are covered in the book. The contents of the book have already been class tested. From the responses of students, the authors feel confident that the book should be useful for students pursuing science and engineering, BCA, MCA and those appearing for examinations of Department of Electronics Society. Authors are also confident that the book will prove its worth to software professionals as well as to those pursuing self-study. B.L. Juneja Anita Seth Contents ❖ vii❖ The authors would like to record their gratitude to the Director of IIT Delhi and Director of Institute of Engineering and Technology, Indore, for providing general facilities for carrying out the writing of this book. Authors are grateful to the Head of Department of Electronics and Telecommunication, IET Indore, and Prof. J.P. Subrahmanyam, Head of Department of M.E., IIT Delhi for encouragement and inspiration. The authors are particularly grateful to Professor Arun Kanda and Professor S.G. Deshmukh – the previous Head and Associate Head of Mechanical Engineering Department, IIT Delhi, for providing facilities to Dr. B.L. Juneja to teach the subject to the graduate students of Mechanical Engineering Department. This has given him the opportunity to test the material in class environment. We are highly grateful to Prof. P.V.M. Rao of Mechanical Department, IIT Delhi for allowing use of computer laboratory and helping with necessary software. Authors are also grateful to Professor G. Singh of Maryland University, USA for his valuable advice, encouragement and for providing the syllabi of the courses on C++ being taught at American Universities. Authors are grateful to Dr. Nitin Seth of Mechanical Department, IET Indore, presently working as Professor, Indian Institute of Foreign Trade, New Delhi for his valuable suggestions and general help in presentation of material. For accomplishing a work like writing of this book, it requires, besides the efforts of the authors, the advice and help of the persons closely associated with the authors. The authors are indeed grateful to Mr. Rajiv Kapoor, Mr. Bharat Kalra and all the colleagues for their valuable suggestions and encouragement. The last but not the least, authors are indeed grateful to Mrs. Asha Seth and Mrs. Pushpa Juneja for providing the moral support and help so that the work could be completed. B.L. Juneja Anita Seth This page intentionally left blank Preface v Acknowledgements vii Chapter 1: Introduction to C++ 1 1.1 Computer and Computer Languages 1 1.2 A Brief History of C++ 4 1.3 Major Additions from C to C++ 5 1.4 Some Advantages of C++ Over C 6 1.5 Range of Applications of C++ 7 1.6 Compilers for Programming with C++ 7 1.7 The C++ Standard Library 8 1.8 Program Development in C++ 9 1.9 Programming Techniques 10 1.10 Object Oriented Programming 12 1.11 Operator Overloading 15 1.12 Inheritance 16 1.13 Polymorphism 16 1.14 Number Systems 17 1.15 Bits and Bytes 20 1.16 Computer Performance 23 Chapter 2: Structure of a C++ Program 27 2.1 Introduction 27 2.2 Components of a Simple C++ Program 28 2.3 Escape Sequences 30 2.4 Variable Declaration and Memory Allocation 34 2.5 Namespaces 36 2.6 User Interactive Programs 38 2.7 Formatting the Output 39 2.8 Function cin.getline() v/s cin 45