Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

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 using Microsoft Visual C++ .Net Console
Application

1 Introduction

The first lab of C++ will describe how to use Microsoft Visual Studio C++ .NET (MVSC). Lab
1 begins with an explanation of C++ typical environment where it covers from the beginning of
constructing a program until the program’s output. Then student will be introduced to the form
and structure of a C++ simple program. Before trying the first program using MVSC, basic
output command 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. 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: A typical C++ program development environment.

ce code. A preprocessor is a program that removes all comments and modifies source code according to preprocessor directive
1.1.2 A simple C++ program

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

/*Program 1: This program


demonstrate a simple C++ 1

Comments 1 program*/

Preprocessor 2
#include <iostream> 2
Directive
using namespace std; 3
Namespace 3
void fun(); 4

4
Global declaration void main()
{
cout<<”Welcome to C++ Lab”; 5

Main function 5 endl;


}
void fun()
User-defined function
6 {
(see Lab 4)
cout<<”C++ is fun!”; 6
endl;
}

Figure 2: Typical structure of a C++ program.


Table 1: Description of C++ structure.
No. Description
1 Comments: Internal programming documentation. Comments will be ignored
by compiler, and comments do not affects the way the program runs.
2 Preprocessor Directive: Tells the compiler to include input/output stream
library file in the program.
3 Namespace: Namespace is a collection of classess, objects, functions, variables,
constant, and types that are enclosed in the libraries. ‘using namespace’
statement will tell the compiler where to look for names in the libraries.
4 Global declaration: Declaration that are visible to all parts of the program.
5 Main function: The starting point of a program. A group of statements/program
instructions. Every C++ program must have a main().
6 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 sequences.
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 (")
\' 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 Microsoft Visual Studio .NET.

2. On the main menu of Microsoft Development Environment, click File -> New -> Project...

3. On the New Project dialog box, in the Location box (at the bottom of the dialog box),
specify the path of the project, e.g. C:\LAB1 
4. In the Templates list view, click Win32 Console Application

5. In the Name box, give the project a name, e.g., Exercise1


 

6. Click OK.

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++ file, right click Source File Folder on the Solution Explorer

11. On the main menu, click Project -> Add -> New Item...
12. In the Installed Template list, make sure Visual C++ or C++ is selected.

13. On the right pane list view, click C++ File (.cpp).

14. In the Name box, give the file a name, e.g: Exercise.

15. Click Add.

16. Create the contents of the file with:


 

/*Program 1: This program demonstrate a simple C++


program*/

#include <iostream>
using namespace std;

int main()
{
cout << "Welcome to Lab 1!!!\n";
return 0;
}
17. To compile the program, on the main menu, click Build -> Build Exercise1.exe
18. If there is no error(s), the Output windows will show the build process was succeeded

19. To see the output (execute), on the main menu, click Debug-> Start without
Debugging.
20. After viewing the result, press Enter to close the DOS window and return to MSVC.

x error: Syntax is the set of rules for forming valid instructions. Syntax error violates the syntax of a language.
ing message: Message from a compiler advises the programmer that a statement is technically acceptable but might have a po
1.3 Exercises

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

University : Universiti Teknikal Malaysia, Melaka


Faculty : FKE
Programme : BEKC

My name is Sara Binti Abdul Azeez. I am from Sarawak.

2. Write a program that can give outputs as below:

 The background / foreground color for the output screen,its font,


size etc can be set in the output windows property
Self-Review Questions

a) Briefly define each of the terms below.


(a) Hardware

(b) Programmer

(c) Application Software

(d) Source code

(e) Compiler

(f) Linker

(g) High Level Language

(h) Syntax Error


b) Answer the questions below.
(a) What is the C++ preprocessor?

(b) What is the symbol that terminates every C++ statement?

(c) How is the newline character formed?

(d) What is the purpose of including comments in the program?

(e) What is the meaning of compiling?

(f) _____________ converts a source program to a machine language object module.


c) State whether the following statements is TRUE or FALSE

a) The purpose of a library file such as iostream is to store a


program’s source code.
b) The C++ standard function to read data from keyboard is cout
c) In general, C++ statement 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

d) Give the output of the following program:

//Program 1: This program that will print lines


#include <iostream>
using namespace std;
void main()
{
cout << “ONE\tTWO”<<endl;
cout << “THREE\n\n\FOUR\n\n\n”;
cout << “\tFIVE\tSIX”;
cout << endl<<endl<<“SEVEN EIGHT NINE\n\n”;
}

Output:

e) Give the output of the following program:


//Program 2: This program will print asterisk
#include <iostream>
using namespace std;
void main()
{
cout << "o\n*o\n";
cout << "***o\n*******o";
cout << "\n*****";
cout << "*******o\n*******************o\n";
}

Output:

You might also like