Software Solution for Engineers and Scientist Episode 6 pdf

90 337 0
Software Solution for Engineers and Scientist Episode 6 pdf

Đ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

oriented facilities. The enhancement ended up including classes, virtual functions, a single line comment style, operator overloading, improved type-checking, multi - ple inheritance, template functions, and some degree of exception handling. The C++ programming was standardized in 1998 as ISO/IEC 14882:1998. The current ver - sion dates from 2003. During its evolution many libraries have been added to the C++ language. One of the first ones was (named iostream) provided much needed stream-level input and output functions. The new operators named cin() and cout() replaced the more awkward C functions named printf() and scanf(). 16.0.2 Advantages of the C++ Language The following are the most often cited advantages of C language: 1. C++ is not a specialized programming language, therefore, it is suitable for develop - ing a wide range of applications, from major system program to minor utilities. 2. Although C++ is a relatively small language it contains all the necessary operators, data types, and control structures to make it generally useful. 3. The language includes an abundant collection of library functions for dealing with input/output, data and storage manipulations, system interface, and other primi- tives not directly implemented in the language. 4. C++ data types and operators closely match the characteristics of the computer hardware. This makes C++ programs efficient as well as easy to interface with as- sembly language programs. 5. The C++ language is not tied to any particular environment or operating system. The language is available on machines that range from microcomputers to mainframes. For this reason, C++ programs are portable, that is, they are relatively easy to adapt to other computer systems. 6. Object orientation is optional in C++. Applications that do not require object orien - tation can turn-off this feature. 16.0.3 Disadvantages of the C++ Language The following are the most often noted disadvantages of C++: 1. Because C++ is not a language of very high level, it is not the easiest to learn. Begin - ners find that some constructions in C++ are complicated and difficult to grasp. 2. The rules of C++ language are not very strict and the compiler often permits consid - erable variations in the coding. This allows some laxity in style which often leads to incorrect or inelegant programming habits. 3. Instead of being line C, a small and compact language, C++ has become large and complex. 4. C++ library functions are devised to operate on a specific machine. This sometimes complicates the conversion of C++ software to other implementations or systems. 5. The insecurities of C were not resolved in C++. 424 Chapter 16 16.1 PC Implementations of C and C++ Several software companies have developed C and C++ compilers for the PC. Some of these products have gained and lost popular favor as other new versions or implemen - tations were introduced to the market. Historically some of the better known imple - mentations of C for the PC were the Microsoft C and Quick C compilers, IBM C 2 compiler (which is a version of Microsoft C licensed to IBM), Intel iC 86 and iC 286 compilers, Borland Turbo C, Turbo C Professional, Lattice C, Aztec C, Zortech C, and Metaware High C. Currently C++ development systems have almost completely replaced the old C compilers. The most popular ones are the Microsoft versions furnished as part of Developer’s Studio usually referred to as Visual C++. The last version of Visual C++ that did not include .NET support was version 6. After version 6, the .NET suffix was added to C++ compiler versions, thus Visual C++ .NET in Developer’s Studio Ver - sion 7, Visual C++ .NET 2003 and Visual C++ .NET 2005. However, in the 2005 ver - sion the .NET designation was removed and there is a Visual C++ 2005 version. Microsoft’s explanation for the name change is that the .NET designation had led user’s to believe that the 7.0 and 7.1 compilers were intended exclusively for .NET development, which is not the case. Borland Corporation had developed and marketed several C++ systems for the PC over the years. The current version is Borland C++ Builder 2006 for Microsoft Windows. Two variations are available in the Borland product: the Professional and the Enterprise editions. 16.2 Flowcharts and Software Design A set of logical instructions designed to perform a specific task is called a program. The document containing a set of instructions for starting a computer system could be described as a power up program for a human operator. By the same token, a computer program is a set of logical instructions that will make the computer perform a certain and specific task. Note that in both cases the concept of a program requires a set of in - structions that follow a logical pattern and a predictable result. A set of haphazard in - structions that lead to no predictable end can hardly be considered a program. Programmers have devised logical aids that help them make certain that com - puter programs follow an invariable sequence of options and their associated ac - tions. One of the most useful of these aids is called a flowchart. A flowchart is a graphical representation of the options and actions that form a program. Flowcharts use graphic symbols to enclose different types of program operations. The most common ones are: 1. Rectangle. Used to represent processing operations such as arithmetic calculations and data manipulations. 2. Parallelogram. Represents input and output functions, such as keyboard input, dis - play, and printing operations. 3. Diamond. Indicates a decision or logical comparison whose result is a yes or no. The C++ Language on the PC 425 4. Circle. Used to represent a program termination point such as the end of execution or an error exit. 5. Flow lines. Are used to connect other flowchart symbols indicating the direction of program flow. The use of a flowchart is best illustrated with an example. Figure 16.1 is a flowchart of the program for turning on a computer system in which the compo - nents can be connected to the power line in three possible ways: Figure 16.1 A Machine Start-Up Flowchart 1. All computer components are directly connected to individual wall outlets. 2. All computer components are connected to a power strip and the power strip is con - nected to the wall outlet. 3. Some components are connected to a power strip and some are connected directly to the wall outlets. Note in the flowchart of Figure 16.1 that the diamond flowchart symbols are used to represent program decisions. These decisions correspond to the princi - ples of Aristotelian logic, therefore, there must be two and not more than two an - swers to the question. These possible answers are usually labeled YES and NO in 426 Chapter 16 START END YES NO NO YES Are all components wired individually ? Are all components wired to power strip ? Turn-on individual component switches Turn-on power strip and individual component switches Turn-on power strip switch the flowchart. Decisions are the crucial points in the program’s logic. A program that requires no decisions or comparisons consists of such simple logic that a flowchart would be trivial and unnecessary. For instance, a flowchart that consists of three processing steps: start, solve problem, and end, is logically meaningless. Computing machines of the present generation are not equipped with humanlike intelligence. Therefore some assumptions that are obvious when dealing with hu - man beings are invalid regarding computers. Computer programs can leave no loose ends and make no assumptions of reasonable behavior. You cannot tell a computer “well, you know what I mean.” The programmer uses flowcharts to make certain that each processing step is clearly specified. Regarding the flowchart graphics it should be noted that arrowheads are optional in flowcharts if execution proceeds from the top down, as in Figure 16.1. However, if execution flows up, to a higher flowchart level, then the connecting lines should have arrowheads indicating the direction of program flow. 16.3 The C++ Console Application Microsoft Visual C++ provides a text-based program type that is useful in developing C++ programs that do not require a graphical user interface. For example, an engineer requires a small program to perform a set of numerical calculations. If the application is not to be distributed to other users and does not require graphics output, then the console application provides a simple way of developing a C++ program. To create a console application you must first start Visual C++ in Developer’s Stu- dio. Once the development environment is displayed, select the New command in the File menu. This command displays the selection box shown in Figure 16.2. Figure 16.2 Visual C++ New File Command The C++ Language on the PC 427 Once the New File box is displayed you must make sure that the Project tab is active and select the Win32 Console Application option towards the bottom of the Project Screen. Next you enter the name of your project and its location in the ma - chine’s file system. In this example we have named our project demo1. The button to the right of the input box labeled “location:” allows browsing the machine’s file system. Once you click the OK button, the following screen contains several radio but - tons which allow selecting between an empty project, a simple application, a “Hello, World” application, or an application that support MFC (Microsoft Foun - dation Classes). In this example we will select “A Simple Application” as shown in Figure 16.3. Figure 16.3 Selecting “A Simple Application” Option Clicking the button labeled “Finish” produces a notification screen that informs the user that a skeleton project has been created, its name, and the pres - ence of two precompiled headers named “Stdafx.h” and “StdAfx.cpp.” Figure 16.4 is a screen capture of this notification. When you press the OK button execution returns to the main Visual Studio screen. This screen is divided into three panes. The one on the right is the editor. The one at the bottom shows development operations and is called the Build pane. The pane at the left, called the Workspace pane shows the program compo - nents. The Workspace pane now shows two buttons on its bottom margin. One, la - beled Class View, is currently selected. This mode of the Workspace pane is used in object-oriented development. At this time you should click the button labeled File View and then the plus sign (+) control to the left of the project files entry. 428 Chapter 16 This action will display the source, header, and resource files currently in the pro - ject. Also a ReamMe.txt file that contains information about the components of the console application that you have just created. Figure 16.4 Visual C++ New Project Information Screen If you now expand the Source Files entry by clicking on its plus sign button a list of two text files will be displayed. The first one, named demo.cpp (assuming that you named your program “demo”) will be the source file created by Visual Studio. You can use this file as a start point for your program. The file named StdAfc.cpp contains a single statement to include stdafx.h with your source. You can view stdafx.h by opening the Header Files group and double clicking the filename. Figure 16.5, on the following page, shows the Visual Studio screen at this point. You should now edit the source file to suit your own programming style and start coding the console application. The Build menu contains command to build the pro - gram (named Rebuild All) and to execute the code within the development environ - ment. For example, the following code listing shows a modification of the simple application file into a Hello, World program. The C++ Language on the PC 429 Figure 16.5 Visual C++ Screen for a New Project // demo.cpp // First demonstration program for a console application // Coded by: Julio Sanchez // Date: March 21, 2007 // #include “stdafx.h” #Include <iostream.h> int main(){ cout < “\nHello World\n\n”; return 0; } Notice that we have eliminated the parameters passed to the main() function since our program does not use them. Also that we have added an include line for the iostream library. This will allow us to use the powerful input and output func - tions in this library (cin() and cout()). When this program executes, a Hello, World screen is displayed. 430 Chapter 16 Chapter 17 Event-Driven Programming Chapter Summary This chapter deals with the principles of Windows programming. In it we explain the differences between a DOS and a Windows program and provide a model, called event-driven programming, that is suitable for Windows applications. We also de- scribe the fundamental components of a Windows program, its file structure, and vi- sual features. 17.0 Graphical Operating Systems The operating systems used in the first generation of digital computers required that the user toggle a series of binary switches, each one with an attached light, in order to enter data into the machine. In these systems, the bootstrap sequence consisted of a predefined sequence of switch-toggling and button-pressing actions which the opera - tor memorized and executed in a ritual-like fashion. To handle one of these machines you had to belong to a select class of binary-speaking, hexadecimal-minded experts who devoted most of their life to deciphering the arcane mysteries of hardware and software. In the years that followed, several inventions and adaptations simplified com - puter input and output. IBM Corporation, that had developed punched cards for its business machines, adapted this technology to its first line of computers. Teletype machines (called TTYs) had been invented for use in the communications industry but were soon adapted for use as computer devices. With the TTY, the typewriter keyboard found its way into computing. Next came the use of Cathode Ray Tubes, from commercial television, as a way of displaying text without having to print it on paper. An added bonus was that the CRT could also be used to display pictures. Other devices followed suit. Some have become standard elements of the tech - nology while others have disappeared. They include the lightpen, the touch screen, the graphic tablet, the joystick, and the mouse. The common element in all of these 431 devices is that they allow the user to visually interact with the machine. The idea for an interactive input device came from the work of Allan Kay at the Xerox Palo Alto Research Center in the early 1970s. Dr. Kay was attempting to design a com - puter that could be used by preschool children, who were too young to read or to type commands in text form. A possible approach was to use small screen objects, called icons, that represented a familiar thing. A mechanical device (the mouse) allowed moving these graphics objects on the screen in order to interact with the system. Interactive graphics and the graphical user interface were the result. Steve Wozniak, one of the founders of Apple computers, relates that he visited Xerox PARC and saw the mouse and the graphical user interface. He immediately concluded that the interactive way of controlling a computer was the way of the future. Back at Apple, Wozniak started the development of an operating system that supported graphical, mouse-controlled, icon-based, user interaction. After one or two unsuccessful tries, the Macintosh computer was released. Soon there - after, Microsoft delivered a graphical operating system for the PC, called Win - dows. 17.1 Enter Windows Many realized the advantages of a more reasonable and physical interaction with a computing machine. However, it took many years for the actual implementation of this idea in an effective operating system that would be preferred by the majority of users. Douglas Engelbart demonstrated a viable mouse interface in 1968, but it took fifteen years for the mouse to become a standard computer component. In order to implement a graphical user interface (GUI) it was necessary to have not only a pointing device that worked but also a graphics-capable video terminal. In addition, the software would have to provide a set of graphics services in a device indepen- dent manner. In the PC world, the evolution of hardware and software components into a graphical operating system took approximately one decade. The first versions were rather crude and achieved little popular acceptance. It was not until Win - dows 95 that Microsoft’s graphical operating system for the PC became the stan - dard. Originally, the development of a graphical operating system for the PC was a joint effort between Microsoft and IBM. But soon, the two companies had major strategic and tactical differences. IBM’s efforts resulted in a product called Oper - ating System/2, or OS/2. It seemed that Windows and OS/2 would share the PC op - erating system market. However, for reasons unrelated to its technical merits, OS/2 quickly lost ground to Windows. OS/2 Warp version 4.0 was on the market until 2005 but is no longer sold by IBM. 17.1.1 Text-Based and Graphical Programs To a programmer, DOS and Windows are in different worlds. DOS programs have complete and unrestricted access to the machine’s hardware. Once the code gains control it can do whatever it pleases. Its only limitations are the hardware capabili - 432 Chapter 17 ties and the programmer’s skills. Although the designers of DOS-like operating sys - tems often list rules that “well behaved programs” should follow, there is no way of enforcing these rules. Intentionally or by error a DOS program can raise havoc with the system by deleting files from storage and even attempting to physically destroy a hardware device. Each DOS application has total control over all system resources. It can allocate all memory to itself, set whatever video mode is convenient, control the printer and the communications lines, and manage the mouse and the keyboard. Re - sources need not be shared since the operating system is dormant while an application is in the foreground. In this environment, more than one application rarely execute si - multaneously. Windows applications must share resources between themselves and with the op - erating system. Memory, CPU, display hardware, communications lines and devices, mouse, keyboard, and disk storage are all shared. Each program operates in its pri - vate address space and has limited access to other memory areas. Code cannot ac - cess the hardware devices directly but must to do so through operating system services called the Application Program Interface (API). This mechanism ensures that all programs are well behaved. Windows and DOS graphics programming are quite different. Because all re- source are shared, Windows must control access to all devices and resources, in- cluding memory, the video system, communications devices, and input and output components. Figure 17.1 shows how DOS and Windows programs access system re- sources. Figure 17.1 DOS and Windows Applications Access to System Resources Event-Driven Programming 433 memory memory disk storage disk storage video system video system I/O devices I/O devices DOS application Windows application Windows API services [...]... such as the keyboard and the mouse The software routine that checks for action on the event generating devices is sometimes called the event loop The Event Handler In the event-driven model, the application is the event handler Its function is to wait for an event to occur and then perform the corresponding action The event handler does not monitor event-generating devices directly, and often does not... true (f stands for flag) 8-bit unsigned integer single ASCII character count of x values and count of y values IEEE double precision real 32-bit integer 32-bit unsigned integer handle handle to a Window 16- bit integer value 32-bit unsigned integer value 32-bit far pointer 16- bit unsigned integer value any pointer, usually far 32-bit coordinates, labeled x and y, packed in a structure red, green, and blue... uses this same handle to identify a particular window to your program If your application has several windows open simultaneously, it can tell to which of them the message refers, by examining the handle DOS programmers first learn about handles in relation to disk files In DOS, there is little use for handles outside of disk files and standard devices Windows handles, on the other hand, refer to many... programmer’s name and other personal information • Name of the source file • List of related sources, program modules, and support files, including libraries, DLLs, and required add-ons • Description of the development system and resources • Program hardware and software requirements The Window Program Components 457 • Program update chronology listing dates and modifications • Program test history and bug fixes... command line version of the Resource Compiler is named RC.EXE; in Borland C++ it is named BRCC.EXE or BRCC32.EXE Before Windows 95, the Resource Compiler also had the task of adding the binary resources to the executable file created by the linker The Microsoft linker for Windows 95 and later versions can directly manipulate files in both OBJ and RES formats 17.3.4 Make Files During the evolution of software. .. Ward and Mellor and also Hatley and Pribhai have introduced variations of the data flow diagrams which are better suited for representing systems in which events take place in real time In Figure 17.3 we have used symbols from conventional data flow diagrams and from the Ward and Mellor extensions in order to model an event chain More elaborate and detailed representations could include symbols for. .. Components 451 specially when referring to Windows data types and structures In our own variables we use it rather loosely, and only for the sake of simplicity and uniformity Table 18.1 Standard Prefixes Recommended by Microsoft PREFIX DATA TYPE INTERPRETATION b/f BOOL by ch cx/cy d dw h hwnd i l Lp/np n p pt BYTE CHAR INT DOUBLE DWORD HANDLE HWND INT LONG FAR* SHORT pointer POINT rgb RGB x/y s sz... Windows platform the options and controls are so intricate that most programmers would not consider entering them manually All major Windows C and C++ development systems for the PC have a make utility In Microsoft’s Visual C++, it is named NMAKE.EXE In the Borland system, it is MAKE.EXE The text file for either system has the extension MAK Make files consist of instructions in ASCII text format They... before the previous one has concluded On the other hand, Windows messages are queued and must wait in line until their turn comes for processing DOS programmers have to change their mindset when working in Windows Figure 17.2 shows two models of programming: the sequential model in a DOS program and the event-driven model in a Windows programs In DOS, the application receives control at load time and. .. to make the format compatible with DOS and with 16- bit versions of Windows The compilers in Windows development systems have many different switches, options, and modes of operation selectable by the user In Visual C++, the compiler options are selected through the Settings command of the Build menu The Project Settings dialog box contains a C/C++ tab which activates the compiler switches and controls, . in C++. 424 Chapter 16 16. 1 PC Implementations of C and C++ Several software companies have developed C and C++ compilers for the PC. Some of these products have gained and lost popular favor. exclusively for .NET development, which is not the case. Borland Corporation had developed and marketed several C++ systems for the PC over the years. The current version is Borland C++ Builder 20 06 for. imple - mentations of C for the PC were the Microsoft C and Quick C compilers, IBM C 2 compiler (which is a version of Microsoft C licensed to IBM), Intel iC 86 and iC 2 86 compilers, Borland Turbo C, Turbo

Ngày đăng: 12/08/2014, 07:22

Từ khóa liên quan

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

  • Đang cập nhật ...

Tài liệu liên quan