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

Introduction to C++

(using Microsoft Visual


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

Understand the basics of C++ programs and its


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 (MVSC). Lab 1 begins with a brief explanation

of the IDE where it covers from the beginning of constructing a program until the
programs output. Then student will be introduced to the structure of a simple C++
program. Before writing the first C++ program using MVSC, basic command of output
will be described.

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. Figure 1 shows a typical C++
program development environment.

1.1 A typical C++ program development environment.

ed the source code. A preprocessor is a program that removes all comments and modifies source code according to preprocessor directive (statement

1.1.2 A simple C++ program


Figure 2 shows the typical structure of a simple C++ program.

1.2 Typical structure of a C++ program.

Table 1 Description of C++ program structure in Figure 2.

No.
1

Description
Comments: Internal programming documentation. Comments will be ignored

by compiler, and comments do not affect the way the program runs.
Preprocessor Directive: Tells the compiler to include input/output stream

library file in the program.


Namespace: Namespace is a collection of classess, objects, functions, variables,
constant, and types that are enclosed in the libraries. using namespace

4
5

statement will tell the compiler where to look for names in the libraries.
Global declaration: Declaration that are visible to all parts of the program.
Main function: The starting point of a program. A group of statements/program

instructions. Every C++ program must have a main().


User-defined functions: A group of statements/program instructions 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 given to it to the standard output

display device, e.g. monitor.

Escape sequence: The backslash character (\). This character tells the compiler
that the sequence following does not have the same meaning as the character
appearing by itself. Table 2 below lists some commonly used escape sequences.

Table 2 Commonly used escape sequence

Escape

Name

Description

Sequence
\a
\t
\n or endl
\"
\'
\?
\\

Audible alert
Horizontal Tab
New line
Double Quote
Apostrophe
Question mark
Backslash

Bell sound
Takes the cursor to the next tab stop
Takes the cursor to the beginning of the next
line
Displays a quotation mark (")
Displays an apostrophe (')
Displays a question mark
Displays a backslash (\)

1.2 Step by step Examples

1.2.1 Your First Program


1. Start the MS Visual C++ 2010 software from the task bar. The main window of the
Microsoft Visual Studio 2010 should be similar to the below display:

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 youd like to store
project files by clicking the 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 >

8.

In the Additional Options section, click the Empty Project check box

9. Click Finish.
10. 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...

11.In the Installed Templates list of the Add New Item dialog box, make sure Visual
C++ is selected.

12.

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

13.

In the Name text box, give the C++ program file a name, e.g: Exercise.

14.

Click Add.
15.

Using the text editor in the IDE, 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;

}
16. To compile the program, on the main menu, click Build -> Build Exercise1.exe

10

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


========== succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

is displayed in the Output window as shown below.

18. To see the output (execute), on the main menu, click Debug-> Start without
Debugging.

11

19. Also displayed in a DOS window (C:\Windows\system32\cmd.exe) is the


program output:

20. After viewing the result, press any key on the keyboard to close the program
output and return to MSVC.

12

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

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

Universiti Teknikal Malaysia, Melaka

Faculty :

FKEKK

Programme :

BENG

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

2. Write programs that can give outputs as below:


a)

b)

13

The background / foreground colour for the output screen, its font,
size etc can be set in the output windows property.

1.4 Self-Review Questions


1. Give appropriate programming terms for the following:
a) A program in high-level or assembly-level 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) Input values used in a program to see if it is producing the correct results.
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 code.
Answer:
g) Aa)
machine language version of source code.
b)interactive program used to create and modify source programs or data.
h) An
c) collection of programs used by the computer.
i) The
d)
j) Punctuation
that signifies the end of a C++ statement.
e)
f)
g)
h)
i)
j)

14

2. State whether the following statements is TRUE or FALSE


a) The purpose of a library file such as iostream is to store a
b)
c)
d)
e)

programs source code.


The C++ standard function to read data from keyboard is cin.
In general, C++ statement are case-sensitive.
One and only one function may be named main.
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:

15

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:

16

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