⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chapter03.html

📁 Thinking in c++ 2nd edition,c++编程思想(第2版)
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<!--
This document was converted from RTF source: 
By rtftohtml 4.19
See http://www.sunpack.com/RTF
Filename:TIC2Vone.rtf
Application Directory:C:\TOOLS\RTF2HTML\
Subject:
Author:Bruce Eckel
Operator:Bruce Eckel
Document Comments:
Version Comments:
Comments:
Keywords:
Translation Date:09/27/2001
Translation Time:05:25:26
Translation Platform:Win32
Number of Output files:22
This File:Chapter03.html
SplitDepth=1
SkipNavPanel=1
SkipLeadingToc=1
SkipTrailingToc=1
GenContents=1
GenFrames=1
GenIndex=1
-->
<HEAD lang="en"><META http-equiv="Content-Type" content="text/html">
<TITLE>3: The C in C++</TITLE>
</HEAD>

<BODY  BGCOLOR="#FFFFFF"><DIV ALIGN="CENTER">
  <a href="http://www.MindView.net">
  <img src="mindview-head.gif" alt="MindView Inc." BORDER = "0"></a>
  <CENTER>
    <FONT FACE="Verdana" size = "-1">
    [ <a href="README-HTML.txt">Viewing Hints</a> ]
    [ <a href="http://www.mindview.net/CPPServices/SolutionGuide.html">Exercise Solutions</a> ]
    [ <a href="http://www.mindview.net/ThinkingInCPP2e.html">Volume 2</a> ]
    [ <a href="http://www.mindview.net/MailingList.html">Free Newsletter</a> ] <br>
    [ <a href="http://www.mindview.net/CPPServices/#PublicSeminars">Seminars</a> ]
    [ <a href="http://www.mindview.net/CPPServices/#SeminarsOnCD">Seminars on CD ROM</a> ]
    [ <a href="http://www.mindview.net/CPPServices/#ConsultingServices">Consulting</a> ]
    </FONT>
  <H2><FONT FACE="Verdana">
  Thinking in C++, 2nd ed. Volume 1</FONT></H2></FONT>
  <H3><FONT FACE="Verdana">&copy;2000 by Bruce Eckel</FONT></H3></FONT>
  
    <FONT FACE="Verdana" size = "-1">
     [ <a href="Chapter02.html">Previous Chapter</a> ] 
    [ <a href="Contents.html">Table of Contents</a> ] 
    [ <a href="DocIndex.html">Index</a> ]
     [ <a href="Chapter04.html">Next Chapter</a> ] 
    </FONT>
    
  </CENTER>
  </P></DIV><A NAME="_Toc472654741"></A><FONT FACE = "Verdana"><H1 ALIGN="LEFT">
3: The C in C++</H1></FONT>
<A NAME="Heading99"></A><FONT FACE = "Verdana"><H1 ALIGN="LEFT">
</H1></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Verdana" SIZE=4>Since C++ is based on C, you must
be familiar with the syntax of C in order to program in C++, just as you
</FONT><BR><FONT FACE="Verdana" SIZE=4>must be reasonably fluent in algebra in
order to tackle calculus.</FONT><BR></P></DIV>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">If you&#8217;ve never seen
<A NAME="Index418"></A><A NAME="Index419"></A>C before, this chapter will give
you a decent background in the style of C used in C++. If you are familiar with
the style of C described in the first edition of Kernighan &amp; Ritchie (often
called K&amp;R C<A NAME="Index420"></A>), you will find some new and different
features in C++ as well as in Standard C. If you are familiar with Standard C,
you should skim through this chapter looking for features that are particular to
C++. Note that there are some fundamental C++ features introduced here, which
are basic ideas that are akin to the features in C or often modifications to the
way that C does things. The more sophisticated C++ features will not be
introduced until later chapters.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">This chapter is a fairly fast coverage of
C constructs and introduction to some basic C++ constructs, with the
understanding that you&#8217;ve had some experience programming in another
language. <A NAME="Index421"></A>A more gentle introduction to C is found in the
<A NAME="Index422"></A>CD ROM packaged in the back of this book, titled
<I>Thinking in C: Foundations for Java &amp; C++</I> by Chuck Allison (published
by MindView, Inc., and also available at www.MindView.net). This is a seminar on
a CD ROM with the goal of taking you carefully through the fundamentals of the C
language. It focuses on the knowledge necessary for you to be able to move on to
the C++ or Java languages rather than trying to make you an expert in all the
dark corners of C (one of the reasons for using a higher-level language like C++
or Java is precisely so we can avoid many of these dark corners). It also
contains exercises and guided solutions. Keep in mind that because this chapter
goes beyond the <I>Thinking in C </I>CD, the CD is not a replacement for this
chapter, but should be used instead as a preparation for this chapter and for
the
book.</FONT><A NAME="_Toc462979737"></A><A NAME="_Toc472654742"></A><BR></P></DIV>
<A NAME="Heading100"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Creating functions<A NAME="Index423"></A><A NAME="Index424"></A></H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In old (pre-Standard) C, you could call a
function with any number or type of arguments and the compiler wouldn&#8217;t
complain. Everything seemed fine until you ran the program. You got mysterious
results (or worse, the program crashed) with no hints as to why. The lack of
help with argument passing and the enigmatic bugs that resulted is probably one
reason why C was dubbed a &#8220;high-level assembly
language<A NAME="Index425"></A>.&#8221; Pre-Standard C programmers just adapted
to it.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Standard C and C++ use a feature called
<A NAME="Index426"></A><A NAME="Index427"></A><I>function prototyping</I>.<I>
</I>With function prototyping, you must use a description of the types of
arguments when declaring and defining a function. This description is the
&#8220;prototype.&#8221; When the function is called, the compiler uses the
prototype to ensure that the proper arguments are passed in and that the return
value is treated correctly. If the programmer makes a mistake when calling the
function, the compiler catches the mistake.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Essentially, you learned about function
prototyping (without naming it as such) in the previous chapter, since the form
of function declaration in C++ requires proper prototyping. In a function
prototype, the argument list contains the types of arguments that must be passed
to the function and (optionally for the declaration) identifiers for the
arguments. The order and type of the arguments must match in the declaration,
definition, and function call. Here&#8217;s an example of a function prototype
in a declaration:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>int</font> translate(<font color=#0000ff>float</font> x, <font color=#0000ff>float</font> y, <font color=#0000ff>float</font> z);</PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You do not use the same form when
declaring variables in function prototypes as you do in ordinary variable
definitions. That is, you cannot say: <B>float x, y, z</B>. You must indicate
the type of <I>each</I> argument. In a function declaration, the following form
is also acceptable:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>int</font> translate(<font color=#0000ff>float</font>, <font color=#0000ff>float</font>, <font color=#0000ff>float</font>);</PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Since the compiler doesn&#8217;t do
anything but check for types when the function is called, the identifiers are
only included for clarity when someone is reading the code.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In the function definition, names are
required because the arguments are referenced inside the
function:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>int</font> translate(<font color=#0000ff>float</font> x, <font color=#0000ff>float</font> y, <font color=#0000ff>float</font> z) {
  x = y = z;
  <font color=#009900>// ...</font>
} </PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">It turns out this rule applies only to C.
In C++, an argument may be unnamed
<A NAME="Index428"></A><A NAME="Index429"></A>in the argument list of the
function definition. Since it is unnamed, you cannot use it in the function
body, of course. Unnamed arguments are allowed to give the programmer a way to
&#8220;reserve space in the argument list.&#8221; Whoever uses the function must
still call the function with the proper arguments. However, the person creating
the function can then use the argument in the future without forcing
modification of code that calls the function. This option of ignoring an
argument in the list is also possible if you leave the name in, but you will get
an annoying warning message about the value being unused every time you compile
the function. The warning is eliminated if you remove the name.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">C and C++ have two other ways to declare
an argument list. If you have an
<A NAME="Index430"></A><A NAME="Index431"></A><A NAME="Index432"></A><A NAME="Index433"></A>empty
argument list, you can declare it as <B>func(&#160;)</B> in C++, which tells the
compiler there are exactly zero arguments. You should be aware that this only
means an empty argument list in C++. In C it means &#8220;an indeterminate
number of arguments (which is a &#8220;hole&#8221; in C since it disables type
checking in that case). In both C and C++, the declaration <B>func(void);</B>
means an empty argument list. The
<A NAME="Index434"></A><A NAME="Index435"></A><B>void</B> keyword means
&#8220;nothing&#8221; in this case (it can also mean &#8220;no type&#8221; in
the case of pointers, as you&#8217;ll see later in this
chapter).<A NAME="Index436"></A><A NAME="Index437"></A><A NAME="Index438"></A></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The other option for argument lists
occurs when you don&#8217;t know how many arguments or what type of arguments
you will have; this is called a <I>variable argument
list<A NAME="Index439"></A><A NAME="Index440"></A><A NAME="Index441"></A></I>.
This &#8220;uncertain argument list&#8221; is represented by ellipses
(<B>...</B>)<A NAME="Index442"></A>. Defining a function with a variable
argument list is significantly more complicated than defining a regular
function. You can use a variable argument list for a function that has a fixed
set of arguments if (for some reason) you want to disable the error checks of
function prototyping. Because of this, you should restrict your use of variable
argument lists to C and avoid them in C++ (in which, as you&#8217;ll learn,
there are much better alternatives). Handling variable argument lists is
described in the library section of your local C
guide.</FONT><A NAME="_Toc462979738"></A><A NAME="_Toc472654743"></A><BR></P></DIV>
<A NAME="Heading101"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Function return values<A NAME="Index443"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A C++ function prototype must specify the
return value type of the function (in C, if you leave off the return value type
it defaults to <B>int</B>). The return type specification precedes the function
name. To specify that no value is returned, use the
<B>void<A NAME="Index444"></A><A NAME="Index445"></A> </B>keyword. This will
generate an error if you try to return a value from the function. Here are some
complete function prototypes:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>int</font> f1(<font color=#0000ff>void</font>); <font color=#009900>// Returns an int, takes no arguments</font>
<font color=#0000ff>int</font> f2(); <font color=#009900>// Like f1() in C++ but not in Standard C!</font>
<font color=#0000ff>float</font> f3(<font color=#0000ff>float</font>, <font color=#0000ff>int</font>, <font color=#0000ff>char</font>, <font color=#0000ff>double</font>); <font color=#009900>// Returns a float</font>
<font color=#0000ff>void</font> f4(<font color=#0000ff>void</font>); <font color=#009900>// Takes no arguments, returns nothing</font></PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To return a value from a function, you
use the
<A NAME="Index446"></A><A NAME="Index447"></A><A NAME="Index448"></A><B>return</B>
statement. <B>return</B> exits the function back to the point right after the
function call. If <B>return</B> has an argument, that argument becomes the
return value of the function. If a function says that it will return a
particular type, then each <B>return </B>statement must return that type. You
can have more than one <B>return</B> statement in a function
definition:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C03:Return.cpp</font>
<font color=#009900>// Use of "return"</font>
#include &lt;iostream&gt;
<font color=#0000ff>using</font> <font color=#0000ff>namespace</font> std;

<font color=#0000ff>char</font> cfunc(<font color=#0000ff>int</font> i) {
  <font color=#0000ff>if</font>(i == 0)
    <font color=#0000ff>return</font> 'a';

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -