Professional Documents
Culture Documents
1 Introduction
The first lab of C++ will describe how to use the Integrated Development
Environment (IDE) of the Microsoft Visual C++ 2010 (MSVC). The lab begins with a
brief explanation of the IDE where it covers from the beginning of constructing a C++
program until the program’s output. Then student will be introduced to the structure of a
simple C++ program. Before writing the first C++ program using MSVC, basic
command of output will be described.
1
1.1 Theoretical Background
A C++ system generally consists of three parts which are; a program development
environment, the language, and the C++ Standard Library. Let's consider the steps in
creating and executing a C++ program using a C++ development environment
(illustrated in Figure. 1.1). Every C++ program typically goes through these five steps:
edit, compile, link, load and execute.
1. Text Editor
2. Compiler
Program is created in
the editor and stored
on a disk 2.1 Preprocessor 2.2 Translator
Converting the
Preprocessor program
program to machine
process the code
language (object code)
3. Linker
Links the object code
with C++ libraries to
form an executable 4. Loader
program
On OS command (run),
Loader will locate the
executable program and
reads it into memory to
begins execution.
2
1.1.2 A simple C++ program
Figure 1.2 shows the typical structure of a simple C++ program, with Program 1 as an
example. Meanwhile, Table 1.1 describes the structure of Program 1.
Preprocessor 2
#include <iostream> 2
Directive
using namespace std; 3
Namespace 3
void fun(); 4
Global declaration 4
int main()
{ 5
cout<<”Welcome to C++ Lab\n”;
5
Main function fun();
return 0;
User-defined function }
6
(see Lab 4) void fun()
{ 6
cout<<”C++ is fun!”;
cout<<endl;
}
3
standard C++ library.
4 Global declaration: Declarations that are visible to all parts of the program.
5 Main function: The starting point of a C++ program’s execution. Contains a
group of statements (program instructions). Every C++ program must have this
function.
6 User-defined functions: A group of statements that were defined by the
programmer.
Escape
Name Description
Sequence
\a Audible alert Bell sound
\t Horizontal Tab Takes the cursor to the next tab stop
Takes the cursor to the beginning of the next
\n or endl New line
line
\" Double Quote Displays a quotation mark (")
4
\' Apostrophe Displays an apostrophe (')
\? Question mark Displays a question mark
\\ Backslash Displays a backslash (\)
1. Start the MS Visual Studio 2010 software from the task bar, that has the main
window similar to the display below:
5
2. The first step in writing a Visual C++ program is to create a project for it. In the
menu bar, click File -> New -> Project...
3. In the New Project dialog box shown below, select by clicking Visual C++ in the
Installed Template pane and Win32 Console Application in the middle pane.
4. In the Name text box, give the project a name, e.g. Exercise1
5. In the Location text box (at the bottom of the dialog box), specify the path of the
project (e.g. C:\LAB1), or select the location or folder in which you’d like to store
the project files by clicking Browse…
6. Click OK. Note that there is no need to enter a name in the Solution name text box;
the system fills the project name in it by default.
7. In the Win32 Application Wizard - Exercise1 dialog box, click Next >
6
8. In the Additional Options section, click the Empty Project check box. Then click
Finish.
9. To create a new C++ program file, right click the Source Files Folder on the
Solution Explorer. In the popup menu, click Add then New Item or in the menu
bar, click Project -> Add -> New Item...
7
10. In the Installed Templates list of the Add New Item dialog box, make sure Visual
C++ is selected.
12. In the Name text box, give the C++ program file a name, e.g: Exercise. Then click
Add.
8
13. You have just created an empty C++ source file (Exercise.cpp) and added it to the
project (Exercise1). The main window will display the text editor, ready for editing
this new C++ file (Exercise.cpp). Fill the contents of file Exercise with the
following C++ program:
#include <iostream>
using namespace std;
int main()
{
cout << "Welcome to Lab 1!!!\n";
return 0;
}
14. To compile and link the program, on the main menu, click Build -> Build
Exercise1; or press Ctrl+F7.
9
16. To load, execute and see the output, on the main menu, click Debug-> Start
without Debugging; or press Ctrl+F5.
10
17. Displayed in a DOS window (C:\Windows\system32\cmd.exe) is the program’s
output:
18. After viewing the result, press any key on the keyboard to close the program’s
output and return to MSVC.
11
1.3 Exercises
b)
The background / foreground colour for the output screen, its font,
size etc can be set in the output windows property.
12
1.4 Self-Review Questions
Answer:
a)
b)
c)
d)
e)
f)
g)
h)
i)
j)
13
2. State whether the following statements is TRUE or FALSE.
a) The purpose of a header file such as iostream is to store a
program’s source code.
b) The C++ standard function to read data from keyboard is cin.
c) In general, C++ statements are case-sensitive.
d) One and only one function may be named main.
e) Comments are used by the preprocessor to help format the
program.
Answer:
14
4. Give the output of the following program:
//Program 1: This program that will print lines
#include <iostream>
using namespace std;
int main()
{
cout << "ONE\tTWO"<<endl;
cout << "THREE\n\n\FOUR\n\n\n";
cout << "\tFIVE\tSIX";
cout << endl<<endl<<"SEVEN EIGHT NINE\n\n";
return 0;
}
Output:
Output:
15