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 - Welcome Application: Introduction to C++

Similar presentations


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

1 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++ Programming Outline 2.1 Test-Driving the Welcome Application 2.2 Compiling and Running the Template Welcome Application 2.3 Introduction to C++ Code 2.4 Constructing the Welcome 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. 2.1Test-Driving the Welcome Application

4 © 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 Welcome application. 2.1Test-Driving the Welcome Application (Cont.)

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

6 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.2 Compiling and Running the Template Welcome Application Projects –Large applications can contain many projects Solutions

7 © 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 Welcome Application (Cont.)

8 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 2.4 New Project dialog. Title Bar (displaying New Project ) Visual C++ Projects directory (selected) Project Types: pane Location of the new project (your location may be different) ASP.NET Web Application (selected) Templates: pane Description of project selected in Templates: pane Default project and solution name 2.2 Compiling and Running the Template Welcome Application (Cont.)

9 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 2.5 New Project dialog with updated project information. 2.2 Compiling and Running the Template Welcome Application (Cont.) Updated project location Updated project and solution name Browse… button

10 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 2.6 Project Location dialog. 2.2 Compiling and Running the Template Welcome Application (Cont.) SimplyCpp directory (selected) Open button

11 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 2.7 Win32 Application Wizard dialog. 2.2 Compiling and Running the Template Welcome Application (Cont.) Application Settings link

12 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 2.8 Application Settings in the Win32 Application Wizard dialog. 2.2 Compiling and Running the Template Welcome Application (Cont.) Console application radio button (selected) Empty project checkbox (selected)

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

14 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 2.10 Add Existing Item - Welcome dialog. 2.2 Compiling and Running the Template Welcome Application (Cont.) Welcome.cpp file (selected)

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

16 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Compiling –Creates several files in the directory.ncb files –Contain information for IntelliSense.sln files –Contain information about the solution.vcproj files –Contains information regarding the project.suo files –Hidden 2.2 Compiling and Running the Template Welcome Application (Cont.)

17 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 2.12 Welcome directory. 2.2 Compiling and Running the Template Welcome Application (Cont.) Source code (.cpp ) file Solution (.sln ) file Project (.vcproj ) file

18 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.2 Compiling and Running the Template Welcome Application (Cont.) Contains machine code created by compiler Contains executable file created after linking Contains various files that store debugging information Figure 2.13 Welcome directory after compilation. Newly created Debug directory

19 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Build > Build Solution Command –Creates.exe executable file.exe file is used to run application 2.2 Compiling and Running the Template Welcome Application (Cont.)

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 Welcome Application (Cont.) Newly created.exe file

21 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Running the application –Debug > Start Without Debugging Application does not produce output Figure 2.15 Running the template Welcome application. 2.2 Compiling and Running the Template Welcome Application (Cont.)

22 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.3Introduction to C++ Code Text editors –Code appears similar but every text editor and IDE is different Figure 2.16 Welcome 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 Welcome 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 –Processes lines beginning with a pound sign # Processed before the source code is compiled –Necessary for input / output –Forgetting to include iostream in a program that uses input or output is an error Figure 2.18 Preprocessor directive. 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 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 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.) Defining 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) Usually tied to keyboard but can be tied to other devices –Output Often output to cout (standard output stream object) Usually tied to monitor but can be tied to other devices 2.4Constructing the Welcome Application

29 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.4Constructing the Welcome Application (Cont.) Executable statement –Compiler generates code to perform an action Stream insertion operator << –Outputs a stream of characters to the screen –Binary operator 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 Welcome 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 Welcome Application (Cont.) Figure 2.22 Welcome application using cout to display text.

32 © 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 –Escape character is backslash \ Allows escape sequences to be read by compiler inside string literals 2.4Constructing the Welcome Application (Cont.)

33 © 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 Welcome Application (Cont.)

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

35 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.4Constructing the Welcome 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 Welcome application using multiple stream insertion operators. Multiple stream insertion operators

36 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2.4Constructing the Welcome Application (Cont.) Figure 2.26 Welcome application output.

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

38 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Welcome.cpp (1 of 1) © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Welcome 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 welcome message Exit the application

39 © 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

40 © 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 line 12

41 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 2.31 Task List window listing a syntax error. 2.5Compilation Errors (Cont.) Location of error within the file Description of error Location of file containing error

42 © 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

43 © 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.) Number of errors displayed in task list title bar Description of the errors including file name and line number

44 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Figure 2.34 Syntax error causes remainder of line to be displayed in cyan. 2.5Compilation Errors (Cont.) Syntax error creates unterminated string (displayed in cyan)

45 © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Fixing the errors –Go back to your code and fix the syntax errors indicated in the Command Prompt 2.5Compilation Errors (Cont.)


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

Similar presentations


Ads by Google