C++ NOTES

BY

G T CHANDRASEKHAR

Email Address: gtcsekhar@yahoo.com


                                                                                                                                  some Important C++ Programmes

A program is a list of detailed instructions. A program is simply a list of instructions that tells the computer what to do. Computers are only dumb machines. They cannot think; they can only execute your orders within the programs that you write. Without programs, computers are worthless.

A program is to a computer what a recipe is to a cook. A recipe is nothing more than a program (a list of instructions) that tells the cook exactly what to do. The recipe's end result is a finished dish, and the end result of a computer program is an application such as a word processor or a payroll program. By itself, your computer does not know how to be a word processor. By following a list of detailed programming instructions written by a programmer, however, the computer performs the actions necessary to do word processing.

If you want your computer to help you with your household budget, keep track of names and addresses, or play a game of solitaire, you have to supply a program so that it knows how to do those things. You can either buy the program or write one yourself.

There are several advantages to writing your own programs. When you write your own programs, they do exactly what you want them to (you hope!). Although it would be foolish to try to write every program that you need to use (there is not enough time, and there are many good programs on the market), some applications are so specific that you simply cannot find a program that does exactly what you want. >/p>

Some companies hire a staff of programmers to write the programs used within the company. When a company acquires a computer system, that company does not want to change the way it does business. Therefore, the programmers design and write programs that are exactly right for the company. Even when a company buys a set of programs, they often want to write new programs or change the prewritten software to fit their business better.

Program
If you want to insert a character, position the cursor where you want to insert and type the new text. The first time you do this, you will be in insert mode, meaning that the characters to the right of those you type shift to the right and make room for new ones. If you press the Insert key, Visual C++ changes to overtype mode, meaning that the new characters you type replace those on the screen. The Insert key toggles you back and forth between the two modes. The OVR indicator on the status bar tells you what mode the editor is in currently.

// Filename: EDIT.CPP 

#include 


void main()

{

   cout << "Visual C++ is easy!"

}



Program
Type the following program. When typing the program, be sure to press Enter at the end of each line (including the very last line). Type as accurately as possible so that no mistakes appear later. The program takes more lines than will fit in the workbench editing window, so you'll see the workbench scroll downward when you fill the open window.

Project 1 Listing. The Visual C++ project program.

// Filename: PROJECT1.CPP

// Prints the first 20 odd, then even, numbers.

// Once done, it prints them in reverse order.

#include 

void main()

  {

    int num;   // The for loop control variable

    cout << "The first 20 odd numbers:\n";

    for (num = 1; num < 40; num += 2)

      { cout << num << ' '; }

    cout << "\n\nThe first 20 even numbers:\n";

    for (num = 2; num <= 40; num += 2)

      { cout << num << ' '; }

    cout << "\n\nThe first 20 odd numbers in reverse:\n";

    for (num = 39; num >= 1; num -= 2)

      { cout << num << ' '; }

    cout << "\n\nThe first 20 even numbers in reverse:\n";

    for (num = 40; num >= 2; num -= 2)

      { cout << num << ' '; }

    return;

  }

When you finish typing the complete program, you can use PageUp, PageDown, and the arrow keys to scroll the top of the program back into view.

Step 3: Compile and run the program.

Before running the program, you must compile it. Choose Project | Build, (Shift+F8) which is an alternative to Project | Execute when you know the program needs to be made.

If errors appear, the status bar will display an error count. Pressing F4 guides you through the code and leaves you near where the errors are. You can fix any errors that might appear. Recompile the program when you type it exactly as the listing shows.

If no errors appear, the error count will be zero and you can then press Ctrl+F5 to execute the program. Here is what you'll see in the QuickWin window:


The first 20 odd numbers: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39

The first 20 even numbers: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40

The first 20 odd numbers in reverse: 39 37 35 33 31 29 27 25 23 21 19 17 15 13 11 9 7 5 3 1

The first 20 even numbers in reverse: 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2

Close the output window and return to the workbench's editing window (use Alt+Tab or click on the workbench window if you do not directly return to the workbench).



Step 4: Save your work.

If you exit Visual C++ without saving your program, you'll lose the program and have to reenter it in order to see the results again. Therefore, you'll want to save your programs to a disk file.

---------------------------------------------------------------------------------

All of the programs in this book, including the one shown in Listing 1, are stored on the enclosed program disk. You don't have to save this listing unless you want the practice, because it is already on the disk.

---------------------------------------------------------------------------------

To save a program, select Alt+F,S (for File Save). You'll see the File Save dialog box appear on the screen.

Type a program name along with a path name if you want to store the program in a subdirectory. All Visual C++ programs should end with the .CPP filename extension.

Step 5: Exit Visual C++.

Select Alt+F4 (for File Exit) to close Visual C++. If you did not save your program, Visual C++ tells you with a final dialog box that gives you one last chance to save the program.

After saving the program, you can exit Visual C++ and switch off your PC. You should always return to DOS with Windows 3.1 or shut down your PC under Windows 95 before powering off your computer.