You are on page 1of 48

Tutorial 2 - HelloWorld Application: Introduction to C++ Programming

Outline

2.1
2.2 2.3 2.4 2.5 2.6

Test-Driving the HelloWorld Application


Compiling and Running the Template HelloWorld Application Introduction to C++ Code Constructing the HelloWorld Application Compilation Errors Wrap-Up

Copyright 19922005 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.

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

2.1

Test-Driving the HelloWorld Application (Cont.)

Figure 2.1

Locating the completed HelloWorld application.

Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 19922004

2.1

Test-Driving the HelloWorld Application (Cont.)

Figure 2.2 Running the completed HelloWorld application.

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

2.2 Compiling and Running the Template HelloWorld Application


Projects Large applications can contain many projects

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

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


Figure 2.4 New Project dialog. File > New > Project; Title Bar (displaying New Project) Templates: pane

Project Types: pane

Description of project selected in Templates: pane


Default project and solution name

Location of the new project (your location may be different)

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

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

Updated project and solution name

Updated project location

Browse button

Copyright 19922005 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

Copyright 19922005 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

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

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


Figure 2.9 Solution Explorer window in Visual Studio .NET.

HelloWorld solution HelloWorld project Source Files folder

New Solution
Named HelloWorld

New Project
Named HelloWorld
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

2.2 Compiling and Running the HelloWorld Application (Cont.)


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

HelloWorld.cpp file (selected)

Copyright 19922005 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.

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

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


Figure 2.11 Solution Explorer window after adding HelloWorld.cpp.

HelloWorld. cpp file

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

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


Before Compiling Review .vcxproj files

located in the HelloWorld Project directory


VC++ Project Contains information regarding the project
HelloWorld project folder

HelloWorld.cpp file (selected)

VC++ Project
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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


Compiling

Output Window

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

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


After Compiling Creates several files in multiple directories

Creates both object (.obj) and linked executable (.exe) files


.exe file is used to run application
HelloWorld project folder

Newly Created Debug folder (selected)


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

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


Figure 2.13 HelloWorld Project directory after compilation. HelloWorld project folder

Newly Created Debug folder (selected)

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.

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


The Object file contains the Object (i.e., the Machine) code created by the compiler. HelloWorld
project folder

Debug folder Object File (selected)

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

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


After compiling, the HelloWorld solution Debug directory contains the executable file.
HelloWorld solution folder

HelloWorld project

Solution Debug folder

Solution (.sln) file

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

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

Figure 2.14

Examining the contents of the Debug directory.

Newly created .exe file

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.

Running the application from Visual Studio

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

Introduction to C++ Code

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

Introduction to C++ Code (Cont.)

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

Compiler ignores comments

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

2.3
Figure 2.18

Introduction to C++ Code (Cont.)


Preprocessor directive (pass 1).

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

Introduction to C++ Code (Cont.)

using directive. using directive

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

Introduction to C++ Code (Cont.)

main function definition.

Exiting the function using return

Function main Entry point of every C++ program Returns an integer

return
0 indicates successful termination
Copyright 19922005 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 }

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

2.4

Constructing the HelloWorld Application

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

Constructing the HelloWorld Application (Cont.)


Inserting a C++ statement that displays text when executed.

Figure 2.21

Displaying a message

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
Copyright 19922005 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 (-) 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

Constructing the HelloWorld Application (Cont.)

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.

Now Run the application from Visual Studio

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.

Insert getch() instruction without an argument.

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

Your Hello World message is displayed.

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

Your Hello World message is displayed.

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


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

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

2.4

Constructing the HelloWorld Application (Cont.)

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
Copyright 19922005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

2.4

Constructing the HelloWorld Application (Cont.)


Description

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

Constructing the HelloWorld Application (Cont.)


Outp ut Welcome to C++! Welcome to C++!

Sta te me nt cout << "Welcome\nto\nC++!";

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++!";

(followed by three beeps)


Use \n for a newline. "This is quoted text." Welcome to C++! to C++!

Figure 2.24

Esc a p e seq uenc e exa mp les.

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

2.4

Constructing the HelloWorld Application (Cont.)


HelloWorld application using multiple stream insertion operators.

Figure 2.25

Multiple stream insertion operators

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

Constructing the HelloWorld Application (Cont.)

Figure 2.27 Inserting newlines.


Appending a newline to displayed text

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

HelloWorld.cpp (1 of 1)

Define the main function Print a HelloWorld message

Full line comments describe the applications purpose

Include the <iostream> header file

using directive provides access to the std namespace

Exit the application

HelloWorld application using a newline character

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

Compilation Errors (Cont.)

Output window listing a syntax error.

Missing semicolon at the end of the statement preceding return

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

2.5

Compilation Errors (Cont.)

Figure 2.32 Introducing two syntax errors into your code.

Two syntax errors

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

Compilation Errors (Cont.)

Syntax error messages generated by the compiler.

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

Copyright 19922005 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

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

You might also like