Professional Documents
Culture Documents
Outline
2.1
2.2 2.3 2.4 2.5 2.6
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Objectives
Execute an application.
Use escape sequences. Locate and correct syntax errors.
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2.1
Figure 2.1
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 19922004
2.1
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Solutions
Figure 2.3 Solutions can contain one or more projects.
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 19922004
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Browse button
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Open button
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
New Solution
Named HelloWorld
New Project
Named HelloWorld
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
VC++ Project
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Output Window
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
After compiling, a newly created HelloWorld project Debug directory contains the object file. The Object file contains the Object code created by compiler.
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
HelloWorld project
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Figure 2.14
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2.2 Compiling and Running the Template HelloWorld Application (Cont.) Run the application from the console window.
In addition to the application not producing any output, what is the difference?
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2.3
Figure 2.16 HelloWorld applications C++ source code in Visual Studio .NET.
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2.3
Figure 2.17 Examining comments in the HelloWorld applications C++ source code in Visual Studio .NET.
Full-line comments
Comments
Begin with two forward slashes // Improve readability Explain code
Full-line comments End-of-line comments
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2.3
Figure 2.18
Preprocessor directive
Preprocessor (i.e., Compiler Directives) Processes lines beginning with a pound sign # Processed before the source code is compiled (pass 2) <iostream> Tells Preprocessor to include console window input/ output code Forgetting to include iostream in a program that uses input or output is an error
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2.3
Figure 2.19
using directive
Specifies std (standard) namespace Accesses C++ Standard Library Advance C++ feature. Allows names to be qualified You can find this variable name in this space (std). Nice feature if name is duplicated in another library.
Analogy: Your first and last name. Typically you are only called by your first name but to remove ambiguity, if required you may be called by your first and last name.
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2.3
Figure 2.20 Function main header
return
0 indicates successful termination
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2.3
Structure of function main Declare return type (int etc.) Start with left brace { Body of the function End with right brace }
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2.4
C++ is case sensitive Using incorrect capitalization for identifiers and keywords is an error C++ applications input / output data Input Certain C++ input comes from cin (standard input stream object). cin >> myVariable; Usually tied to keyboard but can be tied to other devices Output Often output to cout (standard output stream object) cout << "HelloWorld"; Usually tied to monitor but can be tied to other devices
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2.4
Figure 2.21
Displaying a message
Executable statement
Semicolon (;)
Statement terminator Forgetting the semicolon at the end of a statement is an error
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2.4
Operators Special characters in C++ that perform operations Binary and unary
Addition operator (+), subtraction operator (-) and multiplication operator (*) are examples of C++ operators
Unary operators Operators that require only one operand such as the preincrement operator (++) Binary operators Operators such as multiplication (*) or division (/) that require two operands to perform their operation String A sequence of characters
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2.4
In Section 2.4 your textbook has you run the Welcome application using the start without debugging (shortcut ctrlF5) command. This command is no longer supported in VisualStudio 2010. While you can turn the ctrl-F5 command back ON, I will step you through some alternative solutions.
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2.2 Compiling and Running the Template HelloWorld Application (Cont.) Run the application from the console window.
Unlike running the application from the console window Visual Studio simply opens the console window runs the program and closes it again. Lets look at some solutions to this problem.
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2.2 Compiling and Running the Template HelloWorld Application (Cont.) In this solution we simply add #include<conio.h> to the include list and place getch(); just before the return statement. Include console I/O library
header file.
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2.2 Compiling and Running the Template HelloWorld Application (Cont.) Build your solution (F7) and the start debugging (F5).
Press any key to run the return statement and close the console window.
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2.2 Compiling and Running the Template HelloWorld Application (Cont.) In this solution we add a breakpoint just before the return statement. Click here to turn Breakpoint
ON and OFF
A breakpoint tells the debugger to stop simulation of the program before running this line of code. To set a breakpoint select the line and Debug > Toggle Breakpoint (F9) or simply click in the column where the breakpoint is shown.
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2.2 Compiling and Running the Template HelloWorld Application (Cont.) Start debugging (F5).
Press any key to run the return statement and close the console window.
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2.4
String literals
Compiler does not ignore whitespace characters
Escape Sequences
How you send a control character or a printable character that is not on the keyboard. Escape character is backslash \
Allows escape sequences to be read by compiler inside string literals
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2.4
Escape Sequence \n
Newline. Positions the screen cursor at the beginning of the next line. Horizontal tab. Moves the screen cursor to the next tab stop. Carriage return. Positions the screen cursor at the beginning of the current line; does not advance to the next line. Alert. Sounds the system bell. Backslash. Used to print a backslash character. Double quote. Used to print a double quote character.
\t \r \a \\ \"
Figure 2.23
Escape sequences.
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 19922004
2.4
cout << "Welcome to C++!\a\a\a"; cout << "Use \\n for a newline."; cout << "\"This is quoted text.\""; cout << "\tWelcome to C++!"; cout << "Welcome \rto C++!";
Figure 2.24
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2.4
Figure 2.25
Stream insertion operators resume printing where previous statement has stopped When breaking up lengthy statements, choose logical break points such as escape sequences, etc.
Figure 2.26 HelloWorld application output. Note missing space.
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2.4
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
HelloWorld.cpp (1 of 1)
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2.5
Debugging
Compilation Errors
Process of locating and removing errors Compilation errors Compiler detects errors in code Syntax errors Logic Errors Cause applications to produce erroneous results Can be fixed using a debugger
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2.5
Figure 2.30
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2.5
Creating syntax errors Delete n in the last escape sequence in line 10 Capitalize r in return
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
2.5
Figure 2.33
Double-click line in the output window to have marker placed next to the line with the error in the source code
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.