ebook img

Introduction to Programming with C++ PDF

714 Pages·2014·11.697 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 Introduction to Programming with C++

ONLINE ACCESS Thank you for purchasing a new copy of Introduction to Programming with C++, Third Edition. Your textbook includes six months of prepaid access to the book’s Companion Website. This prepaid subscription provides you with full access to the following student support areas: (cid:2)(cid:3)(cid:3)(cid:4)(cid:5)(cid:6)(cid:7)(cid:8)(cid:9)(cid:8)(cid:10)(cid:7)(cid:11) (cid:2)(cid:3) (cid:12)(cid:7)(cid:13)(cid:3)(cid:14)(cid:15)(cid:16)(cid:17)(cid:10)(cid:7)(cid:18)(cid:11) (cid:2)(cid:3) (cid:19)(cid:10)(cid:20)(cid:6)(cid:7)(cid:21)(cid:10)(cid:3)(cid:11)(cid:20)(cid:17)(cid:17)(cid:22)(cid:7)(cid:23)(cid:7)(cid:21)(cid:10)(cid:11) Use a coin to scratch off the coating and reveal your student access code. Do not use a knife or other sharp object as it may damage the code. To access the Introduction to Programming with C++, Third Edition, Companion Website for the (cid:24)(cid:18)(cid:11)(cid:10)(cid:3)(cid:10)(cid:5)(cid:23)(cid:7)(cid:25)(cid:3)(cid:26)(cid:8)(cid:20)(cid:3)(cid:27)(cid:5)(cid:22)(cid:22)(cid:3)(cid:21)(cid:7)(cid:7)(cid:6)(cid:3)(cid:10)(cid:8)(cid:3)(cid:18)(cid:7)(cid:28)(cid:5)(cid:11)(cid:10)(cid:7)(cid:18)(cid:3)(cid:8)(cid:21)(cid:22)(cid:5)(cid:21)(cid:7)(cid:3)(cid:20)(cid:11)(cid:5)(cid:21)(cid:28)(cid:3)(cid:16)(cid:3)(cid:29)(cid:8)(cid:23)(cid:17)(cid:20)(cid:10)(cid:7)(cid:18)(cid:3)(cid:27)(cid:5)(cid:10)(cid:15)(cid:3)(cid:16)(cid:21)(cid:3)(cid:30)(cid:21)(cid:10)(cid:7)(cid:18)(cid:21)(cid:7)(cid:10)(cid:3)(cid:29)(cid:8)(cid:21)(cid:21)(cid:7)(cid:29)(cid:10)(cid:5)(cid:8)(cid:21)(cid:3)(cid:16)(cid:21)(cid:6)(cid:3)(cid:16)(cid:3)(cid:27)(cid:7)(cid:13)(cid:3) browser. The process takes just a couple of minutes and only needs to be completed once. 1. Go to www.pearsoninternationaleditions.com/liang 2. Click on Companion Website. 3. Click on the Register button. 4. On the registration page, enter your student access code* found beneath the scratch-off panel. Do not type the dashes. You can use lower- or uppercase. 5.(cid:3)(cid:31)(cid:8)(cid:22)(cid:22)(cid:8)(cid:27)(cid:3)(cid:10)(cid:15)(cid:7)(cid:3)(cid:8)(cid:21)!(cid:11)(cid:29)(cid:18)(cid:7)(cid:7)(cid:21)(cid:3)(cid:5)(cid:21)(cid:11)(cid:10)(cid:18)(cid:20)(cid:29)(cid:10)(cid:5)(cid:8)(cid:21)(cid:11)"(cid:3)(cid:30)#(cid:3)(cid:26)(cid:8)(cid:20)(cid:3)(cid:21)(cid:7)(cid:7)(cid:6)(cid:3)(cid:15)(cid:7)(cid:22)(cid:17)(cid:3)(cid:16)(cid:10)(cid:3)(cid:16)(cid:21)(cid:26)(cid:3)(cid:10)(cid:5)(cid:23)(cid:7)(cid:3)(cid:6)(cid:20)(cid:18)(cid:5)(cid:21)(cid:28)(cid:3)(cid:10)(cid:15)(cid:7)(cid:3)(cid:8)(cid:21)(cid:22)(cid:5)(cid:21)(cid:7)(cid:3)(cid:18)(cid:7)(cid:28)(cid:5)(cid:11)(cid:10)(cid:18)(cid:16)(cid:10)(cid:5)(cid:8)(cid:21)(cid:3) process, simply click the Need Help? icon. 6.(cid:3)$(cid:21)(cid:29)(cid:7)(cid:3)(cid:26)(cid:8)(cid:20)(cid:18)(cid:3)(cid:17)(cid:7)(cid:18)(cid:11)(cid:8)(cid:21)(cid:16)(cid:22)(cid:3)%(cid:8)(cid:28)(cid:5)(cid:21)(cid:3)(cid:9)(cid:16)(cid:23)(cid:7)(cid:3)(cid:16)(cid:21)(cid:6)(cid:3)&(cid:16)(cid:11)(cid:11)(cid:27)(cid:8)(cid:18)(cid:6)(cid:3)(cid:16)(cid:18)(cid:7)(cid:3)(cid:29)(cid:8)(cid:21)(cid:24)(cid:18)(cid:23)(cid:7)(cid:6)(cid:25)(cid:3)(cid:26)(cid:8)(cid:20)(cid:3)(cid:29)(cid:16)(cid:21)(cid:3)(cid:13)(cid:7)(cid:28)(cid:5)(cid:21)(cid:3)(cid:20)(cid:11)(cid:5)(cid:21)(cid:28)(cid:3)(cid:10)(cid:15)(cid:7)(cid:3) Introduction to Programming with C++ Companion Website! To log in after you have registered: You only need to register for this Companion Website once. After that, you can log in any time at www.pearsoninternationaleditions.com/liang(cid:3)(cid:13)(cid:26)(cid:3)(cid:17)(cid:18)(cid:8)((cid:5)(cid:6)(cid:5)(cid:21)(cid:28)(cid:3)(cid:26)(cid:8)(cid:20)(cid:18)(cid:3)%(cid:8)(cid:28)(cid:5)(cid:21)(cid:3)(cid:9)(cid:16)(cid:23)(cid:7)(cid:3)(cid:16)(cid:21)(cid:6)(cid:3)&(cid:16)(cid:11)(cid:11)(cid:27)(cid:8)(cid:18)(cid:6)(cid:3)(cid:27)(cid:15)(cid:7)(cid:21)(cid:3) prompted. )(cid:30)(cid:23)(cid:17)(cid:8)(cid:18)(cid:10)(cid:16)(cid:21)(cid:10)+(cid:3)/(cid:15)(cid:7)(cid:3)(cid:16)(cid:29)(cid:29)(cid:7)(cid:11)(cid:11)(cid:3)(cid:29)(cid:8)(cid:6)(cid:7)(cid:3)(cid:29)(cid:16)(cid:21)(cid:3)(cid:8)(cid:21)(cid:22)(cid:26)(cid:3)(cid:13)(cid:7)(cid:3)(cid:20)(cid:11)(cid:7)(cid:6)(cid:3)(cid:8)(cid:21)(cid:29)(cid:7)"(cid:3)/(cid:15)(cid:5)(cid:11)(cid:3)(cid:11)(cid:20)(cid:13)(cid:11)(cid:29)(cid:18)(cid:5)(cid:17)(cid:10)(cid:5)(cid:8)(cid:21)(cid:3)(cid:5)(cid:11)(cid:3)((cid:16)(cid:22)(cid:5)(cid:6)(cid:3)#(cid:8)(cid:18)(cid:3)(cid:11)(cid:5)0(cid:3)(cid:23)(cid:8)(cid:21)(cid:10)(cid:15)(cid:11)(cid:3) (cid:20)(cid:17)(cid:8)(cid:21)(cid:3)(cid:16)(cid:29)(cid:10)(cid:5)((cid:16)(cid:10)(cid:5)(cid:8)(cid:21)(cid:3)(cid:16)(cid:21)(cid:6)(cid:3)(cid:5)(cid:11)(cid:3)(cid:21)(cid:8)(cid:10)(cid:3)(cid:10)(cid:18)(cid:16)(cid:21)(cid:11)#(cid:7)(cid:18)(cid:16)(cid:13)(cid:22)(cid:7)"(cid:3)(cid:30)#(cid:3)(cid:10)(cid:15)(cid:5)(cid:11)(cid:3)(cid:16)(cid:29)(cid:29)(cid:7)(cid:11)(cid:11)(cid:3)(cid:29)(cid:8)(cid:6)(cid:7)(cid:3)(cid:15)(cid:16)(cid:11)(cid:3)(cid:16)(cid:22)(cid:18)(cid:7)(cid:16)(cid:6)(cid:26)(cid:3)(cid:13)(cid:7)(cid:7)(cid:21)(cid:3)(cid:18)(cid:7)((cid:7)(cid:16)(cid:22)(cid:7)(cid:6)(cid:25)(cid:3)(cid:5)(cid:10)(cid:3)(cid:23)(cid:16)(cid:26)(cid:3) no longer be valid. C++ Quick Reference if Statements switch Statements Array/Initializer if (condition) switch (intExpression) int list[10]; { { int list[] = {1, 2, 3, 4}; statements; case value1: } statements; Multidimensional Array/Initializer break; ... int list[10][15]; if (condition) case valuen: int list[2][2] = {{1, 2}, {3, 4}}; { statements; statements; break; } default: Dynamic Memory Creation/Deletion else statements; { } int* p1 = new int; statements; int* p2 = new int[10]; } delete p1; delete [] p2; if (condition1) { statements; Frequently Used functions } else if (condition2) time(0) returns current time { srand(seed) sets a new seed for generating random numbers statements; rand() returns a random integer } pow(a, b) returns ab else Character Functions { statements; isdigit(c) returns true if c is a digit. } isalpha(c) returns true if c is a letter. isalnum(c) returns true if c is a letter or a digit. islower(c) returns true if c is a lowercase letter. isupper(c) returns true if c is an uppercase letter. tolower(c) returns a lowercase for c. toupper(c) returns an uppercase for c. Loop Statements C-String Functions while (condition) strlen returns string length { strcpy copies a string statements; strcat concatenate two strings } strcmp compares two strings atol converts a string to a long value do itoa converts a an integer a string { statements; The string Class Member Functions } while (condition); append appends new contents to the string for (init; condition; insert inserts new contents to the string adjustment) at retrieves character from the string { [] string subscript operator statements; length returns the length of the string. } substr returns a substring from the string Companion Website: www.cs.armstrong.edu/liang/cpp3e ONLINE ACCESS Thank you for purchasing a new copy of Introduction to Programming with C++, Third Edition. Your textbook includes six months of prepaid access to the book’s Companion Website. This prepaid subscription provides you with full access to the following student support areas: (cid:2)(cid:3)(cid:3)(cid:4)(cid:5)(cid:6)(cid:7)(cid:8)(cid:9)(cid:8)(cid:10)(cid:7)(cid:11) (cid:2)(cid:3) (cid:12)(cid:7)(cid:13)(cid:3)(cid:14)(cid:15)(cid:16)(cid:17)(cid:10)(cid:7)(cid:18)(cid:11) (cid:2)(cid:3) (cid:19)(cid:10)(cid:20)(cid:6)(cid:7)(cid:21)(cid:10)(cid:3)(cid:11)(cid:20)(cid:17)(cid:17)(cid:22)(cid:7)(cid:23)(cid:7)(cid:21)(cid:10)(cid:11) Use a coin to scratch off the coating and reveal your student access code. Do not use a knife or other sharp object as it may damage the code. To access the Introduction to Programming with C++, Third Edition(cid:24)(cid:3)(cid:14)(cid:8)(cid:23)(cid:17)(cid:16)(cid:21)(cid:5)(cid:8)(cid:21)(cid:3)(cid:12)(cid:7)(cid:13)(cid:11)(cid:5)(cid:10)(cid:7)(cid:3)(cid:25)(cid:8)(cid:18)(cid:3)(cid:10)(cid:15)(cid:7)(cid:3)(cid:26)(cid:18)(cid:11)(cid:10)(cid:3) time, you will need to register online using a computer with an Internet connection and a web browser. The process takes just a couple of minutes and only needs to be completed once. 1. Go to www.pearsoninternationaleditions.com/liang 2. Click on Companion Website. 3. Click on the Register button. 4. On the registration page, enter your student access code* found beneath the scratch-off panel. Do not type the dashes. You can use lower- or uppercase. 5. Follow the on-screen instructions. If you need help at any time during the online registration process, simply click the Need Help? icon. 6.(cid:3)(cid:27)(cid:21)(cid:28)(cid:7)(cid:3)(cid:29)(cid:8)(cid:20)(cid:18)(cid:3)(cid:17)(cid:7)(cid:18)(cid:11)(cid:8)(cid:21)(cid:16)(cid:22)(cid:3)(cid:30)(cid:8)(cid:31)(cid:5)(cid:21)(cid:3)(cid:9)(cid:16)(cid:23)(cid:7)(cid:3)(cid:16)(cid:21)(cid:6)(cid:3)!(cid:16)(cid:11)(cid:11)"(cid:8)(cid:18)(cid:6)(cid:3)(cid:16)(cid:18)(cid:7)(cid:3)(cid:28)(cid:8)(cid:21)(cid:26)(cid:18)(cid:23)(cid:7)(cid:6)(cid:24)(cid:3)(cid:29)(cid:8)(cid:20)(cid:3)(cid:28)(cid:16)(cid:21)(cid:3)(cid:13)(cid:7)(cid:31)(cid:5)(cid:21)(cid:3)(cid:20)(cid:11)(cid:5)(cid:21)(cid:31)(cid:3)(cid:10)(cid:15)(cid:7)(cid:3) Introduction to Programming with C++ Companion Website! To log in after you have registered: You only need to register for this Companion Website once. After that, you can log in any time at www.pearsoninternationaleditions.com/liang (cid:13)(cid:29)(cid:3)(cid:17)(cid:18)(cid:8)$(cid:5)(cid:6)(cid:5)(cid:21)(cid:31)(cid:3)(cid:29)(cid:8)(cid:20)(cid:18)(cid:3)(cid:30)(cid:8)(cid:31)(cid:5)(cid:21)(cid:3)(cid:9)(cid:16)(cid:23)(cid:7)(cid:3)(cid:16)(cid:21)(cid:6)(cid:3)!(cid:16)(cid:11)(cid:11)"(cid:8)(cid:18)(cid:6)(cid:3)"(cid:15)(cid:7)(cid:21)(cid:3) prompted. *Important: The access code can only be used once. This subscription is valid for six months upon activation and is not transferable. If this access code has already been revealed, it may no longer be valid. C++ Quick Reference if Statements switch Statements Array/Initializer if (condition) switch (intExpression) int list[10]; { { int list[] = {1, 2, 3, 4}; statements; case value1: } statements; Multidimensional Array/Initializer break; ... int list[10][15]; if (condition) case valuen: int list[2][2] = {{1, 2}, {3, 4}}; { statements; statements; break; } default: Dynamic Memory Creation/Deletion else statements; { } int* p1 = new int; statements; int* p2 = new int[10]; } delete p1; delete [] p2; if (condition1) { statements; Frequently Used functions } else if (condition2) time(0) returns current time { srand(seed) sets a new seed for generating random numbers statements; rand() returns a random integer } pow(a, b) returns ab else Character Functions { statements; isdigit(c) returns true if c is a digit. } isalpha(c) returns true if c is a letter. isalnum(c) returns true if c is a letter or a digit. islower(c) returns true if c is a lowercase letter. isupper(c) returns true if c is an uppercase letter. tolower(c) returns a lowercase for c. toupper(c) returns an uppercase for c. Loop Statements C-String Functions while (condition) strlen returns string length { strcpy copies a string statements; strcat concatenate two strings } strcmp compares two strings atol converts a string to a long value do itoa converts a an integer a string { statements; The string Class Member Functions } while (condition); append appends new contents to the string for (init; condition; insert inserts new contents to the string adjustment) at retrieves character from the string { [] string subscript operator statements; length returns the length of the string. } substr returns a substring from the string Companion Website: www.cs.armstrong.edu/liang/cpp3e get with the programming Through the power of practice and immediate personalized feedback, MyProgrammingLab improves your performance. International Edition contributions by Anisha Sharma MyProgrammingLab™ Learn more at www.myprogramminglab.com PEARSON ALWAYS LEARNING Editorial Director: Marcia Horton This page intentionally left blank IntroductIon to ProgrammIng WIth c++ Third Edition International Edition contributions by mohit P. tahiliani nItK Surathkal Y. daniel Liang Armstrong Atlantic State University Prentice Hall Upper Saddle River Boston Columbus San Francisco New York Indianapolis London Toronto Sydney Singapore Tokyo Montreal Dubai Madrid Hong Kong Mexico City Munich Paris Amsterdam Cape Town Editorial Director: Marcia Horton Acquisitions Editor, International Edition: Sandhya Ghoshal Executive Editor: Tracy Johnson Publishing Administrator, International Edition: Hema Mehta Associate Editor: Carole Snyder Project Editor, International Edition: Karthik Subramanian Editorial Assistant: Jenah Blitz-Stoehr Operations Supervisor: Nick Sklitsis Director of Marketing: Christy Lesko Manufacturing Buyer: Lisa McDowell Marketing Manager: Yez Alayan Art Director: Anthony Gemmellaro Marketing Coordinator: Kathryn Ferranti Text and Cover Designer: Anthony Gemmellaro Director of Production: Erin Gregg Manager, Visual Research: Karen Sanatar Managing Editor: Scott Disanno Manager, Rights and Permissions: Michael Joyce Production Project Manager: Kayla Smith-Tarbox Text Permission Coordinator: Brian Wysock Publisher, International Edition: Angshuman Chakraborty Cover Art: Tetra Images/Glow Images Publishing Administrator and Business Analyst, Lead Media Project Manager: Renata Buetera International Edition: Shokhi Shah Khandelwal Full-Service Project Management: Laserwords Associate Print and Media Editor, International Edition: Cover Printer: Lehigh-Phoenix Color/Hagerstown Anuprova Dey Chowdhuri 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.pearsoninternationaleditions.com © Pearson Education Limited 2014 The rights of Y. Daniel Liang to be identified as author of this work have been asserted by him in accordance with the Copyright, Designs and Patents Act 1988. Authorized adaptation from the United States edition, entitled Introduction to Programming With C++, Third Edition, ISBN 978-0-13-325281-1, by Y. Daniel Liang, published by Pearson Education © 2014. 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. 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 suppliers 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 typographical errors. Changes are periodically added to the in- formation herein. Microsoft and/or its respective suppliers 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. This book is not sponsored or en- dorsed by or affiliated with the Microsoft Corporation. ISBN 10: 0-273-79324-1 ISBN 13: 978-0-273-79324-3 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 14 13 12 11 10 Typeset in Times by Laserwords Printed and bound by Courier Kendalville in The United States of America The publisher’s policy is to use paper manufactured from sustainable forests. This book is dedicated to my current and former C++ students. You have inspired and helped me to continue to improve this book. To Samantha, Michael, and Michelle

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.