ebook img

The art of C++ PDF

349 Pages·2004·1.235 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 The art of C++

TThhee AArrtt ooff CC++++ bbyy HHeerrbbeerrtt SScchhiillddtt IISSBBNN::00007722225555112299 MMccGGrraaww--HHiillll//OOssbboorrnnee ©© 22000044 TThhee aauutthhoorr ooff tthhiiss tteexxtt aapppplliieess CC++++ ttoo aa wwiiddee vvaarriieettyy ooff hhiigghh--ppoowweerreedd,, pprraaccttiiccaall aapppplliiccaattiioonnss,, eeaacchh ffooccuussiinngg oonn aa ddiiffffeerreenntt aassppeecctt ooff tthhee llaanngguuaaggee.. EExxaammpplleess rraannggee ffrroomm aa ggaarrbbaaggee ccoolllleeccttoorr ssuubbssyysstteemm aanndd aa tthhrreeaadd ccoonnttrrooll ppaanneell ttoo AAII--bbaasseedd sseeaarrcchheess aanndd mmoorree.. TTaabbllee ooff CCoonntteennttss TThhee AArrtt ooff CC++++ PPrreeffaaccee CChhaapptteerr 11 -- TThhee PPoowweerr ooff CC++++ CChhaapptteerr 22 -- AA SSiimmppllee GGaarrbbaaggee CCoolllleeccttoorr ffoorr CC++++ CChhaapptteerr 33 -- MMuullttiitthhrreeaaddiinngg iinn CC++++ CChhaapptteerr 44 -- EExxtteennddiinngg CC++++ CChhaapptteerr 55 -- AAnn IInntteerrnneett FFiillee DDoowwnnllooaaddeerr CChhaapptteerr 66 -- FFiinnaanncciiaall CCaallccuullaattiioonnss iinn CC++++ CChhaapptteerr 77 -- AAII--BBaasseedd PPrroobblleemm SSoollvviinngg CChhaapptteerr 88 -- BBuuiillddiinngg aa CCuussttoomm SSTTLL CCoonnttaaiinneerr CChhaapptteerr 99 -- AA MMiinnii CC++++ IInntteerrpprreetteerr IInnddeexx LLiisstt ooff FFiigguurreess LLiisstt ooff TTaabblleess Back Cover Take your C++ skills to the next level with help from programming guru Herb Schildt. In this unique volume, Herb unlocks the secrets that professional programmers use to create world-class software. In the process, he explores the power, elegance, and artistry of C++. Inside, Herb applies C++ to a wide variety of high-powered, practical applications, each focusing on a different aspect of the language. Examples range from a garbage collector subsystem and a thread control panel to AI-based searches and a custom STL container. Herb even develops a Mini C++ interpreter! You can use each application as-is or as a starting point for your own development. This is some of the most exciting C++ code found anywhere! Inside You’ll Explore the power of C++ Create a garbage collector subsystem for memory management Develop a thread control panel Build a translator to extend the capabilities of C++ Develop a restartable Internet file downloader Create a financial analysis library Explore artificial intelligence with AI-based search techniques Build a custom STL container Develop a Mini C++ Interpreter About the Author Herbert Schildt is the world’s number one programming author. He is an authority on the C, C++, Java, and C# programming languages, and was a member of the ANSI/ISO committee that standardized C++. His programming books have sold more than three million copies worldwide and have been translated into all major foreign languages. The Art of C++ Herbert Schildt McGraw-Hill/Osborne 2100 Powell Street, 10th Floor Emeryville, California 94608 U.S.A. To arrange bulk purchase discounts for sales promotions, premiums, or fund-raisers, please contact McGraw- Hill/Osborne at the above address. For information on translations or book distributors outside the U.S.A., please see the International Contact Information page immediately following the index of this book. The Art of C++ Copyright © 2004 by The McGraw-Hill Companies. All rights reserved. Printed in the United States of America. Except as permitted under the Copyright Act of 1976, 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 permission of publisher, with the exception that the program listings may be entered, stored, and executed in a computer system, but they may not be reproduced for publication. 1234567890 CUS CUS 01987654 ISBN 0-07-225512-9 Publisher Brandon A. Nordin Vice President & Associate Publisher Scott Rogers Editorial Director Wendy Rinaldi Project Editor Jenn Tust Acquisitions Coordinator Athena Honore Technical Editor Fahad Gilani Copy Editor Sally Engelfried Proofreader Marian Selig Indexer Sheryl Schildt Composition Carie Abrew, Lucie Ericksen Illustrator Melinda Lytle, Kathleen Edwards, Gregg Scott Series Design Roberta Steele Cover Design Jeff Weeks This book was composed with Corel VENTURA™ Publisher. Information has been obtained by McGraw-Hill/Osborne from sources believed to be reliable. However, because of the possibility of human or mechanical error by our sources, McGraw-Hill/Osborne, or others, McGraw-Hill/Osborne does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from the use of such information. About the Author Herbert Schildt is a leading authority on the C, C++, Java, and C# languages, and is a master Windows programmer. His programming books have sold more than 3 million copies worldwide and have been translated into all major foreign languages. He is the author of numerous C++ bestsellers, including C++: The Complete Reference, C++ From the Ground Up, C++: A Beginner’s Guide, and STL Programming From the Ground Up. His other bestsellers include C: The Complete Reference, Java 2: The Complete Reference, and C#: The Complete Reference. Schildt holds a master’s degree in computer science from the University of Illinois. He can be reached at his consulting office at (217) 586-4683. Preface Since the early days of FORTRAN, the on-going development of computer languages has followed what can best be described as an evolutionary path, with the efforts of the past shaping developments in the future. In this process, weak features fell away and misguided efforts terminated in dead-ends. Over the years, these evolutionary forces distilled into undiluted form, the pure essence of what a programming language should be. The result was C++, and no other language holds a more important place in the history programming. There are many reasons for the success of C++. Its syntax is terse, yet elegant, its object- model is streamlined and conceptually clean, and its libraries are carefully crafted and interlocking. These features by themselves, however, are not what earned C++ a place in history. Instead, it is these things coupled with the power that C++ puts into the hands of the programmer. No language before or since has given the programmer more direct control over the computer. With C++, the programmer is master of the machine—and this is the way that all programmers want it. No boundaries, no limits, no constraints. That’s C++. What’s Inside This book is different from most other books on C++. Whereas other books teach the basics of the language, this book shows how to apply it to a wide range of interesting, useful, and, at times, mysterious programming tasks. In the process, it displays the power and elegance of the C++ language. Thus, it is through the art of C++ that the artistry of C++’s design is displayed. In general, the book contains two types of applications. The first type I call “pure code” because they focus on expanding the C++ programming environment, itself. The garbage collector in Chapter 2, the thread control panel in Chapter 3, and the custom STL container in Chapter 8 are examples. The second type shows how C++ can be applied to a variety of computing tasks. For example, Chapter 5 develops a restartable Internet downloader, Chapter 6 shows how to create financial applications, and Chapter 8 applies C++ to Artificial Intelligence. The book ends with a unique and interesting piece of code: the Mini C++ interpreter, which interprets a small subset of C++. Mini C++ gives insight into how the keywords and syntax of C++ work together to create the grammar of the language. Moreover, it lets you get “inside the language,” showing some of the reasons behind C++’s design. Although Mini C++ is fun to use as-is, it can also serve as a starting point for your own language development, or be adapted to work as an interpreter for any other language. Each chapter develops code that you can use as-is, without changes. For example, the garbage collector in Chapter 2 is applicable to many programming tasks. However, the real benefits result when you use the applications as starting points for your own development. For example, the Internet file downloader in Chapter 8 could be enhanced to start a download at a specific time, or to watch a download site, waiting to download until an updated file is posted. In general, think of the various programs and subsystems as launching pads for your own projects. Knowledge of C++ Is Assumed This book assumes that you have a solid grounding in the fundamentals of the C++ language. You should be able to create, compile, and run C++ programs. You should be able to use pointers, templates, and exceptions, understand copy constructors, and be familiar with the most common parts of the standard library. Thus, this book assumes that you have the skills that one would acquire in a course on C++. If you need to refresh or enhance your basic knowledge, I recommend the following books. C++ From the Ground Up C++: A Beginner’s Guide C++: The Complete Reference All are published by McGraw-Hill/Osborne. Don’t Forget: Code on the Web Remember, the source code for all of the examples and projects in this book is available free-of-charge on the Web at www.osborne.com. More From Herb Schildt The Art of C++ is just one in the Herb Schildt series of programming books. Here are some others that you will find of interest. To learn more about C++, we recommend the following. C++: The Complete Reference C++: A Beginner’s Guide Teach Yourself C++ C++ From the Ground Up STL Programming From the Ground Up To learn about Java programming, you will find these books especially helpful. Java 2: The Complete Reference The Art of Java (Co-authored with James Holmes) Java 2: A Beginner’s Guide Java 2: Programmer’s Reference To learn about C# we suggest the following books: C#: A Beginner’s Guide C#: The Complete Reference If you want to learn about the C language, the foundation of all modern programming, then the following titles will be of interest. C: The Complete Reference Teach Yourself C Chapter 1: The Power of C++ Overview C++ is all about power: the power to control the machine at its lowest level, the power to produce highly optimized code, the power to interact directly with the operating system. This power runs deep and wide. With C++ you have detailed control over objects, including their creation, destruction, and inheritance; access to pointers; and support for low-level I/O. You can add new features by defining classes and overloading operators. You can construct your own libraries and hand-optimize code. You can even “break the rules” when necessary. C++ is not a language for the timid. It is for the programmer who demands and deserves the world’s most powerful programming language. Of course, C++ is not just raw power. It is power channeled, focused, and directed. Its careful design, rich libraries, and subtle syntax yield a programming environment that is both flexible and agile. Although C++ excels in the creation of high-performance system code, it is suitable for any type of programming task. For example, its string handling is second to none, its math and numeric capabilities make it excellent for scientific programming, and its ability to produce fast object code makes it perfect for any CPU-intensive task. The purpose of this book is to demonstrate the power, scope, and agility of C++. It does so by applying it to a varied cross section of applications. Some of the applications demonstrate the capabilities of the language itself. These are called “pure code” examples because they show the expressiveness of the C++ syntax and the elegance of its design. The garbage collector in Chapter 2 and the C++ interpreter in Chapter 9 are examples. Other applications illustrate the ease with which C++ can be applied to common programming tasks. For example, the download manager in Chapter 5 illustrates the ability of C++ to create high-performance networked code. Chapter 6 applies C++ to various financial computations. Collectively, the applications show the wide ranging versatility of C++. Before moving on to the applications, however, it is good to reflect for a moment on the things that make C++ such a great language. To this end, this chapter takes some time to point out several of the features that define the “power of C++.” A Terse yet Rich Syntax If there is one fundamental defining characteristic of C++, it is the terseness of its syntax. C++ defines only 63 keywords. In a seeming contradiction, much of the power of C++ is derived from not building into the language more features than are necessary. Instead, C++ defines a rich but compact syntax that provides the control statements, operators, data types, and object-oriented features necessary for any modern language—but not more! Thus, the C++ syntax is clean, consistent, and uncluttered. This lean philosophy has two important benefits. First, the keywords and syntax of C++ apply to all environments in which C++ can be used. That is, the core features of C++ are universally available to all applications, independent of the execution environment. Features that are sensitive to the execution environment, such as multithreading, are left to the operating system, which is best able to handle them efficiently. Thus, C++ does not attempt a “one size fits all” solution, which can lead to degraded runtime performance. Second, a streamlined, logically consistent syntax allows complex constructs to be expressed with clarity. This is an important advantage in a world in which programs have grown to truly enormous sizes. While it is, of course, true that a poor programmer will write poor C++ code, a good programmer can create amazingly clear and concise code. This ability to transparently represent complicated logic is one of the reasons that the C++ syntax has become the near universal language of programming.

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.