Teach Yourself Visual C++ 5 in 21 Days

Appendix A: Learning C/C++ Quickly (Part 1)

 
Objectives:

The first chapter for this term will be one of the last in the book. We need to cover the appendices first, to review C and to study some features of C++. The objectives important to this chapter are:

  • creating a console application
  • using the basic Visual C++ interface
  • compiling applications
  • reviewing basic C syntax
  • reviewing pointers
Concepts:

This chapter is meant to review concepts from your previous C courses, as well serve as an introduction to C++ concepts.

In the appendices, we will be creating console (DOS based) applications. This is to review and to warm up to creating the Windows based applications in the numbered chapters, while learning some new syntax in the C++ language.

The book assumes that you are familiar with Windows, in its current incarnations, regarding clicking, using menus, typing in text boxes, and other basic Windows user skills. The tutorial on page 666 (no hidden reference intended) begins with the fact that you must create a directory to house your C++ files before creating them. This is the first thing related to files that a user must do manually. It is not done automatically by the C++ programming environment. No information is given to the reader about how to create this directory, so we will go over this in class using Windows Explorer, which is quite like the old File Manager program. (Note: I said Windows Explorer, not Internet Explorer. There is a big difference.)

After creating the directory for your files (you may want one for each new project, when you do this professionally), you can start the programming environment. The menus depicted on page 667 may be different from those we will actually see in the lab, and from those you see at home (if you have purchased the software). I bought the Microsoft Visual Studio package, so I have a few more choices on my menus (the academic pricing is quite reasonable). The important thing is to find your way to "Microsoft Visual C++ 5.0". This should take you to a screen like the one at the bottom of page 667.

You begin creating a project with the familiar File menu, New command. The screen that comes up next is shown on page 668 (again, your screen may vary, depending on what Microsoft packages are installed). Note that the screen has four possible subscreens available, each of which may be selected from the tabs on the screen. Novell refers to this format as a sequence of cards, using the card file metaphor. Microsoft seems to just refer to them as tabs. The important thing, mentioned in the text, is to make sure you have the right tab selected before taking the next step. Your purpose in coming to this screen is to tell Visual C++ what kind of project you are creating, what its name is, and where to store the files. Once that is done, you click the OK button, and you will be taken to the screen on page 669.

You are now in a workspace, a sort of view of the project files that the computer is aware of while you are creating or modifying the project. The screen on page 669 shows that it is looking at the classes for its project. The word class is very special in C++, and we are introduced to it in the next chapter. For now, assume it is a necessary part of the project.

The next couple of pages walk you through creating a C++ source code file in this project. Note that you should always take care to select the right kind of object from any tab screen when creating a new object, else the project will not work.

Note that the source code file has a new extension, cpp, which stands for C Plus Plus. As you type the source code in it, the code will become color coded as the interface recognizes what you type. Remarks show up as green text on most systems. Remarks, by the way, are noted a new way in C++. You can still use the notation you know about slashes and stars, but the C++ way is to precede a comment/remark with a double slash.

The next difference you will notice is that we no longer use the stdio.h file in the #include section. It is replaced by the iostream.h file, which has the standard C++ functions defined in it.

Compiling a C++ file can still be done the old fashioned way, from a command line, but the preferred method is to use the built in compiler in our integrated programming environment. In other words, use the menus to tell the computer to compile the source code. On page 672, you are instructed to select the Build command from the Build menu. This may often work, but I prefer to select Rebuild All.

The text cautions you to remember that you are programming in C, the most case sensitive language available. (It can't help it, it was born in UNIX.) So, if you have compiler errors, suspect that there are capitalization errors.

Another new and odd looking code difference you will see in this program is the use of cout. This is discussed on page 676. Essentially, think of cout as an access route to your standard output device. You send strings to it using the redirection character, <<, which you will remember means "send input to". The syntax is to type cout first, then the redirection character, then the string you want sent to cout. You can follow the first string with a second, a third and so on, but each string must be preceded with a new redirection character.

The next few pages of the text review doing math with variables. Remember that a variable must be declared before it can be used, and that the C standard is to declare all variables before any executed lines of code in a function. (That seems oddly worded? More surprises in Appendix B.)

The chapter then reviews creating a function. A function must be prototyped before it is called. This is the equivalent to declaring a variable, formally stating what this object is and how it should behave. Speaking of objects, our author takes a certain approach to naming objects, which you will see in many texts on C++ and Visual Basic: when he creates an int object, he starts the name of that object with a lower case i; when he creates a char, he starts the name of that object with a lower case c, and so on. This is not a syntax requirement, only a standard. Since it is a standard that is widely taught and followed, you should be aware of it and able to use it.

Back to functions, an example appears on page 683 of prototyping a function, then calling it in main. Note that this example is incomplete. The body of the function is not shown, and is written on the next page.

On page 685, the author discusses passing parameters to a function. Remember that a function call such as:

	MyFunction( var1, var2)


passes the values of the variables to the function called, not the variables themselves. This is the reason that the receiving function must have variables of its own to hold those values, and also why the receiving function cannot directly change the values in the original variables. At least, not when we do it this way.

The author observes that variables declared in a function are by default local variables, known only to that function. Also called automatic variables, they are not known to other functions, and in fact, cease to exist when the function they are declared in ceases to run.

The simplest way to make variables known and accessible to multiple functions is discussed on page 692. Global variables are declared outside of any function, usually before the main function, so that all functions that follow in the program know about them. The author cautions against their use on page 695, because of the high probability that they will cause a problem in any project of sufficient size. A reason to use this language is to be able to divide the project into tasks that can be handled by individuals working in a group. It is inevitable that you will eventually misuse a global variable created by someone else without meaning to, causing your function or theirs to fail. If you do not allow global variables in the project, you will avoid the problem. At the very least, require that any globals be documented for all programmers to know about, and require that they be approved by the project manager before being implemented.

In order to access a variable that is not global, we pass the address of a variable to a function, instead of its value. Addresses are discussed on page 696. An address is a location in memory, usually expressed as a hexadecimal number. A variable actually stored in one or more memory addresses, since larger variable types take more memory. The address of a given variable is the address of the first byte where it is stored.

A pointer is a variable that holds an address. Pointers usually point to the address where a variable is stored. You should review the section starting on page 698 about pointers, how they are declared and used. Specifically, review the use of the star (indirection operator) and ampersand (address of operator). Remember that the star has more uses in C (and C++) than any other character.

To close this chapter, the author adds a bit about conditional logic (the if operator) and strings. The simple if-else structure is reviewed and we are reminded that strings are just arrays of characters in C. Review the strcpy function on page 706, whose notation is

	strcpy(pointer to an array, string to place in the array)

To use string functions, include the string header (string.h) file in your programs.