rhl28.htm

来自「linux的初学电子书」· HTM 代码 · 共 2,643 行 · 第 1/4 页

HTM
2,643
字号
<HTML>

<HEAD>

<TITLE>Red Hat Linux Unleashed rhl28.htm </TITLE>

<LINK REL="ToC" HREF="httoc.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/httoc.htm">

<LINK REL="Index" HREF="htindex.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/htindex.htm">

<LINK REL="Next" HREF="rhl29.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl29.htm">

<LINK REL="Previous" HREF="rhl27.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl27.htm"></HEAD>

<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080">

<A NAME="I0"></A>

<H2>Red Hat Linux Unleashed rhl28.htm</H2>

<P ALIGN=LEFT>

<A HREF="rhl27.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl27.htm" TARGET="_self"><IMG SRC="purprev.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>

<A HREF="httoc.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/httoc.htm" TARGET="_self"><IMG SRC="purtoc.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A>

<A HREF="rhl29.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl29.htm" TARGET="_self"><IMG SRC="purnext.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A>


<HR ALIGN=CENTER>

<P>

<UL>

<UL>

<UL>

<LI>

<A HREF="#E68E226" >What Is C++?</A>

<UL>

<LI>

<A HREF="#E69E345" >Why C++?</A>

<UL>

<LI>

<A HREF="#E70E6" >Data Encapsulation</A>

<LI>

<A HREF="#E70E7" >Inheritance</A>

<LI>

<A HREF="#E70E8" >Polymorphism</A></UL>

<LI>

<A HREF="#E69E346" >Classes of Objects and Methods</A>

<LI>

<A HREF="#E69E347" >GCC Options</A>

<LI>

<A HREF="#E69E348" >Debugging and Profiling Options</A>

<LI>

<A HREF="#E69E349" >GCC C++ Specific Options</A></UL>

<LI>

<A HREF="#E68E227" >Debugging C++ Applications</A>

<UL>

<LI>

<A HREF="#E69E350" >Debugging Virtual Functions</A>

<LI>

<A HREF="#E69E351" >Debugging Exception Handlers</A>

<LI>

<A HREF="#E69E352" >Summary of gdb C++ Specific Commands</A></UL>

<LI>

<A HREF="#E68E228" >GNU C++ Class Libraries</A>

<UL>

<LI>

<A HREF="#E69E353" >Streams</A>

<LI>

<A HREF="#E69E354" >Strings</A>

<LI>

<A HREF="#E69E355" >Random Numbers</A>

<LI>

<A HREF="#E69E356" >Data Collection</A>

<LI>

<A HREF="#E69E357" >Linked Lists</A>

<LI>

<A HREF="#E69E358" >Plex Classes</A>

<LI>

<A HREF="#E69E359" >Stacks</A>

<LI>

<A HREF="#E69E360" >Queues</A>

<LI>

<A HREF="#E69E361" >Sets</A></UL>

<LI>

<A HREF="#E68E229" >Summary</A></UL></UL></UL>

<HR ALIGN=CENTER>

<A NAME="E66E28"></A>

<H1 ALIGN=CENTER>

<CENTER>

<FONT SIZE=6 COLOR="#FF0000"><B>28</B></FONT></CENTER></H1>

<BR>

<A NAME="E67E28"></A>

<H2 ALIGN=CENTER>

<CENTER>

<FONT SIZE=6 COLOR="#FF0000"><B>Programming in C++</B></FONT></CENTER></H2>

<BR>

<P><A HREF="rhl27.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl27.htm">Chapter 27</A>, &quot;Programming in C,&quot; 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:

<BR>

<UL>

<LI>What C++ is

<BR>

<BR>

<LI>Why to use C++

<BR>

<BR>

<LI>The GNU C++ compiler

<BR>

<BR>

<LI>Debugging C++ applications

<BR>

<BR>

</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.

<BR>

<BR>

<A NAME="E68E226"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>What Is C++?</B></FONT></CENTER></H3>

<BR>

<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++, Symantec C++, and Watcom C++. These compilers can compile MS-DOS, MS Windows, OS/2, and Windows NT programs. 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.

<BR>

<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 g++. One of the major enhancements in Release 2.0 of GCC was merging these two compilers.

<BR>

<P>GCC now incorporates a C compiler, a C++ compiler, and an Objective C compiler, although you will still find the g++ executable on your system.

<BR>

<BR>

<A NAME="E69E345"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Why C++?</B></FONT></CENTER></H4>

<BR>

<P>C++ and object-oriented programming (OOP) did not just happen. There were many fundamental 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.

<BR>

<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.

<BR>

<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.

<BR>

<P>The main concepts that OOP focuses on are the following:

<BR>

<UL>

<LI>Data encapsulation

<BR>

<BR>

<LI>Inheritance

<BR>

<BR>

<LI>Polymorphism

<BR>

<BR>

</UL>

<BR>

<A NAME="E70E6"></A>

<H5 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Data Encapsulation</B></FONT></CENTER></H5>

<BR>

<P>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.

<BR>

<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.

<BR>

<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.

<BR>

<P>C++ implements data encapsulation through the use of classes.

<BR>

<BR>

<A NAME="E70E7"></A>

<H5 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Inheritance</B></FONT></CENTER></H5>

<BR>

<P>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.

<BR>

<P>Inheritance is implemented in C++ by allowing objects to be subclassed by other objects.

<BR>

<BR>

<A NAME="E70E8"></A>

<H5 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Polymorphism</B></FONT></CENTER></H5>

<BR>

<P>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.

<BR>

<P>Polymorphism is implemented in C++ using virtual functions.

<BR>

<BR>

<A NAME="E69E346"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Classes of Objects and Methods</B></FONT></CENTER></H4>

<BR>

<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 shape in the following way:

<BR>

<PRE>

<FONT COLOR="#000080">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:

<BR>

<PRE>

<FONT COLOR="#000080">class shape {

public:

float area;

float volume;

float calc_area();

float calc_volume():

};</FONT></PRE>

<P>You have now defined a C++ class. The calc_area and calc_volume 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 shape as

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">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.

<BR>

<BR>

<A NAME="E69E347"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>GCC Options</B></FONT></CENTER></H4>

<BR>

<P>This section describes some of the GCC 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.

<BR>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>When you are compiling C++ programs, it is easiest to use the g++ script. This sets all the default C++ options so you don't have to.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<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.

<BR>

<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.

<BR>

<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 a.out. For example, the following command would create a file named a.out in the current 
directory:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">gcc test.C</FONT></PRE>

<P>To specify a name other than a.out for the executable file, you can use the -o compiler option. For example, to compile a C++ program file named count.C (the capital C is used to show C++ code, as opposed to a small c for C code) into an executable file 
named count, you would type the following command:

<BR>

<BR>

<PRE>

<FONT COLOR="#000080">gcc -o count count.C</FONT></PRE>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE>When you are using the -o option, the executable filename must occur directly after the -o on the command line.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<P>Other compiler options allow you to specify how far you want the compile to proceed. The -c 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 .o extension by default.

<BR>

<P>The -S 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 .s extension by default. The -E 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.

<BR>

<BR>

<A NAME="E69E348"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Debugging and Profiling Options</B></FONT></CENTER></H4>

<BR>

<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 -gstabs+ option and the -pg option.

<BR>

<P>The -gstabs+ option tells GCC to produce stabs format debugging information that the GNU debugger (gdb) can use to help you debug your program. For more information on debugging your C++ programs, refer to the &quot;Debugging C++ Applications&quot; 
section later in this chapter.

<BR>

<P>The -pg option tells GCC to add extra code to your program that will, when executed, generate profile information that can be used by the gprof program to display timing information about your program. For more information on gprof, refer to the 
&quot;gprof&quot; section in <A HREF="rhl27.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl27.htm">Chapter 27</A>.

<BR>

<BR>

<A NAME="E69E349"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>GCC C++ Specific Options</B></FONT></CENTER></H4>

<BR>

<P>The GCC options that control how a C++ program is compiled are listed in Table 28.1.

<BR>

<BR>

<P ALIGN=CENTER>

<CENTER>

<FONT COLOR="#000080"><B>Table 28.1. GCC options.</B></FONT></CENTER>

<BR>



<TABLE  BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 >

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

<I>Option</I>

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

<I>Meaning</I></FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

-fall-virtual

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Treats all possible member functions as virtual. This applies to all functions except for constructor functions and new or deleted member functions.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

-fdollars-in-identifiers

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Accepts $ in identifiers. You can also prohibit the use of $ in identifiers by using the -fno-dollars-in-identifiers option.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

-felide-constructors

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Tells the compiler to leave out constructors whenever possible.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

-fenum-int-equiv

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Permits implicit conversion of int to enumeration types.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

-fexternal-templates

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Produces smaller code for template declarations. This is done by having the compiler generate only a single copy of each template function where it is defined.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

-fmemoize-lookups

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Uses heuristics to compile faster. These heuristics are not enabled by default because they are effective only for certain input files.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

-fno-strict-prototype

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Treats a function declaration with no arguments the same way that C would treat it. This means that the compiler treats a function prototype that has no arguments as a function that will accept an unknown number of arguments.</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

⌨️ 快捷键说明

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