Previous Page TOC Next Page



- 1 -
Programming and Visual C++


programming


What You'll Learn About


This book is all you need in order to learn the fundamentals of Visual C++, even if you have never programmed a computer. Writing computer programs is different from using them. Most of today's word processors are fairly easy to learn to use, but lots of work goes into creating them. Word processors are just an example of the results of programming efforts. Many programmers spend many hours, days, and weeks writing the programs that people use.

Visual C++ is Microsoft's version of the C++ programming language. Based on the C programming language, C++ is an improved version of C that takes the C language to the next level of evolution of programming languages—those that provide object-oriented programming.

This unit introduces you to the Visual C++ programming language and to programming concepts in general. You will step through the history of C++, see how C++ compares to the C programming language, and learn a little about what to expect when working with Microsoft's Visual C++.

Introduction to Programming




The mystery of programming will quickly disappear when you see that programming is a process of reducing a problem into small steps that the computer can understand.

definition

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.



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.




Prewritten software is often called packaged software.



If you are brand new to the programming process and you want a more in-depth look at how individuals and companies' data processing departments write programs, check out Absolute Beginner's Guide to Programming (Sams Publishing, 1993).


The Programming Process


When you want to write a program, where do you begin? Before leaping to the keyboard and typing, a good programmer goes through several steps:

  1. Decide what is to be done. Before writing a line of code, you need to understand what you are trying to do. Large organizations employ systems analysts to do nothing but understand the business processes and document them in a form that the programming team can understand.

  2. Design the program. This is where the programmer decides how the program will work. There are two sides to this: First, how will the user of the program use it (for example, will it have menus to help the user, will it produce reports, what will they look like)? Second, what is the best way to write it? Again, in a large organization, there might be designers whose job it is simply to decide how the program should work and document this in a way that enables the programmer to easily turn the description into a working program.

  3. The programmer then translates the design of how the program should work into step-by-step detailed instructions that the computer understands, which is sometimes called coding.

  4. The programmer then tests the program thoroughly. The difference between a good program and a bad program is often the amount of effort put into this stage. Even good programmers will make mistakes in coding their programs. It is the programmer's job to find them all.

This book concentrates on the final two steps. Let's look at them in detail.

definition

An editor lets you easily type and change programs.

To give Visual C++ programming instructions to your computer, all you need to do is install the copy of Visual C++ that comes with this book. Appendix A explains how to install the compiler. Your Visual C++ includes an editor and a compiler. An editor is similar to a word processor. It is a program that enables you to type a Visual C++ program into memory, make changes to the program (such as moving, copying, and inserting text), and save the program permanently to a disk file. After you use the editor to type the program, you must compile it with the Visual C++ compiler before you run the program.

definition

A compiler converts your program to low-level machine instructions.

Visual C++ is a compiled language. A C++ compiler (the C++ programming language that is part of your Visual C++) takes your C++ language instructions and translates them into a form that your computer can read. A C++ compiler is a tool that your computer uses to understand the C++ language instructions in your programs. Microsoft's Visual C++ comes with its own editor and integrated programming environment that makes your programming easier to manage.

definition

A program is also known as code.

After you write Visual C++ code, you run it through Visual C++, issue the proper compiling instructions, and run the program. The program's output is the result of the program. The user of the program is the person (sometimes the programmer) who uses the program but cares little (or not at all) about the underlying program that produces the output. Figure 1.1 shows a diagram of the steps necessary to write and execute a Visual C++ program.

definition

A preprocessor reads a program's preprocessor directives to control the compilation.

Notice that your Visual C++ program must be routed through a preprocessor before it is compiled. The preprocessor reads special symbols in the code called preprocessor directives that you enter in the program to control the program's compilation. Visual C++ automatically performs the preprocessor step, so it requires no additional learning on your part except for the preprocessor directives that you put inside your programs. This book teaches you about the most important preprocessor directive, #include, in Unit 4, "Visual C++'s Program Structure."

Figure 1.1. The steps necessary to make a Visual C++ program produce results.

As Figure 1.1 shows, your program must go through one last stage after compiling and before running. It is called the linking, or the link editing, stage. When your program is linked, a program called the linker supplies needed runtime information to the compiled program. You also can combine several compiled programs into one executable program by linking them. Most of the time, however, Visual C++ does all the linking. You rarely have to concern yourself with the linking stage until you write advanced applications.

Exterminating Pests


definition

A syntax error is usually a typing error.

Because you are typing instructions for a machine, you must be very accurate. If you misspell a word, leave out a quotation mark, or make another mistake, Visual C++ informs you with an error message. The most common error is a syntax error, which generally means that you misspelled a word.

When you compile your program and it has mistakes, your compiler tells you what it thinks those mistakes are. The compiler will not send your program through the linker if you made typing mistakes. Therefore, you must read the compiler's error message, figure out what the problem is, and correct the error by returning to the editor and fixing the mistake.

definition

A program mistake is called a bug. When you correct a mistake, you debug the program.

A program error is commonly known as a bug. If you don't understand the error message, you might have to check your compiler's help text or scour your program's source code until you find the offending code line. The process of locating and correcting program errors is called debugging.



Visual C++ can't catch all program bugs. Sometimes logic errors creep into your code. Logic errors are more difficult to find than typing errors because your compiler does not tell you about them. For instance, if you write a program that prints payroll checks, but you tell the computer to print all checks for a negative amount, the computer will obey your instructions. Of course, the instructions themselves are incorrect because they are the result of an error in your logic.

Throughout this book, you are given a chance to find bugs in programs. One or two problems at the end of each unit show you an incorrect program statement or group of statements that lets you hone your debugging skills.



The program that you write tells the computer exactly what to do and how to do it. You must learn how to write programs that contain no errors.



This unit does not contain Stop and Type sections due to its textual nature.


C++ Compared with Other Languages




C++ is an efficient language that relies more than other languages on operators.



If you have not programmed before, do not concern yourself if you do not understand this section. Some of the discussion relates to ideas that are covered in detail in later units.

The really special thing about C++ is that it is an object-oriented programming language. Because this is a complicated concept, you will explore it later in the book, starting in Unit 20.

If you have programmed before, you should understand a little about how C++ differs from other programming languages. C++ is very efficient. It evolved from C, which was designed to allow technical programmers to write the fastest possible code. A C++ program will normally run much faster than a BASIC program.

C++ is a structured language that allows large programs to be built out of small, easy to understand pieces of code. Early languages, such as the original BASIC and FORTRAN, did not have this idea. To write large programs was difficult and the results of trying are described as spaghetti code. Many of the object-oriented features of C++ have been introduced to address this problem. C++ has many of the features of a high-level language (a programming language that uses commands that bear little relationship to the instructions a computer uses), but it also can handle the same programming detail as assembler language (code that directly represents machine instructions, which is a low-level language).

definition

A keyword is a C++ language command.

definition

An operator is a special character that performs a specific function, such as Multiply.

Visual C++ is a small programming language with only 44 commands (called keywords), plus a number more that are Visual C++-specific and not always available in other C++ language implementations. To compensate for its small vocabulary, C++ has one of the largest assortments of operators, such as +, -, and &&. The large number of operators in C++ might tempt programmers to write cryptic programs that have only a small amount of code. You will see throughout this book, however, that making the program more readable is more important than saving some lines of code. This book teaches you how to use the C++ operators to their fullest extent while maintaining readable programs.

C++'s large number of operators (almost equal to the number of keywords) requires an understanding of the order in which operators will be applied. Unlike most other languages, there are many levels of precedence, which helps C++ to decide what you were trying to write. Appendix C includes the complete C++ operator table. As you learn C++, you must learn how this table works. This is not as difficult as it sounds, but its importance can't be overstated.

C++ is a strongly typed language. This means that the language does not automatically change numbers into words and vice versa. Although this might seem inconvenient, it stops accidental mistakes. C was quite careless about this. Languages such as BASIC and PASCAL are very strict. C++ does provide a number of special ways to allow the programmer to easily convert from one type to another.

definition

I/O stands for input/output and refers to data flowing to and from your PC.

C++ also has no input or output statements. (You might want to read that sentence again!) C++ has no commands that perform input or output. This is one of the most important reasons why C++ is available on so many different computers. The I/O statements of most languages tie those languages to specific hardware. BASIC, for instance, has almost 20 I/O commands, some of which write to the screen, some to the printer, some to a modem, and so forth. If you write a BASIC program for a personal computer, chances are good that it cannot run on a mainframe without considerable modification.

C++'s input and output is performed through the abundant use of operators and function calls. With every C++ compiler comes a library of standard I/O functions. I/O functions are hardware-independent, meaning that they work on any device and on any computer that conforms to the C++ standard.

C started out as a language for technical programmers. Although C++ is now considered a general-purpose language, it still carries the baggage of its C ancestry. To master C++ completely, you must be more aware of the way your computer works than most other languages require. You certainly do not have to be a hardware expert, but understanding the internal data representation makes C++ much more usable and meaningful. Other languages such as BASIC or PASCAL provide checks to ensure that you have not made a silly programming error when running your program. These checks slow the program down (and sometimes stop the programmer from deliberately doing something technical with the computer), so C++ decided it could do without them. Beware!

C++ and Personal Computers




The small size of C made it a perfect candidate for personal computers. C++ built on this wide availability.

C was a relatively unknown language until it was placed on the personal computer. With the invention and growth of the personal computer, C blossomed into a worldwide computer language. C++ extends that use on smaller computers. C++ was developed by AT&T in the early 1980s. Most C++ programmers work on a personal computer-based C++ system.

Personal computers typically are called personal computers or PCs from the widespread use of the original IBM PC. The early PCs did not have the memory capacity of the large computers used by government and big business. Nevertheless, PC owners still needed a way to program these machines. BASIC was the first programming language used for PCs. Over the years, many other languages were moved from larger computers to the PC. However, no language was as successful as C in becoming the worldwide standard programming language. C++ seems to be the next standard. C++ first appeared on PCs in 1988, and now C++ is one of the most popular programming languages in use.



Visual C++ follows the AT&T standard closely. After you learn Visual C++, you'll be able to write C++ programs on virtually all computers. Different vendors provide their own tools for editing and compiling the programs, but the C++ language remains the same.

The Visual in Visual C++ also refers to features of the product that are provided for Windows programming. This book does not seek to teach Windows programming. As a beginner, there are more than enough issues to cover. However, more than enough goodies are within the Visual toolset to help the novice programmer. Visual C++ takes advantage of the Windows environment to provide an easy to use programming environment. Among the helpful features are context-sensitive help, a Windows-based editor, and an integrated tool for building programs.

Homework



General Knowledge


  1. What is a program?

  2. What does a computer do without programs?

  3. What is a computer bug?

  4. Name the two kinds of computer bugs.

  5. What is the difference between the two kinds of computer bugs?

  6. Instead of writing your own programs, why don't you purchase every program you need?

  7. Why is C++ called a high-level low-level language?

  8. How does an editor help you create C++ programs?

  9. What does I/O mean?

  10. Why does the C++ language contain no I/O commands?



    There are no What's the Output? or Find the Bug sections here due to this unit's conceptual nature.


    Extra Credit


  11. Technically, your computer cannot run the source code that you write, even though you supply this source code in the Visual C++ programming language. Describe what the computer does to your source code before you can run the programs that you write.

  12. The advantages of writing your own programs include making programs that work just the way you want them to. What disadvantages can you think of to writing your own programs?

Previous Page Page Top TOC Next Page