Вы находитесь на странице: 1из 15

Introduction to C++

(using Microsoft Visual C++)


By the end of this section, the students should be able
to:

 Understand the basics of C++ programs and its


development environment.
 Develop a simple C++ program of the Console
Application type using the Microsoft Visual C++.

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

1.1.1 A Typical C++ Environment

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.

Figure 1.1 A typical C++ program development environment.

A program is also called source code. A preprocessor is another


program that removes all comments and modifies source code,
according to the commands called preprocessor directives (statements
begin with #) found in the source code .

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.

/*Program 1: This program demonstrates


1
a simple C++ program*/
Comments 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;
}

Figure 1.2 Typical structure of a C++ program.

Table 1.1 Descriptions of Program 1’s structure


No. Description
1 Comments: Internal programming documentation. Comments will be ignored
by compiler, and comments do not affect the way the program runs.
Programmers use them to include short explanations or observations
concerning the code or program.
2 Preprocessor Directive: The directive #include <iostream>, instructs
the preprocessor to include a section of standard C++ code, from a header file
known as iostream, that allows this program to perform standard input and
output operations, such as writing the output of this program to the screen.
3 Namespace: Namespace is a collection of classess, objects, functions,
variables, constant, and types that are enclosed in the C++ libraries. When you
use using namespace std; you are instructing C++ compiler to use the

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.

 All C++ statements except preprocessor directive end with a


semicolon (;).
 C++ functions must have an opening and matching closing
bracket. ( {…<statement>…} )

1.1.3 Basic Command of Output

Below are some basic commands to understand your first program.


 cout<< : an output object that sends data to the standard output display
device, e.g. monitor.
 Escape sequence: Consists of two characters. First is the backslash character (\),
next the character that determines the interpretation of the escape sequence.
Table 1.2 below lists some commonly used escape sequences.

Table 1.2 Commonly used escape sequence

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.2 Step by Step Examples

1.2.1 Your First Program

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.

11. In the middle pane, click C++ File (.cpp).

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:

/*Program 1: This program demonstrate a simple 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.

15. If there is no error in your project, the message :

====== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped =====


is displayed in the Output window as shown below.

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.

 Syntax error: Syntax is the set of rules for forming valid


instructions. Syntax error violates the syntax of a language.
 Warning message: Message from a compiler advises the
programmer that a statement is technically acceptable but might
have a potential error.

11
1.3 Exercises

1. Write a program that can print as the following:

University : Universiti Teknikal Malaysia, Melaka


Faculty : FKE
Programme : BENC

My name is Muhammad Bin Abdul Rahman. I am from Johor.

2. Write programs that can give the following output:


a)

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

1. Give appropriate programming terms for the following:

a) A program written in high-level or assembly language.


b) A mistake that is a direct violation of the syntax rules.
c) A program compiles and runs, but gives an incorrect output.
d) It links object code with the C++ library to form executable code.
e) The language, made up of binary coded instructions, that is used directly by the
computer.
f) A program that translates a program written in a high-level language into
machine language.
g) A machine language version of source code.
h) An interactive program used to create and modify programs or data.
i) Statement that begins with #.
j) Punctuation that signifies the end of a C++ statement.

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.

3. a) List the sequence of steps associated with implementing a program.


b) State the difference between:
i. syntax error and logic error
ii. logic error and run-time error
iii. testing and desk checking

c) Define the term ‘test data’.

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:

5. Give the output of the following program:

//Program 2: This program will print asterisk


#include <iostream>
using namespace std;
int main()
{
cout << "o\t*o\n";
cout << "***o\n*******o";
cout << "\n*****";
cout << "*******o\n*******************o\n";
return 0;
}

Output:

15

Вам также может понравиться