ITP LAB 1

Embed Size (px)

Citation preview

  • 7/25/2019 ITP LAB 1

    1/6

    DEPARTMENT OF TELECOMMUNICATION ENGINEERING

    MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHOROINTRODUCTION TO PROGRAMMING

    (1STSEMESTER, 1STYear) LAB EXPERIMENT # 1

    ________________________________________________________________________

    Name: _____________________________________________ Roll No: _____________

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

    ________________________________________________________________________

    PERFORMANCE OBJECTIVE

    Upon successful completion of this experiment, the student will be able to learn:

    An introduction to C++ Compiler

    Discussion1 Compiled Languages and C++

    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 ofcommands 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 dont need to worry about the details of processor instructions. C++

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

    addresses).

    1.2 The Compilation Process

    Introduction to Compiler & Compiled Language C++, Features of IDE

  • 7/25/2019 ITP LAB 1

    2/6

    DEPARTMENT OF TELECOMMUNICATION ENGINEERING

    MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHOROINTRODUCTION TO PROGRAMMING

    (1STSEMESTER, 1STYear) LAB EXPERIMENT # 1

    ________________________________________________________________________

    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 thetop-center is the name of the source file with extension of the current activated source file.

    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 Compilemenu and click Compile or press F9key while holding down

    theAltKey. To run the current source program open Runmenu and click Runor press F9while holding down

    the Ctrlkey.

  • 7/25/2019 ITP LAB 1

    3/6

    DEPARTMENT OF TELECOMMUNICATION ENGINEERING

    MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHOROINTRODUCTION TO PROGRAMMING

    (1STSEMESTER, 1STYear) LAB EXPERIMENT # 1

    ________________________________________________________________________

    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. Rememberone 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 (basic.cpp)

    #include //header file

    #include //header file

    void main()

    {

    clrscr();

    cout

  • 7/25/2019 ITP LAB 1

    4/6

    DEPARTMENT OF TELECOMMUNICATION ENGINEERING

    MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHOROINTRODUCTION TO PROGRAMMING

    (1STSEMESTER, 1STYear) LAB EXPERIMENT # 1

    ________________________________________________________________________

    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 aspreprocessorwhich 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 thedefinitions 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, cinare not understandable to the computer so the header files tells the compile that

    coutis 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 theparentheses.

    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

    contents of main function are enclosed in curly braces. The voidbefore 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

  • 7/25/2019 ITP LAB 1

    5/6

    DEPARTMENT OF TELECOMMUNICATION ENGINEERING

    MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHOROINTRODUCTION TO PROGRAMMING

    (1STSEMESTER, 1STYear) LAB EXPERIMENT # 1

    ________________________________________________________________________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 programwithout 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.cppon 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

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

    EXERCISE1. IN THE FOLLOWING FIGURE SOME ESCAPE SEQUENCES ARE GIVEN .USE THESE ESCAPE SEQUENCE INTO THE

    BASIC.CPP PROGRAM AND LISTTHE OUTPUT.

    2. FINDWHATDOWEMEANBYERRORS?

    3.

    REMOVETHEFOLLOWINGFROMTHEBASIC.CPPCODEANDSEEWHATTYPESOFERRORSAREGENERATED.

    a. OPENINGANDCLOSINGBRACES

    b. INTMAIN()

    c.

    #INCLUDE

  • 7/25/2019 ITP LAB 1

    6/6

    DEPARTMENT OF TELECOMMUNICATION ENGINEERING

    MEHRAN UNIVERSITY OF ENGINEERING & TECHNOLOGY, JAMSHOROINTRODUCTION TO PROGRAMMING

    (1STSEMESTER, 1STYear) LAB EXPERIMENT # 1

    ________________________________________________________________________

    LABSUBMISSIONLABMUSTBESUBMITTEDWITHINONEWEEK

    *****************THEEND****************