📄 0475-0477.html
字号:
<HTML>
<HEAD>
<TITLE>Developer.com - Online Reference Library - 0672311739:RED HAT LINUX 2ND EDITION:C and C++ Programming</TITLE>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!-- ISBN=0672311739 //-->
<!-- TITLE=RED HAT LINUX 2ND EDITION //-->
<!-- AUTHOR=DAVID PITTS ET AL //-->
<!-- PUBLISHER=MACMILLAN //-->
<!-- IMPRINT=SAMS PUBLISHING //-->
<!-- PUBLICATION DATE=1998 //-->
<!-- CHAPTER=23 //-->
<!-- PAGES=0455-0486 //-->
<!-- UNASSIGNED1 //-->
<!-- UNASSIGNED2 //-->
<P><CENTER>
<a href="0472-0474.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0478-0480.html">Next</A>
</CENTER></P>
<A NAME="PAGENUM-475"><P>Page 475</P></A>
<P>result code are specified when the function is defined and remain invariable throughout
program execution.
</P>
<P>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.
</P>
<P>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.
</P>
<P>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."
</P>
<H4><A NAME="ch23_ 20">
File Naming
</A></H4>
<P>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.
</P>
<H4><A NAME="ch23_ 21">
Differences Between C and C++
</A></H4>
<P>C++ differs from C in some details apart from the more obvious object-oriented features.
Some of these are fairly superficial, including the following:
</P>
<UL>
<LI> The ability to define variables anywhere within a code block rather than always at
the start of the block
<LI> The addition of an
enum datatype to facilitate conditional logic based on case values
<LI> The ability to designate functions as
inline, causing the compiler to generate another copy of the function code at that point in the program rather than a call to
shared code
</UL>
<P>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
</P>
<A NAME="PAGENUM-476"><P>Page 476</P></A>
<P>used in object-oriented C++ programs, they are worth examining more closely in this
short introduction to the language.
</P>
<H4><A NAME="ch23_ 22">
Scope of Reference in C and C++
</A></H4>
<P>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.
</P>
<P>Listing 23.10. Scope of reference example 1.
</P>
<!-- CODE //-->
<PRE>
#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);
}
</PRE>
<!-- END CODE //-->
<P>When this program is executed after compilation, it creates the following output:
</P>
<!-- CODE SNIP //-->
<PRE>
5
a
</PRE>
<!-- END CODE SNIP //-->
<P>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.
</P>
<A NAME="PAGENUM-477"><P>Page 477</P></A>
<P>Listing 23.11. Scope of reference example 2.
</P>
<!-- CODE //-->
<PRE>
#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);
}
</PRE>
<!-- END CODE //-->
<P>The global tmp is not modified when the local
tmp variables are used within their respective functions, as shown by the output:
</P>
<!-- CODE SNIP //-->
<PRE>
1.234
5
1.234
a
1.234
</PRE>
<!-- END CODE SNIP //-->
<P>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.
</P>
<P>Why would a language such as C or C++ allow different scopes of reference for the same
variable?
</P>
<P>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
</P>
<P><CENTER>
<a href="0472-0474.html">Previous</A> | <a href="../ewtoc.html">Table of Contents</A> | <a href="0478-0480.html">Next</A>
</CENTER></P>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -