week 02 - hello world

Upload: justine-fulls

Post on 05-Apr-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 Week 02 - Hello World

    1/48

    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.

  • 8/2/2019 Week 02 - Hello World

    2/48

    Copyright 1992 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    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.

  • 8/2/2019 Week 02 - Hello World

    3/48

    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.1 Test-Driving the HelloWorld Application(Cont.)

  • 8/2/2019 Week 02 - Hello World

    4/48

    Copyright 1992 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    Figure 2.2 Running the completed HelloWorld application.

    2.1 Test-Driving the HelloWorld Application (Cont.)

  • 8/2/2019 Week 02 - Hello World

    5/48

    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 theTemplate HelloWorld Application

    Projects

    Large applications can contain many projects

    Solutions

  • 8/2/2019 Week 02 - Hello World

    6/48

    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 NewProject )

    Project Types: pane

    Location of the newproject (yourlocation may bedifferent)

    Templates:

    pane

    Description of project selected inTemplates: pane

    2.2 Compiling and Running theTemplate HelloWorld Application (Cont.)

    Default projectand solutionname

  • 8/2/2019 Week 02 - Hello World

    7/48

    Copyright 1992 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    2.2 Compiling and Running theTemplate HelloWorld Application (Cont.)

    Updated projectlocation

    Updated projectand solution name

    Browse button

  • 8/2/2019 Week 02 - Hello World

    8/48

    Copyright 1992 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    2.2 Compiling and Running theTemplate HelloWorld Application (Cont.)

    SimplyCppdirectory(selected)

    Open button

  • 8/2/2019 Week 02 - Hello World

    9/48

    Copyright 1992 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    2.2 Compiling and Running theTemplate HelloWorld Application (Cont.)

    New HelloWorld Solution Folder

    New HelloWorld Project Folder with C++ Source File

  • 8/2/2019 Week 02 - Hello World

    10/48

    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 theTemplate HelloWorld Application (Cont.)

  • 8/2/2019 Week 02 - Hello World

    11/48

    Copyright 1992 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    2.2 Compiling and Running theHelloWorld Application (Cont.)

    HelloWorld.cpp file (selected)

    Download HelloWorld.cpp to your new HelloWorld ProjectFolder.

    Right-click HelloWorld.cpp file and select properties.

    Under General Tab uncheck Read-only. HelloWorld project folder

  • 8/2/2019 Week 02 - Hello World

    12/48

    Copyright 1992 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    2.2 Compiling and Running theHelloWorld Application (Cont.)

    In Visual Studio Project Pane, right-click source folder and selectAdd Existing Item.

  • 8/2/2019 Week 02 - Hello World

    13/48

    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 theTemplate HelloWorld Application (Cont.)

  • 8/2/2019 Week 02 - Hello World

    14/48

    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

  • 8/2/2019 Week 02 - Hello World

    15/48

    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

  • 8/2/2019 Week 02 - Hello World

    16/48

    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 CreatedDebug folder (selected)

    HelloWorld project folder

  • 8/2/2019 Week 02 - Hello World

    17/48

    Copyright 1992 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    2.2 Compiling and Running theTemplate HelloWorld Application (Cont.)

    After compiling, a newly created HelloWorld project Debug directorycontains the object file.

    The Object file contains the Object code created by compiler.

    Figure 2.13

    HelloWorld Project directory after compilation.

    Newly CreatedDebug folder (selected)

    HelloWorld

    project folder

  • 8/2/2019 Week 02 - Hello World

    18/48

    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 createdby the compiler.

    2.2 Compiling and Running the Template HelloWorld Application (Cont.)

    Object File (selected)

    HelloWorld project folder

    Debug folder

  • 8/2/2019 Week 02 - Hello World

    19/48

    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 Debugfolder

    Solution ( .sln ) file

    HelloWorldproject

  • 8/2/2019 Week 02 - Hello World

    20/48

    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 theTemplate HelloWorld Application (Cont.)

    Newly created .exe file

  • 8/2/2019 Week 02 - Hello World

    21/48

    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 theTemplate HelloWorld Application (Cont.)

    In addition to the application not producing any output,what is the difference?

  • 8/2/2019 Week 02 - Hello World

    22/48

    Copyright 1992 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    2.3 Introduction to C++ Code

    Figure 2.16 HelloWorld applications C++ source code in Visual Studio .NET.

  • 8/2/2019 Week 02 - Hello World

    23/48

    Copyright 1992 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    2.3 Introduction 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 applications C++

    source code in Visual Studio .NET.

    Full-line comments

  • 8/2/2019 Week 02 - Hello World

    24/48

    Copyright 1992 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    2.3 Introduction 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

  • 8/2/2019 Week 02 - Hello World

    25/48

    Copyright 1992 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    2.3 Introduction 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 maybe called by your first and last name.

    Figure 2.19 using directive.

    using directive

  • 8/2/2019 Week 02 - Hello World

    26/48

    Copyright 1992 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    2.3 Introduction 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 functionusing return

  • 8/2/2019 Week 02 - Hello World

    27/48

    Copyright 1992 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    2.3 Introduction 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 }

  • 8/2/2019 Week 02 - Hello World

    28/48

    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 anerror

    C++ applications input / output data

    Input

    Certain C++ input comes from cin (standard input streamobject). cin >> myVariable ;

    Usually tied to keyboard but can be tied to other devices

    Output

    Often output to cout (standard output stream object)cout

  • 8/2/2019 Week 02 - Hello World

    29/48

    Copyright 1992

    2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    2.4 Constructing the HelloWorld Application (Cont.)

    Executable statement Compiler generates machine code to perform an action

    Stream insertion operator

  • 8/2/2019 Week 02 - Hello World

    30/48

    Copyright 1992 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    2.4 Constructing the HelloWorld Application (Cont.) Operators

    Special characters in C++ that perform operations

    Binary and unary

    Addition operator ( +), subtraction operator ( - ) andmultiplication operator ( * ) are examples of C++ operators

    Unary operators

    Operators that require only one operand such as the preincrementoperator ( ++ )

    Binary operators

    Operators such as multiplication ( * ) or division ( / ) that requiretwo operands to perform their operation

    String

    A sequence of characters

  • 8/2/2019 Week 02 - Hello World

    31/48

    Copyright 1992 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    2.4 Constructing the HelloWorld Application (Cont.)

    In Section 2.4 your textbook has you run the Welcomeapplication using the start without debugging (shortcut ctrl -F5) command. This command is no longer supported inVisualStudio 2010. While you can turn the ctrl-F5 commandback ON , I will step you through some alternative solutions.

    http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/d6df1031-dd73-4683-b7d8-3252eb108439/http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/d6df1031-dd73-4683-b7d8-3252eb108439/http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/d6df1031-dd73-4683-b7d8-3252eb108439/http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/d6df1031-dd73-4683-b7d8-3252eb108439/http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/d6df1031-dd73-4683-b7d8-3252eb108439/http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/d6df1031-dd73-4683-b7d8-3252eb108439/
  • 8/2/2019 Week 02 - Hello World

    32/48

    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 theTemplate HelloWorld Application (Cont.)

    Unlike running the application from the console windowVisual Studio simply opens the console window runs theprogram and closes it again. Lets look at some solutionsto this problem.

  • 8/2/2019 Week 02 - Hello World

    33/48

    Copyright 1992 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    2.2 Compiling and Running theTemplate HelloWorld Application (Cont.)

    In this solution we simply add #include to theinclude list and place getch(); just before the returnstatement. Include console I/O library

    header file.

    Insert getch() instruction without anargument.

  • 8/2/2019 Week 02 - Hello World

    34/48

    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 theTemplate HelloWorld Application (Cont.)

    Your Hello World message is displayed.

    Press any key to run the return statement and close theconsole window.

  • 8/2/2019 Week 02 - Hello World

    35/48

    Copyright 1992 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    2.2 Compiling and Running theTemplate HelloWorld Application (Cont.)

    In this solution we add a breakpoint just before the returnstatement. Click here to turn Breakpoint

    ON and OFF

    A breakpoint tells the debugger to stop simulation of theprogram before running this line of code.

    To set a breakpoint select the line and Debug > ToggleBreakpoint (F9) or simply click in the column where thebreakpoint is shown.

  • 8/2/2019 Week 02 - Hello World

    36/48

    Copyright 1992 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    Start debugging (F5) .

    2.2 Compiling and Running theTemplate HelloWorld Application (Cont.)

    Your Hello World message is displayed.

    Press any key to run the return statement and close theconsole window.

  • 8/2/2019 Week 02 - Hello World

    37/48

    Copyright 1992 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    Selecting Continue (F5) the simulation runs the return statementending the program and closing the console window.

    2.2 Compiling and Running theTemplate HelloWorld Application (Cont.)

  • 8/2/2019 Week 02 - Hello World

    38/48

    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 syntaxerror

    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.4 Constructing the HelloWorld Application (Cont.)

  • 8/2/2019 Week 02 - Hello World

    39/48

    Copyright 1992 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    Escape Sequence Description\n Newline. Positions the screen cursor at the beginning of the next line.

    \t Horizontal tab. Moves the screen cursor to the next tab stop.

    \r Carriage return. Positions the screen cursor at the beginning of the current line; does not advanceto the next line.

    \a Alert. Sounds the system bell.

    \\ Backslash. Used to print a backslash character.

    \" Double quote. Used to print a double quote character.

    Figure 2 .23 Escape sequences.

    Copyright 1992 2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    2.4 Constructing the HelloWorld Application (Cont.)

  • 8/2/2019 Week 02 - Hello World

    40/48

    Copyright 1992 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    State ment O utputcout

  • 8/2/2019 Week 02 - Hello World

    41/48

    Copyright 1992 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    2.4 Constructing the HelloWorld Application (Cont.)

    Stream insertion operators resume printing where previous statementhas stopped

    When breaking up lengthy statements, choose logical break pointssuch as escape sequences, etc.

    Figure 2.25 HelloWorld application using multiple stream insertion operators.

    Multiple streaminsertion operators

    Figure 2.26

    HelloWorld application output. Note missing space .

  • 8/2/2019 Week 02 - Hello World

    42/48

    Copyright 1992 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    Figure 2.27 Inserting newlines.

    2.4 Constructing the HelloWorld Application (Cont.)

    Appending anewline todisplayed text

  • 8/2/2019 Week 02 - Hello World

    43/48

    Copyright 1992 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    HelloWorld.cpp (1 of 1)

    HelloWorld applicationusing a newline character

    Full line comments describe

    the applications purpose

    Include the header file

    using directive providesaccess to the std namespace

    Define the main function

    Print a HelloWorldmessage

    Exit the application

  • 8/2/2019 Week 02 - Hello World

    44/48

    Copyright 1992 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    2.5 Compilation 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

  • 8/2/2019 Week 02 - Hello World

    45/48

    Copyright 1992 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    Figure 2.30 Output window listing a syntax error.

    2.5 Compilation Errors (Cont.)

    Missing semicolon at the end of

    the statement preceding return

  • 8/2/2019 Week 02 - Hello World

    46/48

    Copyright 1992 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    2.5 Compilation 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

  • 8/2/2019 Week 02 - Hello World

    47/48

    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.5 Compilation Errors (Cont.)

    Description of the errorsincluding file name and linenumber

    Double-click line in the output window tohave marker placed next to the line withthe error in the source code

  • 8/2/2019 Week 02 - Hello World

    48/48

    Copyright 1992 2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

    Lab and Homework Assignment

    Tutorial 2 HelloWorld 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