C++ Programming An Object Oriented Approach, 1e ISE (Behrouz A. Forouzan, Richard F. Gilberg) (Z Library)
Author: Behrouz A. Forouzan, Richard F. Gilberg
教育
"This book compleiments a course designed to teach object-oriented programming using the syntax of the C++ language. It will prepare students for advanced concepts such as data structure and design patterns. Students who have completedA student completing this course will be ready to take on any other object-oriented language course,, a data-structure course, or move into a course about design patterns"--
📄 File Format:
PDF
💾 File Size:
30.0 MB
352
Views
128
Downloads
0.00
Total Donations
📄 Text Preview (First 20 pages)
ℹ️
Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
📄 Page
1
(This page has no text content)
📄 Page
2
C++ Programming: An Object-Oriented Approach Behrouz A. Forouzan Richard F. Gilberg for23380_fm_i-xviii.indd 1 08/11/18 11:51 am
📄 Page
3
C++ PROGRAMMING: AN OBJECT-ORIENTED APPROACH Published by McGraw-Hill Education, 2 Penn Plaza, New York, NY 10121. Copyright © 2020 by McGraw-Hill Education. All rights reserved. Printed in the United States of America. No part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written consent of McGraw-Hill Education, including, but not limited to, in any network or other electronic storage or transmission, or broadcast for distance learning. Some ancillaries, including electronic and print components, may not be available to customers outside the United States. This book is printed on acid-free paper. 1 2 3 4 5 6 7 8 9 LWI 21 20 19 ISBN 978-0-07-352338-5 (bound edition) MHID 0-07-352338-0 (bound edition) ISBN 978-1-259-57145-9 (loose-leaf edition) MHID 1-259-57145-9 (loose-leaf edition) Executive Portfolio Manager: Suzy Bainbridge Product Developer: Heather Ervolino Marketing Manager: Shannon O’Donnell Content Project Managers: Jane Mohr and Sandra Schnee Buyer: Laura Fuller Designer: Egzon Shaqiri Content Licensing Specialist: Shawntel Schmitt Cover Image: ©McGraw-Hill Education Compositor: MPS Limited All credits appearing on page or at the end of the book are considered to be an extension of the copyright page. Library of Congress Cataloging-in-Publication Data Forouzan, Behrouz A., author. | Gilberg, Richard F., author. C++ programming : an object-oriented approach / Behrouz A. Forouzan, Richard F. Gilberg. First edition. | New York, NY : McGraw-Hill Education, [2020] | Includes index. LCCN 2018030975| ISBN 9780073523385 (bound edition : acid-free paper) | ISBN 0073523380 (bound edition : acid-free paper) | ISBN 9781259571459 (loose-leaf edition) | ISBN 1259571459 (loose-leaf edition) LCSH: Object-oriented programming (Computer science) | C++ (Computer program language) LCC QA76.64 .F684 2019 | DDC 005.1/17—dc23 LC record available at https://lccn.loc.gov/2018030975 The Internet addresses listed in the text were accurate at the time of publication. The inclusion of a website does not indicate an endorse- ment by the authors or McGraw-Hill Education, and McGraw-Hill Education does not guarantee the accuracy of the information presented at these sites. mheducation.com/highered for23380_fm_i-xviii.indd 2 08/11/18 11:51 am
📄 Page
4
To my wife, Faezeh Behrouz A. Forouzan To my wife, Evelyn Richard F. Gilberg for23380_fm_i-xviii.indd 3 08/11/18 11:51 am
📄 Page
5
This page intentionally left blank
📄 Page
6
v Preface xv Chapter 1 Introduction to Computers and Programming Languages 1 Chapter 2 Basics of C++ Programming 19 Chapter 3 Expressions and Statements 59 Chapter 4 Selection 112 Chapter 5 Repetition 158 Chapter 6 Functions 208 Chapter 7 User-Defined Types: Classes 273 Chapter 8 Arrays 338 Chapter 9 References, Pointers, and Memory Management 380 Chapter 10 Strings 443 Chapter 11 Relationships among Classes 496 Chapter 12 Polymorphism and Other Issues 553 Chapter 13 Operator Overloading 597 Chapter 14 Exception Handling 657 Chapter 15 Generic Programming: Templates 693 Chapter 16 Input/Output Streams 716 Chapter 17 Recursion 776 Chapter 18 Introduction to Data Structures 813 Chapter 19 Standard Template Library (STL) 852 Chapter 20 Design Patterns Available online Brief Table of Contents for23380_fm_i-xviii.indd 5 08/11/18 11:51 am
📄 Page
7
Appendixes A-R Available online Appendix A Unicode Appendix B Positional Numbering System Appendix C C++ Expressions and Operators Appendix D Bitwise Operations Appendix E Bit Fields Appendix F Preprocessing Appendix G Namespaces Appendix H Ratios Appendix I Time Appendix J Lambda Expressions Appendix K Regular Expressions Appendix L Smart Pointers Appendix M Random Number Generation Appendix N References Appendix O Move versus Copy Appendix P A Brief Review of C++ 11 Appendix Q Unified Modeling Language (UML) Appendix R Bitset CheckPoints Available online True/False Questions Available online Review Questions Available online Glossary Available online Index 915 for23380_fm_i-xviii.indd 6 08/11/18 11:51 am
📄 Page
8
vii Preface xv What Is the C++ Language? xv Why This Book? xv Appendices xvi Instructor Resources xvii Acknowledgments xvii Introduction to Computers and Programming Languages 1 1.1 Computer System 1 1.2 Computer Languages 5 1.3 Language Paradigms 7 1.4 Program Design 10 1.5 Program Development 13 1.6 Testing 15 Key Terms 16 Summary 17 Problems 17 Basics of C++ Programming 19 2.1 C++ Programs 19 2.2 Variable, Value, and Constant 26 2.3 Components of a C++ Program 32 2.4 Data Types 36 Key Terms 52 Summary 52 Problems 52 Programming Projects 57 1 2 Contents for23380_fm_i-xviii.indd 7 08/11/18 11:51 am
📄 Page
9
viii Contents Expressions and Statements 59 3.1 Expressions 59 3.2 Type Conversion 71 3.3 Order of Evaluation 76 3.4 Overflow and Underflow 81 3.5 Formatting Data 85 3.6 Statements 93 3.7 Program Design 98 Key Terms 105 Summary 105 Problems 106 Programs 110 Selection 112 4.1 Simple Selection 112 4.2 Complex Decisions 126 4.3 Decisions on Specific Values 134 4.4 Conditional Expressions 142 4.5 Program Design 144 Key Terms 152 Summary 153 Problems 153 Programs 156 Repetition 158 5.1 Introduction 158 5.2 The while Statement 161 5.3 The for Statement 175 5.4 The do-while Statement 180 5.5 More About Loops 184 5.6 Other Related Statements 188 5.7 Program Design 191 Key Terms 203 Summary 203 Problems 204 Programs 206 3 4 5 for23380_fm_i-xviii.indd 8 08/11/18 11:51 am
📄 Page
10
Contents ix Functions 208 6.1 Introduction 208 6.2 Library Functions 213 6.3 User-Defined Functions 224 6.4 Data Exchange 233 6.5 More About Parameters 244 6.6 Scope and Lifetime 248 6.7 Program Design 256 Key Terms 265 Summary 265 Problems 266 Programs 269 User-Defined Types: Classes 273 7.1 Introduction 273 7.2 Classes 275 7.3 Constructors and Destructors 283 7.4 Instance Members 294 7.5 Static Members 302 7.6 Object-Oriented Programming 311 7.7 Designing Classes 320 Key Terms 332 Summary 332 Problems 333 Programs 335 Arrays 338 8.1 One-Dimensional Arrays 338 8.2 More on Arrays 349 8.3 Multidimensional Arrays 363 8.4 Program Design 369 Key Terms 376 Summary 376 Problems 376 Programs 378 6 7 8 for23380_fm_i-xviii.indd 9 08/11/18 11:51 am
📄 Page
11
x Contents References, Pointers, and Memory Management 380 9.1 References 380 9.2 Pointers 391 9.3 Arrays and Pointers 405 9.4 Memory Management 414 9.5 Program Design 425 Key Terms 437 Summary 437 Problems 437 Programs 442 Strings 443 10.1 C Strings 443 10.2 The C++ String Class 460 10.3 Program Design 484 Key Terms 492 Summary 492 Problems 493 Programs 494 Relationships among Classes 496 11.1 Inheritance 496 11.2 Association 519 11.3 Dependency 528 11.4 Program Design 532 Key Terms 546 Summary 546 Problems 547 Programs 550 Polymorphism and Other Issues 553 12.1 Polymorphism 553 12.2 Other Issues 567 9 10 11 12 for23380_fm_i-xviii.indd 10 08/11/18 11:51 am
📄 Page
12
Contents xi Key Terms 594 Summary 594 Problems 594 Programs 596 Operator Overloading 597 13.1 Three Roles of an Object 597 13.2 Overloading Principles 602 13.3 Overloading as a Member 605 13.4 Overloading as a Nonmember 621 13.5 Type Conversion 625 13.6 Designing Classes 626 Key Terms 652 Summary 653 Problems 653 Programs 654 Exception Handling 657 14.1 Introduction 657 14.2 Exceptions in Classes 675 14.3 Standard Exception Classes 682 Key Terms 688 Summary 688 Problems 689 Programs 692 Generic Programming: Templates 693 15.1 Function Template 693 15.2 Class Template 703 Key Terms 713 Summary 713 Problems 714 Programs 715 13 14 15 for23380_fm_i-xviii.indd 11 08/11/18 11:51 am
📄 Page
13
xii Contents Input/Output Streams 716 16.1 Introduction 716 16.2 Console Streams 720 16.3 File Streams 729 16.4 String Streams 751 16.5 Formatting Data 755 16.6 Program Design 766 Key Terms 773 Summary 773 Problems 774 Programs 774 Recursion 776 17.1 Introduction 776 17.2 Recursive Sort and Search 792 17.3 Program Design 803 Key Terms 808 Summary 808 Problems 809 Programs 811 Introduction to Data Structures 813 18.1 Introduction 813 18.2 Singly Linked List 815 18.3 Stacks and Queues 825 18.4 Binary Search Trees 841 Key Terms 849 Summary 850 Problems 850 Programs 851 16 17 18 for23380_fm_i-xviii.indd 12 08/11/18 11:51 am
📄 Page
14
Contents xiii Standard Template Library (STL) 852 19.1 Introduction 852 19.2 Iterators 853 19.3 Sequence Containers 856 19.4 Container Adapters 877 19.5 Associative Containers 884 19.6 Using Functions 894 19.7 Algorithms 899 Key Terms 910 Summary 910 Problems 911 Programs 911 Design Patterns Online 20.1 Introduction 20.2 Creational Patterns 20.3 Structural Patterns 20.4 Behavioral Patterns Key Terms Summary Problems Programs Online Appendices Appendix A Unicode Appendix B Positional Numbering System Appendix C C++ Expressions and Operators Appendix D Bitwise Operations Appendix E Bit Fields Appendix F Preprocessing 19 20 for23380_fm_i-xviii.indd 13 08/11/18 11:51 am
📄 Page
15
xiv Contents Appendix G Namespaces Appendix H Ratios Appendix I Time Appendix J Lambda Expressions Appendix K Regular Expressions Appendix L Smart Pointers Appendix M Random Number Generation Appendix N References Appendix O Move versus Copy Appendix P A Brief Review of C++ 11 Appendix Q Unified Modeling Language (UML) Appendix R Bitset CheckPoints Available online True/False Questions Available online Review Questions Available online Glossary Available online Index 915 for23380_fm_i-xviii.indd 14 08/11/18 11:51 am
📄 Page
16
xv This book complements a course designed to teach object-oriented programming using the syntax of the C++ language. It will also prepare students for advanced concepts such as data structure and design patterns. Students who have completed this course will be ready to take on any other object-oriented language course, a data-structure course, or a course about design patterns. What Is the C++ Language? C++ is a progressive programming language derived from its predecessors, the C language and the B language. The C++ language expands the idea of a struct to a class in which dif- ferent objects can be created from one single definition of a class with different values for each data element. Furthermore, the C++ language explores the idea of object-oriented languages that simulate real life. In real life, we define a type and then we have objects of that type. In the C++ language, we define a class and then we create objects from that class. C++ also in- cludes the idea of inheritance. In inheritance, we can create a class and then extend the defi- nition to create other classes, just as in real life where the idea of an animal can be extended to create the idea of a horse, a cow, a dog, and so on. Perhaps the most interesting addition to C++ is the idea of polymorphism. Polymor- phism gives us the ability to write several versions of an action with the same name to be used by different objects. This practice is found in real life when we use the verb open. We can say that we open a business, open a can, open a door, and so on. Although the word open is used in all cases, it elicits different actions on different objects. The most recent additions to C++ include the Standard Template Library (STL), a collection of predefined complex objects and actions that can be applied to those objects, as well as design patterns to make problem solving more efficient and coherent. Why This Book? The book has five distinctive goals as discussed below. Teach Computer Programming The book can be used as the first course in computer programming using the C++ language as the vehicle. Chapters 1 to 6 are designed for this purpose. They discuss computer systems and languages. They also discuss the basics of the C++ syntax and program controls, such as decisions and repetitions. Chapters 1 to 6 are essential to learning programming using the C++ language. Teach the Syntax of the C++ Language Chapters 7 to 12 are essential in the study of object-oriented programming. Although Chap- ters 8 and 9 are not directly related to the object-oriented features of C++, we believe that these two chapters can be taught after students understand the basics of object-oriented pro- gramming, which are discussed in Chapter 7. Preface for23380_fm_i-xviii.indd 15 08/11/18 11:51 am
📄 Page
17
xvi Preface Appendices Appendices can be found online and are divided into six categories. References Appendices A to E are designed to be used as references for students. Students may need to consult these appendices when studying chapters in the book. Language Knowledge Appendices F and G give students information about how C++ prepares a source code for compilation and how it handles names in different sections. Present New Features of C++ Chapters 13 to 17 discuss other topics normally taught in a first or second course in program- ming. They can be taught in any order. Discuss Data Structure and Introduce the STL Library Chapters 18 and 19 are an introduction to data structures. They prepare students for a course in data structures. Introduce Design Patterns Chapter 20 (which can be found online) gives simple and alternative solutions to some typical problems in object-oriented programming that would be more difficult to solve if patterns were not used. Chapter 20 gives students an insight to object-oriented programming through a set of standard solutions to specific problems. Although design patterns are nor- mally taught in computer graphics courses, we have applied them to nongraphic problems for students who have no graphical programming experience. Course Outline The twenty chapters of the book are outlined in the following figure. Can be taught in anywhere after chapter 7 Can be taught if time allows (second course) Data structures (optional) Design patterns (optional) Basic C++ language Object-oriented concepts Legend: Chapter 1 Chapter 6 Chapter 7 Chapter 10 Chapter 11 Chapter 12 Chapter 8 Chapter 9 Chapter 13 Chapter 17 Chapter 18 Chapter 19 Chapter 20 for23380_fm_i-xviii.indd 16 08/11/18 11:51 am
📄 Page
18
Preface xvii Advanced Topics Appendixes H to O discuss some advanced topics that were added to C++. They can be taught in the class, or students can use them as a source of additional information. Brief Review of C++ 11 Appendix P gives a brief review of C++ 11 topics that were not discussed in the appendices that discussed advanced topics. Brief Review of UML We have used UML diagrams in the text. Appendix Q provides general insight into UML as a tool for designing object-oriented projects. Bitset The concept of bitset becomes more popular when C++ is used in network programming. We have included this topic in Appendix R. Instructor Resources Accompanying this text are several additional resources which can be found online at www .mhhe.com/forouzan1e. These include CheckPoint questions that help instructors gauge stu- dent understanding after reading each section of a chapter. True/false and review questions are also available to further test student’s knowledge. Complete solutions to the CheckPoint features, true/false questions, review questions, and problems are provided as well. Lastly, Lecture PPTs, text image files, and sample programs are provided as well. Acknowledgments We would like to express our gratitude to the reviewers of this text. Their insight and sug- gestions over the last few years greatly influenced this first edition. In alphabetical order, the reviewers are as follows: Vicki H. Allan, Utah State University Kanad Biswas, Institute of Technology, Delhi Gary Dickerson, Union College Max I. Formitchev, Maximus Energy Cynthia C. Fry, Baylor University Barbara Guillott, Q&A Analyst, CGI, Lafayette, LA Jon Hanrath, Illinois Institute of Technology David Keathly, University of North Texas Robert Kramer, Youngstown State University Kami Makki, Lamar University Christopher J. Mallery, Principal Software Engineering Lead for Microsoft Michael L. Mick, Purdue University, Calumet Amar Raheja, California State Polytechnic University, Pomona Brendan Sheehan, University of Nevada–Reno At McGraw-Hill, we would like to thank the following editorial and production staff: Thomas Scaife, Senior Portfolio Manager; Suzy Bainbridge, Executive Portfolio Manager; Heather Ervolino, Product Developer; Shannon O’Donnell, Marketing Manager; Patrick Diller, Business Project Manager; and Jane Mohr, Content Project Manager. for23380_fm_i-xviii.indd 17 08/11/18 11:51 am
📄 Page
19
This page intentionally left blank
📄 Page
20
1 1 1.1 COMPUTER SYSTEM A computer system is made of two major components: hardware and software. The com- puter hardware is the physical equipment. The software is the collection of programs (instructions) that allow the hardware to do its job. 1.1.1 Computer Hardware The hardware of a computer system consists of six parts: a central processing unit (CPU), main memory, secondary storage, the input system, the output system, and the communica- tion system. These components are connected together by what is called a bus. Figure 1.1 shows these six components and their connection. Central Processing Unit (CPU) The central processing unit (CPU) consists of the arithmetic-logical unit (ALU), the con- trol unit, and a set of registers to hold data temporarily while being processed. The control unit is the traffic cop of the system; it coordinates all the operations of the system. The ALU In this chapter, we describe the components of a computer system and discuss the general ideas behind computer languages. The overview provided in this chapter will help prepare you for future chapters. You can skip the chapter in the first reading and return to it when you have a better understanding of programming. Introduction to Computers and Programming Languages Objectives After you have read and studied this chapter, you should be able to: • Discuss the two major components of a computer: hardware and software. • Describe the six parts of hardware: CPU, primary memory, secondary storage, input system, output system, and communication system. • Describe the two major categories of software: system software and application software. • Describe the evolution of computer languages from machine languages, to assembly languages, and to high-level languages. • Discuss four different paradigms of computer languages: procedural, object-oriented, functional, and logic. • Describe the two steps of program design: understand the problem and develop a solution. • Describe the multistep procedure that transforms a program written in the C++ language to an executable program. for23380_ch01_001-018.indd 1 02/11/18 5:02 pm
The above is a preview of the first 20 pages. Register to read the complete e-book.