📄 ch28.htm
字号:
<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>
-->
<UL>
<LI><A HREF="#Heading1">- 28 -</A>
<UL>
<LI><A HREF="#Heading2">Programming in C++</A>
<UL>
<LI><A HREF="#Heading3">What Is C++?</A>
<UL>
<LI><A HREF="#Heading4">Why C++?</A>
<LI><A HREF="#Heading5">Classes of Objects and Methods</A>
<LI><A HREF="#Heading6">GCC Options</A>
</UL>
<LI><A HREF="#Heading7">NOTE</A>
<LI><A HREF="#Heading8">NOTE</A>
<UL>
<LI><A HREF="#Heading9">Debugging and Profiling Options</A>
<LI><A HREF="#Heading10">GCC C++ Specific Options</A>
</UL>
<LI><A HREF="#Heading11">Debugging C++ Applications</A>
<UL>
<LI><A HREF="#Heading12">Debugging Virtual Functions</A>
<LI><A HREF="#Heading13">Debugging Exception Handlers</A>
<LI><A HREF="#Heading14">Summary of gdb C++ Specific Commands</A>
</UL>
<LI><A HREF="#Heading15">GNU C++ Class Libraries</A>
<UL>
<LI><A HREF="#Heading16">Streams</A>
<LI><A HREF="#Heading17">Strings</A>
<LI><A HREF="#Heading18">Random Numbers</A>
<LI><A HREF="#Heading19">Data Collection</A>
<LI><A HREF="#Heading20">Linked Lists</A>
<LI><A HREF="#Heading21">Plex Classes</A>
<LI><A HREF="#Heading22">Stacks</A>
<LI><A HREF="#Heading23">Queues</A>
<LI><A HREF="#Heading24">Sets</A>
</UL>
<LI><A HREF="#Heading25">Summary</A>
</UL>
</UL>
</UL>
<P>
<HR SIZE="4">
<H2 ALIGN="CENTER"><A NAME="Heading1<FONT COLOR="#000077">- 28 -</FONT></H2>
<H2 ALIGN="CENTER"><A NAME="Heading2<FONT COLOR="#000077">Programming in C++</FONT></H2>
<P><I>by Rick McMullin</I></P>
<P>IN THIS CHAPTER</P>
<UL>
<LI>What Is C++?
<P>
<LI>Debugging C++ Applications 517
</UL>
<P>n GNU C++ Class Libraries 520 Chapter 27, "Programming in C," introduced
you to the C programming environment and C programming tools that come with Linux.
This chapter describes the same kinds of information for C++. This chapter covers
the following topics:
<UL>
<LI>What C++ is
<P>
<LI>Why to use C++
<P>
<LI>The GNU C++ compiler
<P>
<LI>Debugging C++ applications
</UL>
<P>In addition to these topics, this chapter also looks at some of the C++ programming
tools and class libraries that are included on the Linux CD-ROM.
<H3 ALIGN="CENTER"><A NAME="Heading3<FONT COLOR="#000077">What Is C++?</FONT></H3>
<P>C++ is an object-oriented extension to the C programming language. It was developed
at Bell Labs in the early 1980s and is quickly becoming the language of choice in
the computer industry. Dozens of C++ compilers are available on the market today.
The most common of these for PC-based systems are Borland C++, Microsoft's Visual
C++, Zortech C++, and Watcom C++. These compilers compile MS DOS and MS Windows applications,
and some of them compile code to run on OS/2 and Windows NT as well. In addition
to the number of C++ compilers that are available on DOS-based machines, a great
number are also based on other hardware architectures.</P>
<P>Most UNIX systems have C++ compilers available from the system vendor. Linux also
comes with a C++ compiler. This is the GNU C++ compiler. The GNU C++ compiler is
very closely related to the GNU C compiler (GCC). In fact, since Release 2.0 of GCC,
the GNU C++ compiler has been integrated with GCC. Previous to Release 2.0 of GCC,
the GNU C++ compiler was a separate program known as <TT>g++</TT>. One of the major
enhancements in Release 2.0 of GCC was merging these two compilers.</P>
<P>GCC now incorporates a C compiler, a C++ compiler, and an Objective C compiler.
You will still find the g++ executable on your system, but it is now a script file
that calls GCC with all the standard C++ options.
<H4 ALIGN="CENTER"><A NAME="Heading4<FONT COLOR="#000077">Why C++?</FONT></H4>
<P>C++ and object-oriented programming (OOP) did not just happen. There were many
funda- mental reasons for the shift from structured programming to OOP. In the early
days of computer programming, back when PDP-8s still roamed the earth in great numbers,
there was a shift from machine language coding to assembler language coding. This
was done because the computers of the day were a little more powerful than their
predecessors. Programmers wanted to make their lives easier by moving some of the
burden of programming onto the computer.</P>
<P>As the years went by and computers got even more powerful, new, higher-level languages
started to appear. Examples of these languages are FORTRAN, COBOL, Pascal, and C.
With these languages came a programming methodology known as structured programming.
Structured programming helped to simplify the systems being designed by enabling
programmers to break the problem into small pieces and then implement these pieces
as functions or procedures in whatever language was being used.</P>
<P>The structured programming approach worked well for small to medium-sized software
applications, but it started to fall apart as systems reached a certain size. OOP
tried to solve some of the problems that structured programming was causing. It did
this by extending some of the structured programming concepts and by introducing
some of its own.</P>
<P>The main concepts that OOP focuses on are the following:
<UL>
<LI>Data encapsulation
<P>
<LI>Inheritance
<P>
<LI>Polymorphism
</UL>
<P>Data Encapsulation In structured programming, problems often arose where there
was a data structure that was common to several different pieces of code. One piece
of code could access that data without the other piece of code being aware that anything
was happening.</P>
<P>Data encapsulation is a process of grouping common data together, storing it into
a data type, and providing a consistent interface to that data. This ensures that
no one can access that data without going through the user interface that has been
defined for that data.</P>
<P>The biggest benefit that this kind of mechanism provides is that it protects code
outside the code that is directly managing this data from being affected if the structure
of the data changes. This greatly reduces the complexity of large software systems.</P>
<P>C++ implements data encapsulation through the use of classes. Inheritance Inheritance
is a form of code reuse in which you can inherit or use the data and behavior of
other pieces of code. Inheritance is typically used only when a piece of software
logically has many of the same characteristics as another piece of software, such
as when one object is a specialization of another object.</P>
<P>Inheritance is implemented in C++ by allowing objects to be subclassed by other
objects. Polymorphism Polymorphism occurs when a language allows you to define functions
that perform different operations on objects depending on their type. The true power
of this lies in the fact that you can send a message to a base class and that message
can be passed down to each of its subclasses and mean different things to each of
them.</P>
<P>Polymorphism is implemented in C++ using virtual functions.
<H4 ALIGN="CENTER"><A NAME="Heading5<FONT COLOR="#000077">Classes of Objects
and Methods</FONT></H4>
<P>In C++, classes can be thought of as C structures that contain not only the data
fields but also operations that can be performed on those data fields. A simple example
of this concept is a geometric shape. A geometric shape can be many things, such
as a rectangle, a triangle, or a circle. All geometric shapes have certain attributes
in common, including area and volume. You could define a structure in C called <TT>shape</TT>
in the following way:</P>
<PRE><FONT COLOR="#0066FF">struct shape{
float area;
float volume;
}
</FONT></PRE>
<P>If you added some common behavior to this structure, you would have the equivalent
of a C++ class. This would be written as follows:</P>
<PRE><FONT COLOR="#0066FF">class shape {
public:
float area;
float volume;
float calc_area();
float calc_volume():
};
</FONT></PRE>
<P>You have now defined a C++ class. The <TT>calc_area</TT> and <TT>calc_volume</TT>
items are known as methods of the class (instead of functions, as in C). If you were
to define a variable that was of type <TT>shape</TT> as</P>
<PRE><FONT COLOR="#0066FF">shape circle;
</FONT></PRE>
<P>you would have created a circle object. An object is an instance of a class, or
a variable that is defined to be of the type of a class.
<H4 ALIGN="CENTER"><A NAME="Heading6<FONT COLOR="#000077">GCC Options</FONT></H4>
<P>This section describes some of the GCC command-line options that are most commonly
used. I will first talk about some of the options that can be used both with C and
C++ and then talk about C++ specific options. Any of the compiler options that you
use with C you can use with C++ as well, but some of them may not make any sense
in the context of a C++ compile. If you specify options that don't make sense, the
compiler just ignores them.
<DL>
<DT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading7<FONT COLOR="#000077"><B>NOTE:</B> </FONT>When you are compiling
C++ programs, it is easiest to use the <TT>g++</TT> script. This sets all the default
C++ options so you don't have to.
<HR>
</DL>
<P>A great number of compiler options can be passed to GCC. Many of these options
are specific to a certain hardware platform or are for making fine-tuning adjustments
to the code that is produced. You will probably never use any of these kinds of options.
The options covered in this chapter are those that you will use on a regular basis.</P>
<P>Many of the GCC options consist of more than one character. For this reason, you
must specify each option with its own hyphen and not group options after a single
hyphen as you can with most Linux commands.</P>
<P>When you compile a program using GCC without any command-line options, it creates
an executable file (assuming that the compile was successful) and calls it <TT>a.out</TT>.
For example, the following command would create a file named <TT>a.out</TT> in the
current directory:</P>
<PRE><FONT COLOR="#0066FF">gcc test.C
</FONT></PRE>
<P>To specify a name other than <TT>a.out</TT> for the executable file, you can use
the <TT>-o</TT> compiler option. For example, to compile a C++ program file named
<TT>count.C</TT> (the capital <TT>C</TT> is used to show C++ code, as opposed to
a small <TT>c</TT> for C code) into an executable file named <TT>count</TT>, you
would type the following command:</P>
<PRE><FONT COLOR="#0066FF">gcc -o count count.C
</FONT></PRE>
<DL>
<DT><FONT COLOR="#0066FF"></FONT></DT>
</DL>
<DL>
<DD>
<HR>
<A NAME="Heading8<FONT COLOR="#000077"><B>NOTE:</B> </FONT>When you are using
the <TT>-o</TT> option, the executable filename must occur directly after the <TT>-o</TT>
on the command line.
<HR>
</DL>
<P>Other compiler options allow you to specify how far you want the compile to proceed.
The <TT>-c</TT> option tells GCC to compile the code into object code and skip the
assembly and linking stages of the compile. This option is used quite often because
it makes the compilation of multifile C++ programs faster and easier to manage. Object
code files created by GCC have an <TT>.o</TT> extension by default.</P>
<P>The <TT>-S</TT> compiler option tells GCC to stop the compile after it has generated
the assembler files for the C code. Assembler files generated by GCC have an <TT>.s</TT>
extension by default. The <TT>-E</TT> option instructs the compiler to perform only
the preprocessing compiler stage on the input files. When this option is used, the
output from the preprocessor is sent to the standard output rather than being stored
in a file.
<H4 ALIGN="CENTER"><A NAME="Heading9<FONT COLOR="#000077">Debugging and Profiling
Options</FONT></H4>
<P>GCC supports several debugging and profiling options. Of these options, the two
that you are most likely to use for C++ programs are the <TT>-gstabs+</TT> option
and the <TT>-pg</TT> option.</P>
<P>The <TT>-gstabs+</TT> option tells GCC to produce stabs format debugging information
that the GNU debugger (<TT>gdb</TT>) can use to help you debug your program. For
more information on debugging your C++ programs, refer to the "Debugging C++
Applications" section on the next page.</P>
<P>The <TT>-pg</TT> option tells GCC to add extra code to your program that will,
when executed, generate profile information that can be used by the <TT>gprof</TT>
program to display timing information about your program. For additional information
on <TT>gprof</TT>, refer to the "<TT>gprof</TT>" section in Chapter 27.
<H4 ALIGN="CENTER"><A NAME="Heading10<FONT COLOR="#000077">GCC C++ Specific
Options</FONT></H4>
<P>The GCC options that control how a C++ program is compiled are listed in Table
28.1. </P>
<CENTER>
<P><FONT SIZE="4"><B>Table 28.1.</B> GCC options.<B> </B></FONT>
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><I>Option</I></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><I>Meaning</I></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>-fall-virtual</TT></TD>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -