Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
27 views

ITP Lab 2

1. The document discusses an introductory lab experiment on C++ programming for a telecommunications engineering course. 2. It introduces the C++ development environment and compiler, explaining how C++ code is compiled into executable code that can run on different processors. 3. It presents a basic "Hello World" C++ program as an example to demonstrate the core components of a C++ program like header files, functions, and code blocks.

Uploaded by

Sahab
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

ITP Lab 2

1. The document discusses an introductory lab experiment on C++ programming for a telecommunications engineering course. 2. It introduces the C++ development environment and compiler, explaining how C++ code is compiled into executable code that can run on different processors. 3. It presents a basic "Hello World" C++ program as an example to demonstrate the core components of a C++ program like header files, functions, and code blocks.

Uploaded by

Sahab
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

DEPARTMENT OF TELECOMMUNICATION ENGINEERING

MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO


INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 2
ST

________________________________________________________________________

Name: _____________________________________________ Roll No: _____________

Score: ____________Signature of the Lab Tutor: _______________ Date: ___________


________________________________________________________________________

To introduce with Features of IDE, Compiler & Compiled Language C++

PERFORMANCE OBJECTIVE
Upon successful completion of this experiment, the student will be able to learn:
An introduction to C++ Compiler

Discussion
1 Compiled Languages and C++
Definitions :
Program – a series of instructions for a computer to execute.
Programming languages: Machine, Assembly, and High Level (e.g. C++,
JAVA, PYTHON etc)

1.1 Why Use a Language Like C++?

At its core, a computer is just a processor with some memory, capable of running tiny
instructions like “store 5 in memory location 23459.” Why would we express a program as a
text file in a programming language, instead of writing processor instructions?
The advantages:
1. Conciseness: programming languages allow us to express common sequences of
commands more concisely. C++ provides some especially powerful shorthands.
2. Maintainability: modifying codeis easier whenit entailsjust afew text edits,instead of
rearranging hundreds of processor instructions. C++ is object oriented (more on that in
Lectures 7-8), which further improves maintainability.
3. Portability: different processors make different instructions available. Programs written
as text can be translated into instructions for many different processors; one of C++’s
strengths is that it can be used to write programs for nearly any processor.

C++ is a high-level language: when you write a program in it,the shorthands are sufficiently
expressive that you don’t need to worry about the details of processor instructions. C++
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 2
ST

________________________________________________________________________
does give access to some lower-level functionality than other languages(e.g. memory
addresses).

1.2 The Compilation Process

A program goes from text files (or source files) to processor instructions as follows:

Object files are intermediate files that represent an incomplete copy of the program: each
source file only expresses a piece of the program, so when it is compiled into an object file,
the object file has some markers indicating which missing pieces it depends on. The linker
takes those object files and the compiled libraries of predefined code that they rely on, fills
in all the gaps, and spits out the final program, which can then be run by the operating
tem(OS).

The compiler and linker are just regular programs. The step in the compilation process in
which the compiler reads the file is called parsing.
In C++, all these steps are performed ahead of time, before you start running a program. In
some languages, they are done during the execution process, which takes time. This is one
of the reasons C++ code runs far faster than code in many more recent languages.
C++ actually adds an extra step to the compilation process: the code is run through a
preprocessor, which applies some modifications to the source code, before being fed to the
compiler. Thus, the modified diagram is:

2 INTRODUCING C++ ENVIRONMENT


The C++ environment is the graphical user environment as it offers the capability of the mouse. Like many
application environments C++ environment is a window with work area and formatting tool bar at the top. The
blue area in the environment is the text editor where the user writes source code (program). The cyan colored
area below the text editor is the message window in which the compiler shows the list of errors, messages and
warnings. The green button at the left top of the text editor is the close button, which when pressed closes
the current source file and the green arrow at the right top is the minimize/maximize button. The name at the
top-center is the name of the source file with extension of the current activated source file.
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 2
ST

________________________________________________________________________
The formatting bar at the top works same as in many other applications. To open a new source file open File
menu and click New, to open an existing source file click Open, to save a source file click Save, to make a
duplicate copy of the source file click Save As, to quit from the C++ environment click Exit or press Alt+X. To
compile the current source program open Compile menu and click Compile or press F9 key while holding down
the Alt Key. To run the current source program open Run menu and click Run or press F9 while holding down
the Ctrl key.

In C++ text editor you can copy, paste, cut or delete certain block of code with some hotkeys. Hotkeys for
copy, paste, cut and delete are Ctrl+Insert, Shift+Insert, Shift+Delete, Ctrl+Delete respectively.

3 BASIC C++ PROGRAM


Whenever you start any objective you first deal with the basics because the basics are roots through which
you can gain command on that objective. So here also we will start with a basic source program. You can write
any C++ source program into the C++ editor or any other text editor like, Notepad or WordPad. Remember
one thing that all the C++ source files have the extension “.cpp”. let us examine this simple C++ source
program named (basic.cpp):
4 Program Code
A. hello.cpp
#include <iostream.h> //header file
#include <conio.h> //header file
void main()
{
clrscr();
cout<<”Hello World”; Program Body
getch();
return 0;}
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 2
ST

________________________________________________________________________
B. first.cpp
#include <iostream> //this is preprocessor directive
using namespace std; //tells the compiler certain objects such as cout are
contained in the standard namespace
int main () //this is the main function
{
cout << "See mom! I wrote my first C++ program\n";
getchar(); //wait for the enter key to be pressed. Try the program without
this line.
return 0;
}

Line-by-Line Explanation:
Comments:// indicates that everything following it until the end of the line is a
comment: it is ignored by the compiler. Another way to write a comment is to put it
between /* and */ (e.g. x = 1 + /*sneaky comment here*/ 1;). A comment of this form
may span multiple lines. Comments exist to explain non-obvious things going on in the
code. Use them: document your code well!
Preprocessor directives: Lines beginning with a pound sign (#) and the keyword include. These are
known as preprocessor directives. The preprocessor directives are the instructions to the part of the
compiler known as preprocessor which includes some extra files (codes) to the basic source program.

Header Files: The files iostream.h and conio.h are known as the header files which contain the
definitions of some functions. The iostream.h header file contains the definition of standard
input/output streams like, cout and cin where as conio.h header file includes the definitions of
function getch(), getche() and others.
As the computer is just a dumb machine and cannot understand anything until you instruct it and the
keywords cout, cin are not understandable to the computer so the header files tells the compile that
cout is this thing and whenever used do this.
The words clrscr(), cout, and getch() are not known to the computer but the definitions of these codes
are written in header files and these definitions tell the computer how to deal with the words clrscr(),
cout, and getch(). Simply speaking the preprocessor directive #include is responsible for including the
contents of the header files into the source file.

Function: main() or int main() {...}is the function as the function is always along with the parentheses.
The main() function is the first executable function in any C++ program. No matter where the main()
function is located always the first precedence goes to the main() function and its contents. The
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 2
ST

________________________________________________________________________
contents of main function are enclosed in curly braces. The void before the main function says that the
function main has no return type value and at the end of the function main() will not return any value.

The braces “{ and }” also known as curly braces, enclose the block of code present in any function. “{“
is known as the opening brace and ”}” is known as closing brace. Opening brace shows the starting of
the main or any function and closing brace shows the ending of the main or any function. The code of
each and every function is always enclosed in the curly braces.
The function clrscr() is used to clear the console screen. As you work repetitively with the console
screen and output your results continuously with out rubbing the previous output your console screen
would be filled with a lot of text and your console screen will not fit your new output correctly and you
also will not be able to examine your output clearly. So the clrscr() function helps to clean the console
screen.

The cout<<”Hello World” tells the computer to print the string constant “Hello World” on the console
screen. However, a computer cannot understand what cout is but the coding written in iostream.h
header file for cout makes the computer understand what does cout mean. The cout is the standard
output stream which directs the flow of data to the console screen. What ever written in the double
quotations in cout statement is printed as it is on the console screen.

The getch() (get character) function waits to get the character from keyboard. If you run your program
without using getch() your program will show the result in just one blink and will vanish out quickly. So
to make the output console screen stop in order to examine the results clearly we use getch()
function. The definition of the function getch() is present in the header file “conio.h”.

The output of basic.cpp on the console screen may look like as shown in below figure;
Strings: A sequence of characters such as Hello, world is known as a string. A string that
is specified explicitly in a program is a string literal.
Escape sequences: The \n indicates a newline character. It is an example of an escape
sequence – a symbol used to represent a special character in a text literal. Here are all
the C++ escape sequences which you can include in strings:

return 0 indicates that the program should tell the operating system it has completed
successfully. This syntax will be explained in the context of functions; for now, just
include it as the last line in the main block.
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 2
ST

________________________________________________________________________
EXERCISE
1. IN THE FOLLOWING TABLE SOME ESCAPE SEQUENCES ARE GIVEN. USE THESE ESCAPE SEQUENCE INTO THE FIRST.CPP
PROGRAM AND LIST THE OUTPUT.

2. FIND WHAT DO WE MEAN BY ERRORS? HOW MANY TYPES OF ERRORS ARE THERE?? ELABORATE THEM.

3. REMOVE THE FOLLOWING FROM THE BASIC.CPP CODE AND SEE WHAT TYPE OF ERRORS ARE
GENERATED.
a. OPENING AND CLOSING BRACES
b. INT MAIN()
c. # INCLUDE <IOSTREAM.H>
DEPARTMENT OF TELECOMMUNICATION ENGINEERING
MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHORO
INTRODUCTION TO PROGRAMMING
(1 SEMESTER, 1ST Year) LAB EXPERIMENT # 2
ST

________________________________________________________________________
LAB SUBMISSION
LAB MUST BE SUBMITTED ON OR BEFORE 7 NOVEMBER 2017
TH

***************** THE END ****************

You might also like