C++ PROGRAMMING:
FROM PROBLEM ANALYSIS
TO
PROGRAM DESIGN
FOURTH EDITION
D.S. MALIK
Australia Brazil Japan Korea Mexico Singapore Spain United Kingdom United States
C++ Programming: From Problem Analysis
to Program Design, Fourth Edition
by D.S. Malik
Senior Product Manager: Alyssa Pratt
Acquisitions Editor: Amy Jollymore
Content Product Manager: Jill Braiewa
Marketing Manager: Bryant Chrzan
Editorial Assistant: Patrick Frank
Print Buyer: Julio Esperas
Cover Designer: Lisa Kuhn, Curio Press, LLC
Compositor: Integra
Art Director: Marissa Falco
Validation: Green Pen Quality Assurance
ª 2009 Course Technology, Cengage Learning
ALL RIGHTS RESERVED. No part of this work covered by the
copyright herein may be reproduced, transmitted, stored or used
in any form or by any means graphic, electronic, or mechanical,
including but not limited to photocopying, recording, scanning,
digitizing, taping, Web distribution, information networks, or
information storage and retrieval systems, except as permitted
under Section 107 or 108 of the 1976 United States Copyright Act,
without the prior written permission of the publisher.
For product information and technology assistance, contact us at
Cengage Learning Customer & Sales Support, 1-800-354-9706
For permission to use material from this text or product, submit
all requests online at cengage.com/permissions
Further permissions questions can be emailed to
Proofreader: Green Pen Quality Assurance
Indexer: Liz Cunningham
ISBN-13: 978-1-4239-0209-6
ISBN-10: 1-4239-0209-2
Course Technology
25 Thomson Place
Boston, MA 02210
USA
Visualâ C++ .NET and PowerPointâ are registered trademarks
of the Microsoft Corporation; Pentiumâ is a registered
trademark of Intel Corporation; IBM is a registered trademark
of Industrial Business Machines.
Disclaimer
Course Technology reserves the right to revise this publication
and make changes from time to time in its content without
notice.
The programs in this book are for instructional purposes only.
They have been tested with care, but are not guaranteed for any
particular intent beyond educational purposes. The authors and
the publisher do not offer any warranties or representations, nor
do they accept any liabilities with respect to the programs.
Cengage Learning is a leading provider of customized
learning solutions with office locations around the globe,
including Singapore, the United Kingdom, Australia, Mexico,
Brazil, and Japan. Locate your local office at:
international.cengage.com/region
Cengage Learning products are represented in Canada
by Nelson Education, Ltd.
For your lifelong learning solutions, visit
course.cengage.com
Visit our corporate website at cengage.com
Printed in Canada
1 2 3 4 5 6 7 14 13 12 11 10 09 08
TO
My Daughter
Shelly Malik
BRIEF CONTENTS
PREFACE
1. An Overview of Computers and Programming Languages
2. Basic Elements of C++
xxvii
1
29
3. Input/Output
115
4. Control Structures I (Selection)
167
5. Control Structures II (Repetition)
231
6. User-Defined Functions I
307
7. User-Defined Functions II
345
8. User-Defined Simple Data Types, Namespaces,
and the string Type
415
9. Arrays and Strings
469
10. Applications of Arrays (Searching and Sorting)
and the vector Type
545
11. Records (structs)
603
12. Classes and Data Abstraction
641
13. Inheritance and Composition
717
14. Pointers, Classes, Virtual Functions, and Abstract Classes
785
15. Overloading and Templates
853
16. Exception Handling
943
17. Recursion
981
18. Linked Lists
1017
19. Stacks and Queues
1111