⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch01.htm

📁 一本好的VC学习书,本人就是使用这本书开始学习的vc,希望能对大家有帮助
💻 HTM
📖 第 1 页 / 共 3 页
字号:
	presses a button or chooses from a menu--and the program must respond. Programs are	becoming increasingly interactive, and it has became important to design for that	kind of functionality. <HR></DL><P>Old-fashioned programs forced the user to proceed step-by-step through a seriesof screens. Modern event-driven programs present all the choices at once and respondto the user's actions.</P><P>Object-oriented programming attempts to respond to these needs, providing techniquesfor managing enormous complexity, achieving reuse of software components, and couplingdata with the tasks that manipulate that data.</P><P>The essence of object-oriented programming is to treat data and the proceduresthat act upon the data as a single &quot;object&quot;--a self-contained entity withan identity and certain characteristics of its own.<H4 ALIGN="CENTER"><A NAME="Heading8"></A><FONT COLOR="#000077">C++ and Object-OrientedProgramming</FONT></H4><P>C++ fully supports object-oriented programming, including the four pillars ofobject-oriented development: encapsulation, data hiding, inheritance, and polymorphism.Encapsulation and Data Hiding When an engineer needs to add a resistor to the deviceshe is creating, she doesn't typically build a new one from scratch. She walks overto a bin of resistors, examines the colored bands that indicate the properties, andpicks the one she needs. The resistor is a &quot;black box&quot; as far as the engineeris concerned--she doesn't much care how it does its work as long as it conforms toher specifications; she doesn't need to look inside the box to use it in her design.</P><P>The property of being a self-contained unit is called encapsulation. With encapsulation,we can accomplish data hiding. Data hiding is the highly valued characteristic thatan object can be used without the user knowing or caring how it works internally.Just as you can use a refrigerator without knowing how the compressor works, youcan use a well-designed object without knowing about its internal data members.</P><P>Similarly, when the engineer uses the resistor, she need not know anything aboutthe internal state of the resistor. All the properties of the resistor are encapsulatedin the resistor object; they are not spread out through the circuitry. It is notnecessary to understand how the resistor works in order to use it effectively. Itsdata is hidden inside the resistor's casing.</P><P>C++ supports the properties of encapsulation and data hiding through the creationof user-defined types, called classes. You'll see how to create classes on Day 6,&quot;Basic Classes.&quot; Once created, a well-defined class acts as a fully encapsulatedentity--it is used as a whole unit. The actual inner workings of the class shouldbe hidden. Users of a well-defined class do not need to know how the class works;they just need to know how to use it. Inheritance and Reuse When the engineers atAcme Motors want to build a new car, they have two choices: They can start from scratch,or they can modify an existing model. Perhaps their Star model is nearly perfect,but they'd like to add a turbocharger and a six-speed transmission. The chief engineerwould prefer not to start from the ground up, but rather to say, &quot;Let's buildanother Star, but let's add these additional capabilities. We'll call the new modela Quasar.&quot; A Quasar is a kind of Star, but one with new features.</P><P>C++ supports the idea of reuse through inheritance. A new type, which is an extensionof an existing type, can be declared. This new subclass is said to derive from theexisting type and is sometimes called a derived type. The Quasar is derived fromthe Star and thus inherits all its qualities, but can add to them as needed. Inheritanceand its application in C++ are discussed on Day 12, &quot;Inheritance,&quot; andDay 15, &quot;Advanced Inheritance.&quot; Polymorphism The new Quasar might responddifferently than a Star does when you press down on the accelerator. The Quasar mightengage fuel injection and a turbocharger, while the Star would simply let gasolineinto its carburetor. A user, however, does not have to know about these differences.He can just &quot;floor it,&quot; and the right thing will happen, depending on whichcar he's driving.</P><P>C++ supports the idea that different objects do &quot;the right thing&quot; throughwhat is called function polymorphism and class polymorphism. Poly means many, andmorph means form. Polymorphism refers to the same name taking many forms, and isdiscussed on Day 10, &quot;Advanced Functions,&quot; and Day 13, &quot;Polymorphism.&quot;<H3 ALIGN="CENTER"><A NAME="Heading9"></A><FONT COLOR="#000077">How C++ Evolved</FONT></H3><P>As object-oriented analysis, design, and programming began to catch on, BjarneStroustrup took the most popular language for commercial software development, C,and extended it to provide the features needed to facilitate object-oriented programming.He created C++, and in less than a decade it has gone from being used by only a handfulof developers at AT&amp;T to being the programming language of choice for an estimatedone million developers worldwide. It is expected that by the end of the decade, C++will be the predominant language for commercial software development.</P><P>While it is true that C++ is a superset of C, and that virtually any legal C programis a legal C++ program, the leap from C to C++ is very significant. C++ benefitedfrom its relationship to C for many years, as C programmers could ease into theiruse of C++. To really get the full benefit of C++, however, many programmers foundthey had to unlearn much of what they knew and learn a whole new way of conceptualizingand solving programming problems.<H3 ALIGN="CENTER"><A NAME="Heading10"></A><FONT COLOR="#000077">The ANSI Standard</FONT></H3><P>The Accredited Standards Committee, operating under the procedures of the AmericanNational Standards Institute (ANSI), is working to create an international standardfor C++.</P><P>The draft of this standard has been published, and a link is available at <TT>www.libertyassociates.com</TT>.</P><P>The ANSI standard is an attempt to ensure that C++ is portable--that code youwrite for Microsoft's compiler will compile without errors, using a compiler fromany other vendor. Further, because the code in this book is ANSI compliant, it shouldcompile without errors on a Mac, a Windows box, or an Alpha.</P><P>For most students of C++, the ANSI standard will be invisible. The standard hasbeen stable for a while, and all the major manufacturers support the ANSI standard.We have endeavored to ensure that all the code in this edition of this book is ANSIcompliant.<H3 ALIGN="CENTER"><A NAME="Heading11"></A><FONT COLOR="#000077">Should I Learn CFirst?</FONT></H3><P>The question inevitably arises: &quot;Since C++ is a superset of C, should I learnC first?&quot; Stroustrup and most other C++ programmers agree. Not only is it unnecessaryto learn C first, it may be advantageous not to do so. This book attempts to meetthe needs of people like you, who come to C++ without prior experience of C. In fact,this book assumes no programming experience of any kind.<H3 ALIGN="CENTER"><A NAME="Heading12"></A><FONT COLOR="#000077">Preparing to Program</FONT></H3><P>C++, perhaps more than other languages, demands that the programmer design theprogram before writing it. Trivial problems, such as the ones discussed in the firstfew chapters of this book, don't require much design. Complex problems, however,such as the ones professional programmers are challenged with every day, do requiredesign, and the more thorough the design, the more likely it is that the programwill solve the problems it is designed to solve, on time and on budget. A good designalso makes for a program that is relatively bug-free and easy to maintain. It hasbeen estimated that fully 90 percent of the cost of software is the combined costof debugging and maintenance. To the extent that good design can reduce those costs,it can have a significant impact on the bottom-line cost of the project.</P><P>The first question you need to ask when preparing to design any program is, &quot;Whatis the problem I'm trying to solve?&quot; Every program should have a clear, well-articulatedgoal, and you'll find that even the simplest programs in this book do so.</P><P>The second question every good programmer asks is, &quot;Can this be accomplishedwithout resorting to writing custom software?&quot; Reusing an old program, usingpen and paper, or buying software off the shelf is often a better solution to a problemthan writing something new. The programmer who can offer these alternatives willnever suffer from lack of work; finding less-expensive solutions to today's problemswill always generate new opportunities later.</P><P>Assuming you understand the problem, and it requires writing a new program, youare ready to begin your design.<H3 ALIGN="CENTER"><A NAME="Heading13"></A><FONT COLOR="#000077">Your DevelopmentEnvironment</FONT></H3><P>This book makes the assumption that your computer has a mode in which you canwrite directly to the screen, without worrying about a graphical environment, suchas the ones in Windows or on the Macintosh.</P><P>Your compiler may have its own built-in text editor, or you may be using a commercialtext editor or word processor that can produce text files. The important thing isthat whatever you write your program in, it must save simple, plain-text files, withno word processing commands embedded in the text. Examples of safe editors includeWindows Notepad, the DOS Edit command, Brief, Epsilon, EMACS, and vi. Many commercialword processors, such as WordPerfect, Word, and dozens of others, also offer a methodfor saving simple text files.</P><P>The files you create with your editor are called source files, and for C++ theytypically are named with the extension <TT>.CPP</TT>, <TT>.CP</TT>, or <TT>.C</TT>.In this book, we'll name all the source code files with the <TT>.CPP</TT> extension,but check your compiler for what it needs.<BLOCKQUOTE>	<P><HR><FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>Most C++ compilers don't care what	extension you give your source code, but if you don't specify otherwise, many will	use <TT>.CPP</TT> by default.</P>	<P>DO use a simple text editor to create your source code, or use the built-in editor	that comes with your compiler. DON'T use a word processor that saves special formatting	characters. If you do use a word processor, save the file as ASCII text. DO save	your files with the <TT>.C</TT>, <TT>.CP</TT>, or <TT>.CPP</TT> extension. DO check	your documentation for specifics about your compiler and linker to ensure that you	know how to compile and link your programs. <HR></BLOCKQUOTE><H3 ALIGN="CENTER"><A NAME="Heading14"></A><FONT COLOR="#000077">Compiling the SourceCode</FONT></H3><P>Although the source code in your file is somewhat cryptic, and anyone who doesn'tknow C++ will struggle to understand what it is for, it is still in what we callhuman-readable form. Your source code file is not a program, and it can't be executed,or run, as a program can.</P><P>To turn your source code into a program, you use a compiler. How you invoke yourcompiler, and how you tell it where to find your source code, will vary from compilerto compiler; check your documentation. In Borland's Turbo C++ you pick the <TT>RUN</TT>menu command or type</P><PRE><FONT COLOR="#0066FF">tc &lt;filename&gt;</FONT></PRE><P>from the command line, where <TT>&lt;</TT>filename<TT>&gt;</TT> is the name ofyour source code file (for example, <TT>test.cpp</TT>). Other compilers may do thingsslightly differently.<BLOCKQUOTE>	<P><HR><FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>If you compile the source code from	the operating system's command line, you should type the following:	<BLOCKQUOTE>		<P>For the Borland C++ compiler: <FONT COLOR="#0066FF"><TT>bcc</TT> &lt;filename&gt;</FONT><BR>		<BR>		For the Borland C++ for Windows compiler: <FONT COLOR="#0066FF"><TT>bcc</TT> &lt;filename&gt;		</FONT><BR>		<BR>		For the Borland Turbo C++ <FONT COLOR="#0066FF">compiler: <TT>tc</TT> &lt;filename&gt;		</FONT><BR>		<BR>		For the Microsoft compilers: <FONT COLOR="#0066FF"><TT>cl</TT> &lt;filename&gt;</FONT>	</BLOCKQUOTE>	<P><HR></BLOCKQUOTE><P>After your source code is compiled, an object file is produced. This file is oftennamed with the extension <TT>.OBJ</TT>. This is still not an executable program,however. To turn this into an executable program, you must run your linker.<H3 ALIGN="CENTER"><A NAME="Heading15"></A><FONT COLOR="#000077">Creating an ExecutableFile with the Linker</FONT></H3><H3 ALIGN="CENTER"><FONT COLOR="#000077"></FONT></H3><P>C++ programs are typically created by linking together one or more OBJ files withone or more libraries. A library is a collection of linkable files that were suppliedwith your compiler, that you purchased separately, or that you created and compiled.All C++ compilers come with a library of useful functions (or procedures) and classesthat you can include in your program. A function is a block of code that performsa service, such as adding two numbers or printing to the screen. A class is a collectionof data and related functions; we'll be talking about classes a lot, starting onDay 5, &quot;Functions.&quot;</P><P>The steps to create an executable file are<DL>	<DD><B>1.</B> Create a source code file, with a <TT>.CPP</TT> extension.<BR>	<B><BR>	2.</B> Compile the source code into a file with the <TT>.OBJ</TT> extension.<BR>	<B><BR>	3.</B> Link your OBJ file with any needed libraries to produce an executable program.</DL><H3 ALIGN="CENTER"><A NAME="Heading17"></A><FONT COLOR="#000077">The Development

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -