-->

Previous | Table of Contents | Next

Page 475

result code are specified when the function is defined and remain invariable throughout program execution.

Functions are associated with C++ objects as well. But as you will see, the actions performed when an object's function is invoked can automatically differ, perhaps substantially, depending on the specific type of the data structure with which it is associated. This is known as overloading function names. Overloading is related to a second characteristic of C++—the fact that functions can be defined as belonging to C++ data structures, an aspect of the wider language feature known as encapsulation.

In addition to overloading and encapsulation, object-oriented languages allow programmers to define new abstract datatypes (including associated functions) and then derive subsequent datatypes from them. The notion of a new class of data objects, in addition to the built-in classes such as integer, floating-point number, and character, goes beyond the familiar ability to define complex data objects in C. Just as a C data structure that includes, for example, an integer element inherits the properties and functions applicable to integers, so too a C++ class that is derived from another class inherits the parent class's functions and properties. When a specific variable or structure (instance) of that class's type is defined, the class (parent or child) is said to be instantiated.

In the remainder of this chapter, you will look at some of the basic features of C++ in more detail, along with code listings that provide concrete examples of these concepts. To learn more about the rich capabilities of C++, see the additional resources listed at the end of the chapter in the section "Additional Resources."

File Naming

Most C programs will compile with a C++ compiler if you follow strict ANSI rules. For example, you can compile the hello.c program shown in Listing 23.1 with the GNU C++ compiler. Typically, you will name the file something like hello.cc, hello.C, or hello.cxx. The GNU C++ compiler will accept any of these three names.

Differences Between C and C++

C++ differs from C in some details apart from the more obvious object-oriented features. Some of these are fairly superficial, including the following:

Other differences have to do with advanced concepts such as memory management and the scope of reference for variable and function names. Because the latter features especially are

Page 476

used in object-oriented C++ programs, they are worth examining more closely in this short introduction to the language.

Scope of Reference in C and C++

The phrase scope of reference is used to discuss how a name in C, C++, or certain other programming languages is interpreted when the language permits more than one instance of a name to occur within a program. Consider the code in Listing 23.10, which defines and then calls two different functions. Each function has an internal variable called tmp. The tmp that is defined within printnum is local to the printnum function—that is, it can be accessed only by logic within printnum. Similarly, the tmp that is defined within printchar is local to the printchar function. The scope of reference for each tmp variable is limited to the printnum and printchar functions, respectively.

Listing 23.10. Scope of reference example 1.


#include <stdio.h>             /* I/O function declarations */



void printnum  ( int );        /* function declaration      */

void printchar ( char );       /* function declaration      */



main ()

{

   printnum (5);               /* print the number 5        */

   printchar (`a');            /* print the letter a        */

}



/* define the functions called above                        */

/* void means the function does not return a value          */



   void printnum (int inputnum)

{

   int tmp;

   tmp = inputnum;

   printf ("%d \n",tmp);

}



void printchar (char inputchar)

{

   char tmp;

   tmp = inputchar;

   printf ("%c \n",tmp);

}



When this program is executed after compilation, it creates the following output:


5

a

Listing 23.11 shows another example of scope of reference. In this listing, there is a tmp variable that is global—that is, it is known to the entire program because it is defined within the main function—in addition to the two tmp variables that are local to the printnum and printchar functions.

Page 477

Listing 23.11. Scope of reference example 2.


#include <stdio.h>



void printnum  ( int );        /* function declaration             */

void printchar ( char );       /* function declaration             */



main ()

{

   double tmp;                 /* define a global variable          */

   tmp = 1.234;

   printf ("%f\n",tmp);        /* print the value of the global tmp */

   printnum (5);               /* print the number 5                */

   printf ("%f\n",tmp);        /* print the value of the global tmp */

   printchar (`a');            /* print the letter a                */

   printf ("%f\n",tmp);        /* print the value of the global tmp */

}



/* define the functions used above                                  */

/* void means the function does not return a value                  */



void printnum (int inputnum)

{

   int tmp;

   tmp = inputnum;

   printf ("%d \n",tmp);

}



void printchar (char inputchar)

{

   char tmp;

   tmp = inputchar;

   printf ("%c \n",tmp);

}

The global tmp is not modified when the local tmp variables are used within their respective functions, as shown by the output:


1.234

5

1.234

a

1.234

C++ provides a means to specify a global variable even when a local variable with the same name is in scope. The operator :: prefixed to a variable name always resolves that name to the global instance. Thus, the global tmp variable defined in main in Listing 23.11 could be accessed within the print functions by using the label ::tmp.

Why would a language such as C or C++ allow different scopes of reference for the same variable?

The answer to this is that allowing variable scope of reference also allows functions to be placed into public libraries for other programmers to use. Library functions can be invoked merely by knowing their calling sequences, and no one needs to check to be sure that the programmers

Previous | Table of Contents | Next