Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Chapter 1 The C++ Language Objectives Describe various types of programs Describe the role of the operating system Describe how a computer gets instructions Describe the role of the programming language, high- and low-level languages, interpreters, and compilers Explain the structure of a C++ program What is a computer program? Computers are just machines and must follow directions. Directions are grouped into logical sequences to create programs. Programs that perform specific tasks such as word processing are called application programs. When a computer is first turned on, it must follow the instructions embedded in ROM. Operating Systems The operating system takes charge of fundamental system operations including: Managing hardware Maintaining file systems Controlling input and output Loading and executing programs The Computer’s Language The microprocessor is the device that actually processes the instructions given to the computer. Each microprocessor has its own instruction set that it can understand. The instructions provided to the microprocessor must be in machine language. Machine language is a combination of circuits that can be represented with the binary number system. Introduction to Programming Languages Programming languages can be understood by people and computers. Each language has its own set of rules and vocabulary. Some are very much like English, while others are not. Assembly Language Uses numbers and letters to represent machine language instructions. An assembler is needed to change the programmer’s code into machine language. Examples of statements might look like the following: PUSH BP MOV BP, SP MOV SI, 0002 Low Level Languages It is necessary for the programmer to know the instruction set of the microprocessor. Programs are specific to a microprocessor. Low level programs take up less memory. You can do anything that the hardware will allow. High-level Languages One command may represent many microprocessor instructions. You can write programs more quickly. Instructions are usually more easy to read. Easier to move between computers. Examples of Languages High-level C++ Java Basic Pascal Visual Basic Low-level Machine language Assembly language Interpreters Translate the source code of a high level language into machine language code known as object code. Each instruction is interpreted as it is needed. The interpreter must be loaded into memory first. Object code is not saved. Compilers Also translate high level source into object code. Makes the translation once and saves an object file. The object file is combined with other machine language by the linker. The linker generates the executable file. C++ Program Structure C++ has some major elements: Comments Compiler directives Main function Braces Statements Comments Comments are ignored by the compiler. They are used to explain the purpose of the program, explain the source code, identify the parts of the program or store the names of the programmers. Comments begin with a double forward slash (//). Compiler Directives Instructions to the compiler, not really part of the C++ language. A common directive is the #include directive which inserts other prewritten code into your program. Includes are usually used to add additional functionality such as input/output to your program. Main function A function is a block of code that carries out a specific task. Programs are usually divided into many functions, but the main function always runs first. Functions are typically called from other parts of the program and they might return a value. Braces Braces are used to mark the beginning and end of a block of code. Aligning the indention of opening and closing braces can make it easier to quickly identify blocks of code. Statements Statements are instructions that make the program work. Each statement must end with a semicolon. Directives and function declarations do not end with semicolons. C++ is case sensitive so capitalization in statements does matter. From source code to finished product Type source code into text editor. Compile the program into object code. Link the object code with other machine language to generate an executable file. The executable file can be run. Summary Computers follow instructions called programs. Application programs perform specific tasks. Operating systems manage fundamental computer activities. The microprocessor processes each instruction. Summary An interpreter or compiler must translate high level languages into machine language. Experience will teach you what language is appropriate for a specific task. Comments are ignored by the compiler. Directives are commands for the compiler. Summary All C++ programs have a main function. The main function is where the program begins running. Braces mark the beginning and end of blocks of code. Statements are the lines of code the computer executes. Each statement ends with a semicolon. C++ is case sensitive and it ignores extra space.