DOC

IDE Assignment 1

By Doris Harrison,2014-05-28 23:57
7 views 0
IDE Assignment 1

    IDE Practical Assignment 1

This assignment is meant to get you acquainted with

    ; what files and filetypes are used in a C++ program

    ; the Windows Command Prompt

    ; compiling and static linking

    In this assignment we will develop some simple programs with the Command Prompt, outside of C++Builder. This is meant to show that C++Builder is just a helpful tool, but not a necessity for developing a program in C++.

Introduction to the Command Prompt

    Usually we use the mouse to do things like starting a program, or copying files. The Command Prompt is an alternative way to give commands to the Operating System. It has a window like the one shown below:

    When working with the Command Prompt, some new notions are important which are irrelevant for graphical interfaces:

    ; The prompt.

    This is a text at the beginning of a line, just to show that a new command can be

    entered. The prompt ends with the character >. In the screenshot above, the

    prompt is the text C:\Documents and Settings\P.Boots>. Each time when

    a command is finished, the prompt is shown again.

    ; The current drive.

    This is the disk where you are working at the moment. The disk is denoted by a

    letter with a colon, usually C: or D:. The current drive is shown in the prompt.

    You can change the current drive by typing its name, a colon and ENTER:

     D:

    ; The current directory.

    This is the directory (folder) where you are at the moment. The current directory

    is also shown in the prompt.

    ; Pathnames.

    IDE Assignment 1 Page 2

    A pathname is a filename that is preceded by zero or more directorynames (separated by the backslash ‘\’), and optionally a driveletter.

    A pathname is absolute if it starts with a \ (optionally preceded by a driveletter

    and a colon), for example C:\Program Files\Borland.

    A pathname that does not start with a \ is relative, like BDS\4.0\Bin.

    An absolute pathname always starts in the root of the drive, while a relative pathname starts in the current directory.

    There are two special directorynames:

    ; the name . (a single dot) is used for the current directory,

    ; the name .. (two dots) is used for the so called parent-directory (the

    directory where the current directory is in).

    ; The environment.

    In a Command Prompt window, variables can be used, comparable with (but not the same as) variables in a C++ program. All these variables together are called the environment.

    The value of an environment variable is not a number, but it is always a string (a text, consisting of a number of characters).

    You can get the value of a variable by surrounding the name of the variable with percent-characters, for example %TEMP%. If you use %TEMP% on the command

    line, these 6 characters will be replaced by the value of variable TEMP.

    With the command SET you can easily create a new environment variable or change the value of an existing variable:

     SET VAR1=ABC ; the variable VAR1 gets the value ABC

    You can see all the environment variables by using the command SET without

    any arguments:

     SET

    ; The (search) path.

    One of the most important variables in the environment is PATH. It contains a

    number of directory-names, separated by semicolons (;).

    This variable has a very special meaning: when a user types a command (for example PROG), then first the current directory is searched and after that one by one all directories from the PATH are searched until the commandname is found.

    The first program found in this way will be executed.

    Suppose PATH=C:\Borland;C:\Drivers;C:\Temp. If you type PROG as a commandname, then the following directories will be searched: ; current directory

    ; C:\Borland

    ; C:\Drivers

    ; C:\Temp

    If either of the files PROG.EXE, PROG.COM or PROG.BAT are found, then that

    program will be started.

    ; Wildcards.

    Sometimes it is cumbersome to write complete pathnames on the command line: it is a lot of work and there is a big risk of making errors. This can be solved by using wildcards. These are special symbols that, when used in a filename,

IDE Assignment 1 Page 3

    replace 1 or more normal characters. The 2 most important wildcards are:

    ; *: this replaces 0 or more random characters

    ; ?: this replaces exactly one random character.

    Wildcards are not only used in the Command Prompt, but also on other places

    like in some searching engines on Internet (Google also uses the * wildcard, but

    slightly different than described above).

    You can also use the TAB-key to browse through all files and folders in a

    directory.

    The list of all available commands in a Command Prompt can be found by typing HELP.

    The most important commands are:

    ; cd: (Change Directory) to go to another directory

    ; dir: to see the content of the current directory

IDE Assignment 1 Page 4

    Tasks to perform

    Perform the following tasks. Although you need to have C++Builder installed on your

     use it in this assignment. computer, we do not

Task 1 Working with the Command Prompt

    Start the Command Prompt. This can be done in several ways: ; Via the Start button on the taskbar: choose All Programs, Accessories and then

    Command Prompt.

    ; Via the Start button on the taskbar: type cmd (in Run).

    ; From the Windows folder C:\Windows\System32, start the program Cmd by

    double-clicking on it.

    Note: Do not use Command.com to start the Command Prompt, because this is a simpler version that (among other things) does not support long filenames. For example, the folder C:\Program Files will be shown as C:\PROGRA~1.

Task 2 Making a console application in one unit

    One of the simplest programs that we can make in the language C++ is Program 2-2 from the PRO book (Starting out with C++):

     #include

     using namespace std;

     int main()

     {

     cout << "Programming is " << "great fun!";

     return 0:

     }

    This program will print the text ‘Programming is great fun!on the commandline.

    Type the above program with Notepad in a file with the name test.cpp and put it

    in a new folder with the name IDE\Assignment1\ConApp1.

    In the Command Prompt, go to that same directory and compile/link the program with bcc32 from the Command Prompt. Use no option.

    Verify that the files test.obj (the object file) and test.exe (the executable file)

    are added to the directory IDE\Assignment1\ConApp1 (sometimes Explorer does

    not show file extensions; in that case you can use the command DIR in the

    Command Prompt to see the content of the current directory). Run the program test.exe to verify that it runs without problems. (you can do this by typing test in the Command Prompt, or by double clicking on the file test.exe

    in the Explorer).

Task 3 Making a console-application with multiple units.

    We will now make a console application that consists of multiple files (units). Get the file Histogram.cpp from SharePoint (IDE, week 1) and put it in the new directory IDE\Assignment1\ConApp2.

    Open a Command Prompt and compile/link the program with bcc32. Verify again

    that an object file is created (as a result of the compiling step) and an executable file

IDE Assignment 1 Page 5

    (as a result of the linking step). You can ignore warnings because they will not affect the execution of the program.

    Verify that the executable runs without problem.

    Now divide the code over 3 files (units) which all have to be placed in the same directory:

    ; Leave only the function main in Histogram.cpp

    ; Put the function DrawHistogram in a file with the name Draw.cpp. Also

    produce the corresponding header file Draw.h which should contain the

    declaration of that function (only the very first line of the function, ending with

    a semicolon).

    ; Put the function MakeRandomNumber in a file with the name Make.cpp,

    which also needs a corresponding header file.

    Remember that every function must be declared (by adding the correct #include-

    line) before it is used.

    Find out yourself when the iostream headerfile has to be included and when the

    std namespace is used.

First, compile every unit (Histogram, Draw and Make) separately into object files.

    For this, use the command bcc32 c (-c is an option of bcc32 that has as a result

    that bcc32 will only compile the file and not link it).

    Verify after each compile that a new object file is created, but no executable file.

    Next, link all object files into one single executable file. Use again the command bcc32 (now without the c option) and specify all the object files instead of .CPP files.

    Verify that an executable file is created and that it is running in the same way as before.

Task 4 Making a VCL Forms application

    Get the files Project1.cpp, Unit1.cpp, Unit1.h and Unit1.dfm from

    SharePoint and put them in the new directory IDE\Assignment1\WinApp.

    These files together make up a complete C++ application that uses the VCL library. Such applications should be compiled using the extra option tWV:

     bcc32 tWV

    Compile and link these files separately like in the previous task, but now use the extra option tWV.

    Run the program and verify that it works normally.

    Adapt the file Unit1.dfm such that the text in the button is changed into OK, and also the font size of that text is changed to 21 instead of 10.

    Change the name of Unit1 into TextUnit (change the names of all files belonging

    to Unit1)

IDE Assignment 1 Page 6

    Add another button to the window with the name Button2, just below the existing Button1 and with the same size as Button1 (look carefully how Button1 is

    programmed, and do more or less the same for Button2; check all files of the unit: .CPP. .H and .DFM).

    When the new button is clicked, a sound must be played. You can use the standard

    function Beep to produce a sound with a certain length and frequency. The specification of Beep is shown below:

The Beep function generates simple tones on the speaker.

    BOOL Beep(

     DWORD dwFreq, // sound frequency, in hertz

     DWORD dwDuration // sound duration, in milliseconds

     );

    Parameters

dwFreq

    Windows NT:

    Specifies the frequency, in hertz, of the sound. This parameter must be in the range 37 through 32,767 (0x25 through 0x7FFF).

    Windows 95:

    The parameter is ignored.

dwDuration

    Windows NT:

    Specifies the duration, in milliseconds, of the sound.

    One value has a special meaning: If dwDuration is - 1, the function operates asynchronously and produces sound until called again. Windows 95:

    The parameter is ignored.

Return Value

    If the function succeeds, the return value is TRUE.

    If the function fails, the return value is FALSE. To get extended error information, call GetLastError.

Example

    In order to play a sound of 4000 Hz during 2 seconds, you have to call the function Beep as follows:

     Beep(4000, 2000);

    The first value, 4000, means that the frequency is 4000 Hz and the second value, 2000, means that the sound will play for 2000 milliseconds (2 sec).

    In order to use function Beep, the file Windows.h must be included first.

    Let the function Beep be called when the new button is clicked.

Compile and link the program again (using the same steps as before) and run it.

    We should now hear a beep when the new button is clicked (and the text in the label

    should still be changed when the first button is clicked, like before).

    Show the results of tasks 3 and 4 to the teacher.

Report this document

For any questions or suggestions please email
cust-service@docsford.com