C++ Unleashed: Advanced topics

945 206 0
C++ Unleashed: Advanced topics

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

The goal of this book is to provide a focused examination of each of these topics, covering the essential information you need to fully exploit the power of the C++ language. Many of the topics in this book deserve a book in their own right. Because it is not possible, given the available space, to cover every aspect of some of these subjects, the chapters in this book explain only what is most necessary for you to gain a working understanding of the technologies they describe. Often, you will find that the information provided here is sufficient for your immediate needs. Even if that is not always the case, these chapters provide a useful foundation in these advanced issues that will allow you to quickly gain a more comprehensive understanding of them with further study.

P/V Unleashed generic ISBN# Name Date Intro Lp#1 Introduction C++ Unleashed is a survey of advanced topics in C++. The goal of this book is to pro- vide a focused examination of each of these topics, covering the essential information you need to fully exploit the power of the C++ language. Many of the topics in this book deserve a book in their own right. Because it is not pos- sible, given the available space, to cover every aspect of some of these subjects, the chapters in this book explain only what is most necessary for you to gain a working understanding of the technologies they describe. Often, you will find that the informa- tion provided here is sufficient for your immediate needs. Even if that is not always the case, these chapters provide a useful foundation in these advanced issues that will allow you to quickly gain a more comprehensive understanding of them with further study. What Is Covered Part I, “Object-Oriented Programming” We begin with a comprehensive introduction to object-oriented analysis and design. It is my view that C++ is best used to implement a well-designed object-oriented model, rather than to bang out quick-and-dirty code. The significant advantages of object-orient- ed programming can only be realized once you have done the necessary analysis and put the time in to design a well-conceived product. Chapter 1 will get you started on the dif- ficult but rewarding path of object modeling. Along the way, I’ll teach you the funda- mentals of the Unified Modeling Language (UML)—the emerging industry standard. In Chapter 2, you’ll learn how to implement your object model in C++. This mapping, from design model to code, is essential if you want to use C++ to its fullest potential as an object-oriented programming language. Chapter 3 continues this theme, focusing on how C++ supports inheritance and polymor- phism. This detailed examination of the intricacies of polymorphism will lay the ground- work for creating high-quality commercial C++ applications. Part II, “Implementation Issues” In Chapter 4, we discuss advanced memory management techniques. We’ll consider advanced issues with pointers and references and we’ll discuss auto pointers and smart pointers. In Chapter 5, we’ll discuss application frameworks and, within that context, we’ll consider such advanced topics as multi-threading. 01 239-5 Intro 2/19/99 12:56 PM Page 1 C++ UNLEASHED 2 Also in Part II, we’ll offer an in-depth introduction to the Standard Template Library. Chapter 6 focuses on the STL container classes and Chapter 7 follows with a discussion of STL iterators and algorithms. In Chapter 8, we move on to one of the newest features of ANSI C++—namespaces—and we consider how namespaces can help you avoid name clashes as you use third-party libraries. In Chapter 9, we focus on runtime type identification and the new ANSI-style casting operators. Finally, in Chapter 10, we’ll consider how you tune your application perfor- mance to optimize for speed or code size. Part III, “Manipulating Data” Part III opens Chapter 11—a discussion of advanced techniques using recursion. In Chapter 12, we discuss sorting algorithms, and in Chapter 13, we discuss object-oriented searching. This discussion is rounded out in Chapter 14 with a consideration of hashing and parsing techniques. Part IV, “Object Persistence and Encryption” Chapter 15 considers object persistence and demonstrates how to write your objects to disk and how to manage memory with persistent objects. Chapter 16 returns to the appli- cation frameworks and considers ODBC and MFC Database connections. Chapter 17 extends this discussion to consider object persistence using relational databases, and Chapter 18 discusses object-oriented databases. Finally, in Chapter 19, we discuss encryption including Diffie, Hellerman, Hoffman, and Caesar ciphers; public encryption and popular encryption approaches such as Pretty Good Privacy; and DES and Clipper. Part V, “Distributed Computing Topics” Chapter 20 considers CORBA, and Chapter 21 provides an in-depth introduction to COM. Finally, Chapter 22 examines the differences between Java and C++ and considers whether these differences are significant. What You Need To Know Already C++ Unleashed assumes you have read at least one good primer (such as Sams Teach Yourself C++ in 21 Days) and/or have been programming in C++ for at least six months. More experienced programmers will find detail on subjects they may not have consid- ered before; less experienced programmers will find a host of new ideas, information, and best practices. P2/V3 C++ Unleashed ISBN# Name Date Intro Lp#1 01 239-5 Intro 2/19/99 12:56 PM Page 2 INTRODUCTION 3 What Software You Need All of the programs in this book can be created and run with Microsoft Visual C++ or any ANSI-compliant 32-bit compiler. While the example programs in the chapters on the MFC will only compile on a Windows machine (Windows 95 or Windows NT), just about all the other programs in the book will compile on any operating system. You need no other software—just an editor, compiler, and linker. If you use an integrated development environment such as Visual C++, you are all set. While we’ve endeavored to test all the programs in this book on a number of compilers, we do know that it all works in Microsoft Visual C++, and thus we recommend that compiler if you don’t already have another. How To Read This Book Think of this book as a series of “white papers” on advanced topics in C++. Feel free to jump around among the chapters, dipping into those areas which intrigue you. Again, remember that we made no attempt to be “comprehensive” on each topic; rather, our goal was to provide detailed introductions to these advanced topics. Each of these topics is the subject of one or more advanced books. Our goal here is to provide the essential information necessary for you to either start your further study or to obtain a quick and useful overview. One good way to read this book is as Humpty Dumpty advised: begin at the beginning, proceed to the end, and then stop. As an alternative, you might want to read the first three chapters and then pick and choose among those topics which are of most interest to you. In any case, enjoy and please let us know how we did. You can reach me, Jesse Liberty, on the Internet at jliberty@libertyassociates.com. There is support for the book at the Sams Web site (http://samspublishing.com) as well as at my own Web site (http://www.libertyassociates.com)—click the books and resources link. P/V Unleashed generic ISBN# Name Date Intro Lp#1 01 239-5 Intro 2/19/99 12:56 PM Page 3 4 P2/V3 C++ Unleashed ISBN# Name Date Intro Lp#1 01 239-5 Intro 2/19/99 12:56 PM Page 4 P/V Unleashed generic ISBN# Name Date Part Lp#1 Object-Oriented Programming PART I IN THIS PART • Object-Oriented Analysis and Design 7 • Implementing Class Design in C++ 45 • Inheritance, Polymorphism, and Code Reuse 77 03 239-5 Part 1 2/19/99 12:57 PM Page 5 P/V Unleashed generic ISBN# Name Date Part Lp#1 03 239-5 Part 1 2/19/99 12:57 PM Page 6 IN THIS CHAPTER • Building Models 8 • Software Design: The Modeling Language 9 • Software Design: The Process 10 • The Vision 13 • Requirements Analysis 13 • Design 27 1 CHAPTER Object-Oriented Analysis and Design 02 239-5 CH01 2/19/99 12:58 PM Page 7 C++ was created as a bridge between \object-oriented programming and C, the world’s most popular programming language for commercial software development. The goal was to provide object-oriented design to a fast, commercial software development platform. C was developed as a middle ground between high-level business applications languages such as COBOL and the pedal-to-the-metal, high-performance, but difficult-to-use Assembler language. C was to enforce “structured” programming, in which problems were “decomposed” into smaller units of repeatable activities called procedures. The programs we’re writing at the end of the 1990s are far more complex than those written at the beginning of the decade. Programs created in procedural languages tend to be difficult to manage, hard to maintain, and impossible to extend. Graphical user inter- faces, the Internet, digital telephony, and a host of new technologies have dramatically increased the complexity of our projects at the very same time that consumer expecta- tions for the quality of the user interface are rising. In the face of this increasing complexity, developers took a long hard look at the state of the industry. What they found was disheartening, at best. Software was late, broken, defective, bug ridden, unreliable, and expensive. Projects routinely ran over budget and were delivered late to market. The cost of maintaining and building on these projects was prohibitive, and a tremendous amount of money was being wasted. Object-oriented software development offers a path out of the abyss. Object-oriented pro- gramming languages build a strong link between the data structures and the methods that manipulate that data. More important, in object-oriented programming, you no longer think about data structures and manipulating functions; you think instead about objects. Things. The world is populated by things: cars, dogs, trees, clouds, flowers. Things. Each thing has characteristics (fast, friendly, brown, puffy, pretty). Most things have behavior (move, bark, grow, rain, wilt). We don’t think about a dog’s data and how we might manipulate it—we think about a dog as a thing in the world, what it is like and what it does. Building Models If we are to manage complexity, we must create a model of the universe. The goal of the model is to create a meaningful abstraction of the real world. Such an abstraction should be simpler than the real world but should also accurately reflect the real world so that we can use the model to predict the behavior of things in the real world. Object-Oriented Programming PART I 8 P/V C++ Unleashed 72312395 Freelance 10.2.98 CH01 Lp#1 02 239-5 CH01 2/19/99 12:58 PM Page 8 A child’s globe is a classic model. The model isn’t the thing itself; we would never con- fuse a child’s globe with the Earth, but one maps the other well enough that we can learn about the Earth by studying the globe. There are, of course, significant simplifications. My daughter’s globe never has rain, floods, globe-quakes and so forth, but I can use her globe to predict how long it will take me to fly from my home to Indianapolis should I ever need to come in and explain myself to the Sams senior management when they ask me why my manuscript was late (“You see, I was doing great, but then I got lost in a metaphor and it took me hours to get out”). A model that is not simpler than the thing being modeled is not much use. There is a Steven Wright joke about just such a thing: “I have a map on which one inch equals one inch. I live at E5.” Object-oriented software design is about building good models. It consists of two signifi- cant pieces: a modeling language and a process. Software Design: The Modeling Language The modeling language is the least important aspect of object-oriented analysis and design; unfortunately, it tends to get the most attention. A modeling language is nothing more than a convention for how we’ll draw our model on paper. We can easily decide that we’ll draw our classes as triangles, and that we’ll draw the inheritance relationship as a dotted line. If so, we might model a geranium as shown in Figure 1.1. Object-Oriented Analysis and Design CHAPTER 1 9 1 OBJECT-ORIENTED ANALYSIS AND DESIGN P/V C++ Unleashed 72312395 Freelance 10.2.98 CH01 Lp#1 FIGURE 1.1. Generalization/ specialization. Flower Geranium 02 239-5 CH01 2/19/99 12:58 PM Page 9 In the figure, you see that a Geranium is a special kind of Flower. If you and I agree to draw our inheritance (generalization/specialization) diagrams like this, we’ll understand each other perfectly. Over time, we’ll probably want to model lots of complex relation- ships, and so we’ll develop our own complicated set of diagramming conventions and rules. Of course, we’ll need to explain our conventions to everyone else with whom we work, and each new employee or collaborator will have to learn our convention. We may inter- act with other companies that have their own conventions, and we’ll need to allow time to negotiate a common convention and to compensate for the inevitable misunderstandings. It would be more convenient if everyone in the industry agreed on a common modeling language. (For that matter, it would be convenient if everyone in the world agreed on a spoken language, but one thing at a time.) The lingua franca of software development is UML—The Unified Modeling Language. The job of the UML is to answer questions like, “How do we draw an inheritance relationship?” The geranium drawing shown in Figure 1.1 would be drawn as shown in Figure 1.2 in UML. Object-Oriented Programming PART I 10 P/V C++ Unleashed 72312395 Freelance 10.2.98 CH01 Lp#1 FIGURE 1.2. UML drawing of specialization. Flower Geranium In UML, classes are drawn as rectangles, and inheritance is drawn as a line with an arrowhead. Interestingly, the arrowhead points from the more specialized class to the more general class. The direction of the arrow is counter-intuitive for most folks, but it doesn’t matter much; once we all agree, the system works just fine. The details of the UML are rather straightforward. The diagrams are not hard to use or understand, and I’ll explain them as we go along in this chapter and throughout the book, rather than trying to teach the UML out of context. Although it is possible to write a whole book on the UML, the truth is that 90 percent of the time, you use only a small subset of the UML notation, and that subset is easily learned. Software Design: The Process The process of object-oriented analysis and design is far more complex and far more important than the modeling language. So of course, it is what you hear far less about. 02 239-5 CH01 2/19/99 12:58 PM Page 10 [...]... chapters in the rest of this book explain how to implement various architecture mechanisms What Are the Classes? As a C++ programmer, you are used to creating classes Formal design methodology requires you to separate the C++ class from the design class, though they will be intimately related The C++ class you write in code is the implementation of the class you designed These are isomorphic: each class in... highly abstract When you say that in your model your Cat class will have a Meow() method, understand that this means that you will put a Meow() method into your C++ class as well You capture the model’s classes in UML diagrams and you capture the C++ classes in code which can be compiled The distinction is meaningful yet subtle In any case, the biggest stumbling block for many novices is finding the initial... The more you factor out of the derived classes, the more polymorphic your design will be One of the capabilities available in C++, which is not available in Java, is multiple inheritance (although Java has a similar, if limited, capability with its multiple interfaces) P/V C++ Unleashed 72312395 35 Freelance 10.2.98 CH01 Lp#1 02 239-5 CH01 36 2/19/99 12:58 PM Page 36 Object-Oriented Programming PART... to articulate and capture these requirements The outcome of the Analysis phase is the production of a requirements document The first section in the requirements document is the use case analysis P/V C++ Unleashed 72312395 Freelance 10.2.98 CH01 Lp#1 1 OBJECT-ORIENTED ANALYSIS AND DESIGN Although the newsgroups and object-technology mailing lists thrive on splitting hairs, the essentials of object-oriented... lay out a practical approach to the process as the bedrock on which you can build the architecture of your application In the rest of the book, we’ll focus on the details of implementing your design in C++ 13 02 239-5 CH01 14 2/19/99 12:58 PM Page 14 Object-Oriented Programming PART I Use Cases The driving force in analysis, design, and implementation is the use cases A use case is nothing more than... systems that will interact with your new system Remember that when we discuss people, we really mean roles—the bank clerk, the manager, the customer, and so forth One person can have more than one role P/V C++ Unleashed 72312395 Freelance 10.2.98 CH01 Lp#1 02 239-5 CH01 2/19/99 12:58 PM Page 15 Object-Oriented Analysis and Design CHAPTER 1 • The customer • The bank personnel • A back-office system • The... appropriate account Given that the actor and the system behave and respond more or less identically, regardless of whether the deposit is made to the checking or the savings account, these two use P/V C++ Unleashed 72312395 Freelance 10.2.98 CH01 Lp#1 1 OBJECT-ORIENTED ANALYSIS AND DESIGN For the ATM example just mentioned, we can expect such a list to include the following roles: 15 02 239-5 CH01 16... account • Customer withdraws money from his or her account • Customer transfers money between accounts • Customer opens an account • Customer closes an account • Customer logs into his or her account P/V C++ Unleashed 72312395 Freelance 10.2.98 CH01 Lp#1 02 239-5 CH01 2/19/99 12:58 PM Page 17 Object-Oriented Analysis and Design CHAPTER 1 • Customer checks recent transactions 1 OBJECT-ORIENTED ANALYSIS... important to realize that what we are describing here are not design objects, but rather objects in the domain This is documentation of how the world works, not documentation of how our system will work P/V C++ Unleashed 72312395 17 Freelance 10.2.98 CH01 Lp#1 02 239-5 CH01 18 2/19/99 12:58 PM Page 18 Object-Oriented Programming PART I We can diagram the relationship among the objects in the domain of the... language, and there are any number of relationships you can capture The principal relationships captured in analysis, however, are generalization (or specialization), containment, and association P/V C++ Unleashed 72312395 Freelance 10.2.98 CH01 Lp#1 02 239-5 CH01 2/19/99 12:58 PM Page 19 Object-Oriented Analysis and Design CHAPTER 1 Generalization 1 Generalization implies that the derived object is . P/V Unleashed generic ISBN# Name Date Intro Lp#1 Introduction C++ Unleashed is a survey of advanced topics in C++. The goal of this book is to pro- vide. Java and C++ and considers whether these differences are significant. What You Need To Know Already C++ Unleashed assumes you have read at least one good primer (such as Sams Teach Yourself C++ in. link. P/V Unleashed generic ISBN# Name Date Intro Lp#1 01 239-5 Intro 2/19/99 12:56 PM Page 3 4 P2/V3 C++ Unleashed ISBN# Name Date Intro Lp#1 01 239-5 Intro 2/19/99 12:56 PM Page 4 P/V Unleashed

Ngày đăng: 04/07/2014, 15:26

Mục lục

    Contents at a Glance

    Tell Us What You Think!

    Part I Object-Oriented Programming

    Ch 1 Object-Oriented Analysis and Design

    Ch 2 Implementing Class Design in C++

    Ch 3 Inheritance, Polymorphism, and Code Reuse

    Part II Implementation Issues

    Ch 5 How to Use Frameworks

    Ch 6 Standard Template Library Container Classes

    Ch 7 STL Iterators and Algorithms

Tài liệu cùng người dùng

Tài liệu liên quan