ebook img

C++ How to Program: Introducing the New C++14 Standard PDF

1075 Pages·2016·9.069 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview C++ How to Program: Introducing the New C++14 Standard

This page intentionally left blank Paul Deitel Deitel & Associates, Inc. Harvey Deitel Deitel & Associates, Inc. Vice President, Editorial Director: Marcia Horton Acquisitions Editor: Tracy Johnson Editorial Assistant: Kristy Alaura Acquisitions Editor, Global Editions: Sourabh Maheshwari VP of Marketing: Christy Lesko Director of Field Marketing: Tim Galligan Product Marketing Manager: Bram Van Kempen Field Marketing Manager: Demetrius Hall Marketing Assistant: Jon Bryant Director of Product Management: Erin Gregg Team Lead, Program and Project Management: Scott Disanno Program Manager: Carole Snyder Project Manager: Robert Engelhardt Project Editor, Global Editions: K.K. Neelakantan Senior Manufacturing Controller, Global Editions: Trudy Kimber Senior Specialist, Program Planning and Support: Maura Zaldivar-Garcia Media Production Manager, Global Editions: Vikram Kumar Cover Art: Finevector / Shutterstock Cover Design: Lumina Datamatics R&P Manager: Rachel Youdelman R&P Project Manager: Timothy Nicholls Inventory Manager: Meredith Maresca Credits and acknowledgments borrowed from other sources and reproduced, with permission, in this textbook appear on page 6. Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world Visit us on the World Wide Web at: www.pearsonglobaleditions.com © Pearson Education Limited 2017 The rights of Paul Deitel and Harvey Deitel to be identified as the authors of this work have been asserted by them in accordance with the Copyright, Designs and Patents Act 1988. Authorized adaptation from the United States edition, entitled C++ How to Program,10th Edition, ISBN 9780134448237, by Paul Deitel and Harvey Deitel published by Pearson Education © 2017. All rights 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 either the prior written permission of the publisher or a license permitting restricted copying in the United Kingdom issued by the Copyright Licensing Agency Ltd, Saffron House, 6–10 Kirby Street, London EC1N 8TS. All trademarks used herein are the property of their respective owners. The use of any trademark in this text does not vest in the author or publisher any trademark ownership rights in such trademarks, nor does the use of such trademarks imply any affiliation with or endorsement of this book by such owners. British Library Cataloguing-in-Publication Data A catalogue record for this book is available from the British Library 10 9 8 7 6 5 4 3 2 1 ISBN 10: 1-292-15334-2 ISBN 13: 978-1-292-15334-6 Typeset by GEX Publishing Services Printed and bound in Malaysia In memory of Marvin Minsky, a founding father of the field of artificial intelligence. It was a privilege to be your student in two graduate courses at M.I.T. Every lecture you gave inspired your students to think beyond limits. Harvey Deitel Trademarks DEITEL and the double-thumbs-up bug are registered trademarks of Deitel and Associates, Inc. Carnegie Mellon Software Engineering Institute™ is a trademark of Carnegie Mellon University. CERT® is registered in the U.S. Patent and Trademark Office by Carnegie Mellon University. UNIX is a registered trademark of The Open Group. Microsoft and/or its respective suppliers make no representations about the suitability of the information contained in the documents and related graphics published as part of the services for any purpose. All such documents and related graphics are provided “as is” without warranty of any kind. Microsoft and/ or its respective suppliers hereby disclaim all warranties and conditions with regard to this information, including all warranties and conditions of merchantability, whether express, implied or statutory, fitness for a particular purpose, title and non-infringement. In no event shall Microsoft and/or its respective sup- pliers be liable for any special, indirect or consequential damages or any damages whatsoever resulting from loss of use, data or profits, whether in an action of contract, negligence or other tortious action, arising out of or in connection with the use or performance of information available from the services. The documents and related graphics contained herein could include technical inaccuracies or typograph- ical errors. Changes are periodically added to the information herein. Microsoft and/or its respective sup- pliers may make improvements and/or changes in the product(s) and/or the program(s) described herein at any time. Partial screen shots may be viewed in full within the software version specified. Microsoft® and Windows® are registered trademarks of the Microsoft Corporation in the U.S.A. and other countries. Screen shots and icons reprinted with permission from the Microsoft Corporation. This book is not sponsored or endorsed by or affiliated with the Microsoft Corporation. Throughout this book, trademarks are used. Rather than put a trademark symbol in every occurrence of a trademarked name, we state that we are using the names in an editorial fashion only and to the benefit of the trademark owner, with no intention of infringement of the trademark. Contents Chapters 23–26 and Appendices F–J are PDF documents posted online at the book’s Companion Website, which is accessible from http://www.pearsonglobaleditions.com/deitel See the inside front cover for more information. Preface 23 Before You Begin 39 1 Introduction to Computers and C++ 41 1.1 Introduction 42 1.2 Computers and the Internet in Industry and Research 43 1.3 Hardware and Software 45 1.3.1 Moore’s Law 45 1.3.2 Computer Organization 46 1.4 Data Hierarchy 47 1.5 Machine Languages, Assembly Languages and High-Level Languages 50 1.6 C and C++ 51 1.7 Programming Languages 52 1.8 Introduction to Object Technology 54 1.9 Typical C++ Development Environment 57 1.10 Test-Driving a C++ Application 60 1.10.1 Compiling and Running an Application in Visual Studio 2015 for Windows 60 1.10.2 Compiling and Running Using GNU C++ on Linux 65 1.10.3 Compiling and Running with Xcode on Mac OS X 67 1.11 Operating Systems 72 1.11.1 Windows—A Proprietary Operating System 72 1.11.2 Linux—An Open-Source Operating System 72 1.11.3 Apple’s OS X; Apple’s iOS for iPhone®, iPad® and iPod Touch® Devices 73 1.11.4 Google’s Android 73 1.12 The Internet and the World Wide Web 74 1.13 Some Key Software Development Terminology 76 1.14 C++11 and C++14: The Latest C++ Versions 78 8 Contents 1.15 Boost C++ Libraries 79 1.16 Keeping Up to Date with Information Technologies 79 2 Introduction to C++ Programming, Input/Output and Operators 84 2.1 Introduction 85 2.2 First Program in C++: Printing a Line of Text 85 2.3 Modifying Our First C++ Program 89 2.4 Another C++ Program: Adding Integers 90 2.5 Memory Concepts 94 2.6 Arithmetic 95 2.7 Decision Making: Equality and Relational Operators 99 2.8 Wrap-Up 103 3 Introduction to Classes, Objects, Member Functions and Strings 113 3.1 Introduction 114 3.2 Test-Driving an Account Object 115 3.2.1 Instantiating an Object 115 3.2.2 Headers and Source-Code Files 116 3.2.3 Calling Class Account’s getName Member Function 116 3.2.4 Inputting a string with getline 117 3.2.5 Calling Class Account’s setName Member Function 117 3.3 Account Class with a Data Member and Set and Get Member Functions 118 3.3.1 Account Class Definition 118 3.3.2 Keyword class and the Class Body 119 3.3.3 Data Member name of Type string 119 3.3.4 setName Member Function 120 3.3.5 getName Member Function 122 3.3.6 Access Specifiers private and public 122 3.3.7 Account UML Class Diagram 123 3.4 Account Class: Initializing Objects with Constructors 124 3.4.1 Defining an Account Constructor for Custom Object Initialization 125 3.4.2 Initializing Account Objects When They’re Created 126 3.4.3 Account UML Class Diagram with a Constructor 128 3.5 Software Engineering with Set and Get Member Functions 128 3.6 Account Class with a Balance; Data Validation 129 3.6.1 Data Member balance 129 3.6.2 Two-Parameter Constructor with Validation 131 3.6.3 deposit Member Function with Validation 131 3.6.4 getBalance Member Function 131 3.6.5 Manipulating Account Objects with Balances 132 3.6.6 Account UML Class Diagram with a Balance and Member Functions deposit and getBalance 134 3.7 Wrap-Up 134 Contents 9 4 Algorithm Development and Control Statements: Part 1 143 4.1 Introduction 144 4.2 Algorithms 145 4.3 Pseudocode 145 4.4 Control Structures 146 4.4.1 Sequence Structure 146 4.4.2 Selection Statements 148 4.4.3 Iteration Statements 148 4.4.4 Summary of Control Statements 149 4.5 if Single-Selection Statement 149 4.6 if…else Double-Selection Statement 150 4.6.1 Nested if…else Statements 151 4.6.2 Dangling-else Problem 153 4.6.3 Blocks 153 4.6.4 Conditional Operator (?:) 154 4.7 Student Class: Nested if…else Statements 155 4.8 while Iteration Statement 157 4.9 Formulating Algorithms: Counter-Controlled Iteration 159 4.9.1 Pseudocode Algorithm with Counter-Controlled Iteration 159 4.9.2 Implementing Counter-Controlled Iteration 160 4.9.3 Notes on Integer Division and Truncation 162 4.9.4 Arithmetic Overflow 162 4.9.5 Input Validation 163 4.10 Formulating Algorithms: Sentinel-Controlled Iteration 163 4.10.1 Top-Down, Stepwise Refinement: The Top and First Refinement 164 4.10.2 Proceeding to the Second Refinement 164 4.10.3 Implementing Sentinel-Controlled Iteration 166 4.10.4 Converting Between Fundamental Types Explicitly and Implicitly 169 4.10.5 Formatting Floating-Point Numbers 170 4.10.6 Unsigned Integers and User Input 170 4.11 Formulating Algorithms: Nested Control Statements 171 4.11.1 Problem Statement 171 4.11.2 Top-Down, Stepwise Refinement: Pseudocode Representation of the Top 172 4.11.3 Top-Down, Stepwise Refinement: First Refinement 172 4.11.4 Top-Down, Stepwise Refinement: Second Refinement 172 4.11.5 Complete Second Refinement of the Pseudocode 173 4.11.6 Program That Implements the Pseudocode Algorithm 174 4.11.7 Preventing Narrowing Conversions with List Initialization 175 4.12 Compound Assignment Operators 176 4.13 Increment and Decrement Operators 177 4.14 Fundamental Types Are Not Portable 180 4.15 Wrap-Up 180

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.