Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - HelloWorld Application: Introduction to.

Similar presentations


Presentation on theme: "© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - HelloWorld Application: Introduction to."— Presentation transcript:

1 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - HelloWorld Application: Introduction to C++ Programming Outline 2.1 Test-Driving the HelloWorld Application 2.2 Compiling and Running the Template HelloWorld Application 2.3 Introduction to C++ Code 2.4 Constructing the HelloWorld Application 2.5 Compilation Errors 2.6 Wrap-Up © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

2 Objectives In this tutorial, you will learn to: –Read C++ code. –Write a C++ statement that displays a message on the screen. –Compile a C++ application. –Execute an application. –Use escape sequences. –Locate and correct syntax errors.

3 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 2.1 Locating the completed HelloWorld application. 2.1Test-Driving the HelloWorld Application (Cont.)

4 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 2.2 Running the completed HelloWorld application. 2.1Test-Driving the HelloWorld Application (Cont.)

5 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 2.3 Solutions can contain one or more projects. 2.2 Compiling and Running the Template HelloWorld Application Projects −Large applications can contain many projects Solutions

6 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 2.4 New Project dialog. File > New > Project; Title Bar (displaying New Project ) Project Types: pane Location of the new project (your location may be different) Templates: pane Description of project selected in Templates: pane Default project and solution name 2.2 Compiling and Running the Template HelloWorld Application (Cont.)

7 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.2 Compiling and Running the Template HelloWorld Application (Cont.) Updated project location Updated project and solution name Browse… button

8 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.2 Compiling and Running the Template HelloWorld Application (Cont.) SimplyCpp directory (selected) Open button

9 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.2 Compiling and Running the Template HelloWorld Application (Cont.) New HelloWorld Solution Folder New HelloWorld Project Folder with C++ Source File

10 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. New Solution –Named HelloWorld New Project –Named HelloWorld Figure 2.9 Solution Explorer window in Visual Studio.NET. HelloWorld solution HelloWorld project Source Files folder 2.2 Compiling and Running the Template HelloWorld Application (Cont.)

11 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.2 Compiling and Running the HelloWorld Application (Cont.) HelloWorld.cpp file (selected) Download HelloWorld.cpp to your new HelloWorld Project Folder. Right-click HelloWorld.cpp file and select properties. Under General Tab uncheck Read-only. HelloWorld project folder

12 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.2 Compiling and Running the HelloWorld Application (Cont.) In Visual Studio Project Pane, right-click source folder and select “Add Existing Item.”

13 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 2.11 Solution Explorer window after adding HelloWorld.cpp. HelloWorld. cpp file 2.2 Compiling and Running the Template HelloWorld Application (Cont.)

14 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. HelloWorld.cpp file (selected) HelloWorld project folder Before Compiling – Review.vcxproj files –located in the HelloWorld Project directory –VC++ Project –Contains information regarding the project 2.2 Compiling and Running the Template HelloWorld Application (Cont.) VC++ Project

15 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Compiling 2.2 Compiling and Running the Template HelloWorld Application (Cont.) Output Window

16 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. After Compiling −Creates several files in multiple directories −Creates both object (.obj) and linked executable (.exe) files.exe file is used to run application 2.2 Compiling and Running the Template HelloWorld Application (Cont.) Newly Created Debug folder (selected) HelloWorld project folder

17 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.2 Compiling and Running the Template HelloWorld Application (Cont.) After compiling, a newly created HelloWorld project Debug directory contains the object file. The Object file contains the “Object” code created by compiler. Figure 2.13 HelloWorld Project directory after compilation. Newly Created Debug folder (selected) HelloWorld project folder

18 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. The Object file contains the “Object” (i.e., the Machine) code created by the compiler. 2.2 Compiling and Running the Template HelloWorld Application (Cont.) Object File (selected) HelloWorld project folder Debug folder

19 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. After compiling, the HelloWorld solution Debug directory contains the executable file. 2.2 Compiling and Running the Template HelloWorld Application (Cont.) HelloWorld solution folder Solution Debug folder Solution (.sln ) file HelloWorld project

20 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 2.14 Examining the contents of the Debug directory. 2.2 Compiling and Running the Template HelloWorld Application (Cont.) Newly created.exe file

21 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Running the application from Visual Studio Run the application from the console window. 2.2 Compiling and Running the Template HelloWorld Application (Cont.) In addition to the application not producing any output, what is the difference?

22 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.3Introduction to C++ Code Figure 2.16 HelloWorld application’s C++ source code in Visual Studio.NET.

23 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.3Introduction to C++ Code (Cont.) Comments –Begin with two forward slashes // –Improve readability –Explain code Full-line comments End-of-line comments –Compiler ignores comments Figure 2.17 Examining comments in the HelloWorld application’s C++ source code in Visual Studio.NET. Full-line comments

24 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.3Introduction to C++ Code (Cont.) Preprocessor (i.e., Compiler Directives) –Processes lines beginning with a pound sign # Processed before the source code is compiled (pass 2) –Tells Preprocessor to “include” console window input/ output code –Forgetting to include iostream in a program that uses input or output is an error Figure 2.18 Preprocessor directive (pass 1). Preprocessor directive

25 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.3Introduction to C++ Code (Cont.) 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. Figure 2.19 using directive. using directive

26 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.3Introduction to C++ Code (Cont.) Function main –Entry point of every C++ program –Returns an integer return –0 indicates successful termination Figure 2.20 main function definition. Function main header Exiting the function using return

27 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.3Introduction to C++ Code (Cont.) Structure of function main –Declare return type ( int etc.) –Start with left brace { –Body of the function –End with right brace }

28 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 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 2.4Constructing the HelloWorld Application

29 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.4Constructing the HelloWorld Application (Cont.) Executable statement –Compiler generates machine code to perform an action Stream insertion operator << –Outputs a stream of characters to the screen –Binary operator (i.e., it operates on two operands) Semicolon ( ; ) –Statement terminator –Forgetting the semicolon at the end of a statement is an error Figure 2.21 Inserting a C++ statement that displays text when executed. Displaying a message

30 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.4Constructing the HelloWorld Application (Cont.) 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

31 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.4Constructing the HelloWorld Application (Cont.) In Section 2.4 your textbook has you run the Welcome application using the “start without debugging” (shortcut ctrl- F5) 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.turn the ctrl-F5 command back ON

32 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Now Run the application from Visual Studio Run the application from the console window. 2.2 Compiling and Running the Template HelloWorld Application (Cont.) 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.

33 © Copyright 1992–2005 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 to the include list and place getch(); just before the return statement. Include console I/O library header file. Insert getch() instruction without an argument.

34 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Build your solution (F7) and the start debugging (F5). 2.2 Compiling and Running the Template HelloWorld Application (Cont.) Your Hello World message is displayed. Press any key to run the return statement and close the console window.

35 © Copyright 1992–2005 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.

36 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Start debugging (F5). 2.2 Compiling and Running the Template HelloWorld Application (Cont.) Your Hello World message is displayed. Press any key to run the return statement and close the console window.

37 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Selecting Continue (F5) the simulation runs the return statement ending the program and closing the console window. 2.2 Compiling and Running the Template HelloWorld Application (Cont.)

38 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. String literals –Compiler does not ignore whitespace characters –Splitting a statement in the middle of a string is a syntax error 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 2.4Constructing the HelloWorld Application (Cont.)

39 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.4Constructing the HelloWorld Application (Cont.)

40 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.4Constructing the HelloWorld Application (Cont.)

41 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.4Constructing the HelloWorld Application (Cont.) 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.25 HelloWorld application using multiple stream insertion operators. Multiple stream insertion operators Figure 2.26 HelloWorld application output. Note missing space.

42 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 2.27 Inserting newlines. 2.4Constructing the HelloWorld Application (Cont.) Appending a newline to displayed text

43 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. HelloWorld.cpp (1 of 1) HelloWorld application using a newline character Full line comments describe the applications purpose Include the header file using directive provides access to the std namespace Define the main function Print a HelloWorld message Exit the application

44 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.5Compilation Errors Debugging –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

45 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 2.30 Output window listing a syntax error. 2.5Compilation Errors (Cont.) Missing semicolon at the end of the statement preceding ‘return’

46 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.5Compilation Errors (Cont.) Creating syntax errors –Delete ‘n’ in the last escape sequence in line 10 –Capitalize ‘r’ in return Figure 2.32 Introducing two syntax errors into your code. Two syntax errors

47 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 2.33 Syntax error messages generated by the compiler. 2.5Compilation Errors (Cont.) Double-click line in the output window to have marker placed next to the line with the error in the source code. Description of the errors including file name and line number

48 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Lab and Homework Assignment Tutorial 2 − Welcome Application. Turn in annotated source file with your own comments. Answer and Turn-in Tutorial 2 Questions at the end of the Chapter. Always write the question followed by the answer. Remember to highlight the answer. Due next Wednesday


Download ppt "© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - HelloWorld Application: Introduction to."

Similar presentations


Ads by Google