21天学会c

上传人:微*** 文档编号:171701438 上传时间:2022-11-28 格式:DOCX 页数:117 大小:248.87KB
收藏 版权申诉 举报 下载
21天学会c_第1页
第1页 / 共117页
21天学会c_第2页
第2页 / 共117页
21天学会c_第3页
第3页 / 共117页
资源描述:

《21天学会c》由会员分享,可在线阅读,更多相关《21天学会c(117页珍藏版)》请在装配图网上搜索。

1、1st Day Day 1 Getting Si urt(?(i Introduction A Brief History of C+ Programs Solving Problems Procedural, Structured, and Object-OrientedProgramming C+ and Object-Oriented Programming How C+ Evolvedo The ANSI Standard Should I Learn C First? Preparing to Program Your Development Environmento Compili

2、ng the Source Code Creating an Executable File with the Linker The Development Cycle Figure 1.1.o HELLO. CPPYour First C+ Program Listing 1.1. HELLO. CPP, the Hello World program.o Compile Errors Listing 1.2. Demonstration ofo compiler error. Summary Q&A Workshop Quiz ExercisesDay 1Getting StartedIn

3、troductionWelcome to Teach Yourself C+ in 21 Days! Today you will get started on your way to becoming a proficient C+ programmer. You11 learn Why C+ is the emerging standard in software development. The steps to develop a C+ program. How to enter, compile, and link your first working C+ program.A Br

4、ief History of C+Computer languages have undergone dramatic evolution since the first electronic computers were built to assist in telemetry calculations during World War II. Early on, programmers worked with the most primitive computer instructions: machine language. These instructions were represe

5、nted by long strings of ones and zeroes. Soon, assemblers were invented to map machine instructions to human-readable and -manageable mnemonics, such as ADD and MOV.In time, higher-level languages evolved, such as BASIC and COBOL. These languages let people work with something approximating words an

6、d sentences, such as Let I =100. These instructions were translated back into machine language by interpreters and compilers. An interpreter translates a program as it reads it, turning the program instructions, or code, directly into actions. A compiler translates the code into an intermediary form

7、. This step is called compiling, and produces an object file. The compiler then invokes a linker, which turns the object file into an executable program.Because interpreters read the code as it is written and execute the code on the spot, interpreters are easy for the programmer to work with. Compil

8、ers, however, introduce the extra steps of compiling and linking the code, which is inconvenient. Compilers produce a program that is very fast each time it is run. However, the time-consuming task of translating the source code into machine language has already been accomplished.Another advantage o

9、f many compiled languages like C+ is that you can distribute the executable program to people who dont have the compiler. With an interpretive language, you must have the language to run the program.For many years, the principle goal of computer programmers was to write short pieces of code that wou

10、ld execute quickly. The program needed to be small, because memory was expensive, and it needed to be fast, because processing power was also expensive. As computers have become smaller, cheaper, and faster, and as the cost of memory has fallen, these priorities have changed. Today the cost of a pro

11、grammer,s time far outweighs the cost of most of the computers in use by businesses. Well-written,easy-to-maintain code is at a premium. Easy- to-maintain means that as business requirements change, the program can be extended and enhanced without great expense.ProgramsThe word program is used in tw

12、o ways: to describe individual instructions, or source code, created by the programmer, and to describe an entire piece of executable software. This distinction can cause enormous confusion, so we will try to distinguish between the source code on one hand, and the executable on the other.New Term:

13、A program can be defined as either a set of written instructions created by a programmer or an executable piece of software.Source code can be turned into an executable program in two ways: Interpreters translate the source code into computer instructions, and the computer acts on those instructions

14、 immediately. Alternatively, compilers translate source code into a program, which you can run at a later time. While interpreters are easier to work with, most serious programming is done with compilers because compiled code runs much faster. C+ is a compiled language.Solving ProblemsThe problems p

15、rogrammers are asked to solve have been changing. rFwenty years ago, programs were created to manage large amounts of raw data. The people writing the code and the people using the program were all computer professionals. Today, computers are in use by far more people, and most know very little abou

16、t how computers and programs work. Computers are tools used by people who are more interested in solving their business problems than struggling with the computer.Ironically, in order to become easier to use for this new audience, programs have become far more sophisticated. Gone are the days when u

17、sers typed in cryptic commands at esoteric prompts, only to see a stream of raw data. Todays programs use sophisticated user-friendly interfaces, involving multiple windows, menus, dialog boxes, and the myriad of metaphors with which weve all become familiar. The programs written to support this new

18、 approach are far more complex than those written just ten years ago.As programming requirements have changed, both languages and the techniques used for writing programs have evolved. While the complete history is fascinating, this book will focus on the transformation from procedural programming t

19、o object-oriented programming.Procedural, Structured, and Object-Oriented ProgrammingUntil recently, programs were thought of as a series of procedures that acted upon data. A procedure, or function, is a set of specific instructions executed one after the other. The data was quite separate from the

20、 procedures, and the trick in programming was to keep track of which functions called which other functions, and what data was changed. To make sense of this potentially confusing situation, structured programming was created.The principle idea behind structured programming is as simple as the idea

21、of divide and conquer. A computer program can be thought of as consisting of a set of tasks. Any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily underst

22、ood.As an example, computing the average salary of every employee of a company is a rather complex task. You can, however, break it down into these subtasks:1. Find out what each person earns.2. Count how many people you have.3. Total all the salaries.4. Divide the total by the number of people you

23、have.Totaling the salaries can be broken down into1. Get each employee,s record.2. Access the salary.3. Add the salary to the running total.4. Get the next employee* s record.In turn, obtaining each employee* s record can be broken down into1. Open the file of employees.2. Go to the correct record.3

24、. Read the data from disk.Structured programming remains an enormously successful approach for dealing with complex problems. By the late 1980s, however, some of the deficiencies of structured programing had became all too clear.First, it is natural to think of your data (employee records, for examp

25、le) and what you can do with your data (sort, edit, and so on) as related ideas.Second, programmers found themselves constantly reinventing new solutions to old problems. This is often called reinventing the wheel, and is the opposite of reusability. The idea behind reusability is to build component

26、s that have known properties, and then to be able to plug them into your program as you need them. This is modeled after the hardware world-when an engineer needs a new transistor, she doesn, t usually invent one, she goes to the big bin of transistors and finds one that works the way she needs it t

27、o, or perhaps modifies it. There was no similar option for a software engineer.New Term: The way we are now using computerswith menus and buttons and windows-fosters a more interactive, event-driven approach to computer programming. Event-driven means that an event happensthe user presses a button o

28、r chooses from a menuand the program must respond. Programs are becoming increasingly interactive, and it has became important to design for that kind of functionality.Old-fashioned programs forced the user to proceed step-by-step through a series of screens. Modern event-driven programs present all

29、 the choices at once and respond to the users actions.Object-oriented programming attempts to respond to these needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.The essence of object-orient

30、ed programming is to treat data and the procedures that act upon the data as a single object”a self-contained entity with an identity and certain characteristics of its own.C+ and Object-Oriented ProgrammingC+ fully supports object-oriented programming, including the four pillars of object-oriented

31、development: encapsulation, data hiding, inheritance, and polymorphism. Encapsulation and Data Hiding When an engineer needs to add a resistor to the device she is creating, she doesnt typically build a new one from scratch. She walks over to a bin of resistors, examines the colored bands that indic

32、ate the properties, and picks the one she needs. The resistor is a black box as far as the engineer is concernedshe doesnt much care how it does its work as long as it conforms to her specifications; she doesnt need to look inside the box to use it in her design.The property of being a self-containe

33、d unit is called encapsulation. With encapsulation, we can accomplish data hiding. Data hiding is the highly valued characteristic that an 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, you c

34、an use a well-designed object without knowing about its internal data members.Similarly, when the engineer uses the resistor, she need not know anything about the internal state of the resistor. All the properties of the resistor are encapsulated in the resistor object; they are not spread out throu

35、gh the circuitry. It is not necessary to understand how the resistor works in order to use it effectively. Its data is hidden inside the resistors casing.C+ supports the properties of encapsulation and data hiding through the creation of user-defined types, called classes. You11 see how to create cl

36、asses on Day 6,“Basic Classes. Once created, a well-defined class acts as a fully encapsulated entityit is used as a whole unit. The actual inner workings of the class should be 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. Inher

37、itance and Reuse When the engineers at Acme 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 theyd like to add a turbocharger and a six-speed transmission. The chief engineer woul

38、d prefer not to start from the ground up, but rather to say,Lets build another Star, but lets add these additional capabilities. We11 call the new model a Quasar. A Quasar is a kind of Star, but one with new features.C+ supports the idea of reuse through inheritance. A new type, which is an extensio

39、n of an existing type, can be declared. This new subclass is said to derive from the existing type and is sometimes called a derived type. The Quasar is derived from the Star and thus inherits all its qualities, but can add to them as needed. Inheritance and its application in C+ are discussed on Da

40、y 12,Inheritance, and Day 15,Advanced Inheritance. Polymorphism The new Quasar might respond differently than a Star does when you press down on the accelerator. The Quasar might engage fuel injection and a turbocharger, while the Star would simply let gasoline into its carburetor. A user, however,

41、does not have to know about these differences. He can just floor it, and the right thing will happen, depending on which car hes driving.C+ supports the idea that different objects do the right thing through what is called function polymorphism and class polymorphism. Poly means many, and morph mean

42、s form. Polymorphism refers to the same name taking many forms, and is discussed on Day 10,Advanced Functions, and Day 13,Polymorphism.How C+ EvolvedAs object-oriented analysis, design, and programming began to catch on, Bjarne Stroustrup took the most popular language for commercial software develo

43、pment, 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 handful of developers at AT&T to being the programming language of choice for an estimated one million developers worldwi

44、de. It is expected that by the end of the decade, C+ will be the predominant language for commercial software development.While it is true that C+ is a superset of C, and that virtually any legal C program is a legal C+ program, the leap from C to C+ is very significant. C+ benefited from its relati

45、onship to C for many years, as C programmers could ease into their use of C+. To really get the full benefit of C+, however, many programmers found they had to unlearn much of what they knew and learn a whole new way of conceptualizing and solving programming problems.The ANSI StandardThe Accredited

46、 Standards Committee, operating under the procedures of the American National Standards Institute (ANSI), is working to create an international standard for C+.The draft of this standard has been published, and a link is available at www. libertyassociates, com.The ANSI standard is an attempt to ens

47、ure that C+ is portablethat code you write for Microsoft* s compiler will compile without errors, using a compiler from any other vendor. Further, because the code in this book is ANSI compliant, it should compile without errors on a Mac, a Windows box, or an Alpha.For most students of C+, the ANSI

48、standard will be invisible. The standard has been 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 ANSI compliant.Should I Learn C First?The question inevitably arises: ASince C+ is a superse

49、t of C, should I learn C irst? Stroustrup and most other C+ programmers agree. Not only is it unnecessary to learn C first, it may be advantageous not to do so. This book attempts to meet the needs of people like you, who come to C+ without prior experience of C. In fact, this book assumes no progra

50、mming experience of any kind.Preparing to ProgramC+, perhaps more than other languages, demands that the programmer design the program before writing it. Trivial problems, such as the ones discussed in the first few chapters of this book, dont require much design.Complex problems, however, such as t

51、he ones professional programmers are challenged with every day, do require design, and the more thorough the design, the more likely it is that the program will solve the problems it is designed to solve, on time and on budget. A good design also makes for a program that is relatively bug-free and e

52、asy to maintain. It has been estimated that fully 90 percent of the cost of software is the combined cost of 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.The first question you need to ask wh

53、en preparing to design any program is,“What is the problem Im trying to solve? Every program should have a clear, well-articulated goal, and you11 find that even the simplest programs in this book do so.The second question every good programmer asks is,Can this be accomplished without resorting to w

54、riting custom software? Reusing an old program, using pen and paper, or buying software off the shelf is often a better solution to a problem than writing something new. The programmer who can offer these alternatives will never suffer from lack of work; finding less-expensive solutions to todays pr

55、oblems will always generate new opportunities later.Assuming you understand the problem, and it requires writing a new program, you are ready to begin your design.Your Development EnvironmentThis book makes the assumption that your computer has a mode in which you can write directly to the screen, w

56、ithout worrying about a graphical environment, such as the ones in Windows or on the Macintosh.Your compiler may have its own built-in text editor, or you may be using a commercial text editor or word processor that can produce text files. The important thing is that whatever you write your program

57、in, it must save simple, plain-text files, with no word processing commands embedded in the text. Examples of safe editors include Windows Notepad, the DOS Edit command, Brief, Epsilon, EMACS, and vi. Many commercial word processors, such as WordPerfect, Word, and dozens of others, also offer a meth

58、od for saving simple text files.The files you create with your editor are called source files, and for C+ they typically are named with the extension . CPP,. CP, or . C. In this book, we11 name all the source code files with the . CPP extension, but check your compiler for what it needs.NOTE: Most C

59、+ compilers dont care what extension you give your source code, but if you dont specify otherwise, many will use . CPP by default.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

60、 characters. If you do use a word processor, save the file as ASCII text. DO save your files with the . C,. CP, or . CPP extension. DO check your documentation for specifics about your compiler and linker to ensure that you know how to compile and link your programs.Compiling the Source CodeAlthough

61、 the source code in your file is somewhat cryptic, and anyone who doesnt know C+ will struggle to understand what it is for, it is still in what we call human-readab 1 e form. Your source code file is not a program, and it cant be executed, or run, as a program can.To turn your source code into a pr

62、ogram, you use a compiler. How you invoke your compiler, and how you tell it where to find your source code, will vary from compiler to compiler; check your documentation. In Borlands Turbo C+ you pick the RUN menu command or typetc filenamefrom the command line, where is the name of your source cod

63、e file (for example, test, cpp). Other compilers may do things slightly differently.NOTE: If you compile the source code from the operating systems command line, you should type the following:For the Borland C+ compiler: bcc For the Borland C+ for Windows compiler: bcc For the Borland Turbo C+ compi

64、ler: tc For the Microsoft compilers: cl After your source code is compiled, an object file is produced. This file is often named with the extension . OBJ. This is still not an executable program, however. To turn this into an executable program, you must run your linker.Creating an Executable File w

65、ith the LinkerC+ programs are typically created by linking together one or more OBJ files with one or more libraries. A library is a collection of linkable files that were supplied with 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 classes that you can include in your p

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!