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

📄 chapter04.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:30
Translation Platform:Win32
Number of Output files:22
This File:Chapter04.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>4: Data Abstraction</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="Chapter03.html">Previous Chapter</a> ] 
    [ <a href="Contents.html">Table of Contents</a> ] 
    [ <a href="DocIndex.html">Index</a> ]
     [ <a href="Chapter05.html">Next Chapter</a> ] 
    </FONT>
    
  </CENTER>
  </P></DIV><A NAME="_Toc472654814"></A><FONT FACE = "Verdana"><H1 ALIGN="LEFT">
4: Data Abstraction</H1></FONT>
<A NAME="Heading188"></A><FONT FACE = "Verdana"><H1 ALIGN="LEFT">
</H1></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Verdana" SIZE=4>C++ is a productivity enhancement
tool. Why else </FONT><BR><FONT FACE="Verdana" SIZE=4>would you make the effort
(and it is an effort, </FONT><BR><FONT FACE="Verdana" SIZE=4>regardless of how
easy we attempt to make the transition)</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">to switch from some language that you
already know and are productive with to a new language in which you&#8217;re
going to be <I>less</I> productive for a while, until you get the hang of it?
It&#8217;s because you&#8217;ve become convinced that you&#8217;re going to get
big gains by using this new tool.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Productivity, in computer programming
terms, means that fewer people can make much more complex and impressive
programs in less time. There are certainly other issues when it comes to
choosing a language, such as efficiency (does the nature of the language cause
slowdown and code bloat?), safety (does the language help you ensure that your
program will always do what you plan, and handle errors gracefully?), and
maintenance (does the language help you create code that is easy to understand,
modify, and extend?). These are certainly important factors that will be
examined in this book.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">But raw productivity means a program that
formerly took three of you a week to write now takes one of you a day or two.
This touches several levels of economics. You&#8217;re happy because you get the
rush of power that comes from building something, your client (or boss) is happy
because products are produced faster and with fewer people, and the customers
are happy because they get products more cheaply. The only way to get massive
increases in productivity is to leverage off other people&#8217;s code. That is,
to use libraries.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A library<A NAME="Index964"></A> is
simply a bunch of code that someone else has written and packaged together.
Often, the most minimal package is a file with an extension like <B>lib</B> and
one or more header files to tell your compiler what&#8217;s in the library. The
linker knows how to search through the library file and extract the appropriate
compiled code. But that&#8217;s only one way to deliver a library. On platforms
that span many architectures, such as Linux/Unix, often the only sensible way to
deliver a library is with source code, so it can be reconfigured and recompiled
on the new target.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Thus, libraries are probably the most
important way to improve productivity, and one of the primary design goals of
C++ is to make library use easier. This implies that there&#8217;s something
hard about using libraries in C. Understanding this factor will give you a first
insight into the design of C++, and thus insight into how to use
it.</FONT><A NAME="_Toc312373821"></A><A NAME="_Toc472654815"></A><BR></P></DIV>
<A NAME="Heading189"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
A tiny C-like library</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A library usually starts out as a
collection of functions, but if you have used third-party
<A NAME="Index965"></A><A NAME="Index966"></A>C libraries you know there&#8217;s
usually more to it than that because there&#8217;s more to life than
<A NAME="Index967"></A>behavior, actions, and functions. There are also
<A NAME="Index968"></A>characteristics (blue, pounds, texture, luminance), which
are represented by data. And when you start to deal with a set of
characteristics in C, it is very convenient to clump them together into a
<A NAME="Index969"></A><B>struct</B>, especially if you want to represent more
than one similar thing in your problem space. Then you can make a variable of
this <B>struct</B> for each thing.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Thus, most C libraries have a set of
<B>struct</B>s and a set of functions that act on those <B>struct</B>s. As an
example of what such a system looks like, consider a programming tool that acts
like an array, but whose size can be established at runtime, when it is created.
I&#8217;ll call it a <B>CStash</B>. Although it&#8217;s written in C++, it has
the style of what you&#8217;d write in C:
<A NAME="Index970"></A></FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C04:CLib.h</font>
<font color=#009900>// Header file for a C-like library</font>
<font color=#009900>// An array-like entity created at runtime</font>

<font color=#0000ff>typedef</font> <font color=#0000ff>struct</font> CStashTag {
  <font color=#0000ff>int</font> size;      <font color=#009900>// Size of each space</font>
  <font color=#0000ff>int</font> quantity;  <font color=#009900>// Number of storage spaces</font>
  <font color=#0000ff>int</font> next;      <font color=#009900>// Next empty space</font>
  <font color=#009900>// Dynamically allocated array of bytes:</font>
  <font color=#0000ff>unsigned</font> <font color=#0000ff>char</font>* storage;
} CStash;

<font color=#0000ff>void</font> initialize(CStash* s, <font color=#0000ff>int</font> size);
<font color=#0000ff>void</font> cleanup(CStash* s);
<font color=#0000ff>int</font> add(CStash* s, <font color=#0000ff>const</font> <font color=#0000ff>void</font>* element);
<font color=#0000ff>void</font>* fetch(CStash* s, <font color=#0000ff>int</font> index);
<font color=#0000ff>int</font> count(CStash* s);
<font color=#0000ff>void</font> inflate(CStash* s, <font color=#0000ff>int</font> increase);
<font color=#009900>///:~ </font></PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">A tag name like <B>CStashTag</B> is
generally used <A NAME="Index971"></A>for a <B>struct</B> in case you need to
reference the <B>struct </B>inside itself. For example, when creating a
<I>linked list</I> (each element in your list contains a pointer to the next
element), you need a pointer to the next <B>struct</B> variable, so you need a
way to identify the type of that pointer within the <B>struct</B> body. Also,
you'll almost universally see the <A NAME="Index972"></A><B>typedef</B> as shown
above for every <B>struct</B> in a C library. This is done so you can treat the
<B>struct</B> as if it were a new type and define variables of that
<B>struct</B> like this:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>CStash A, B, C;</PRE></FONT></BLOCKQUOTE>

<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>storage</B> pointer is an
<B>unsigned char*</B>. An <B>unsigned char</B> is the smallest piece of storage
a C compiler supports, <A NAME="Index973"></A><A NAME="Index974"></A>although on
some machines it can be the same size as the largest. It&#8217;s implementation
dependent, but is often one byte long. You might think that because the
<B>CStash</B> is designed to hold any type of variable, a
<A NAME="Index975"></A><B>void*<A NAME="Index976"></A></B> would be more
appropriate here. However, the purpose is not to treat this storage as a block
of some unknown type, but rather as a block of contiguous
bytes.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The source code for the implementation
file (which you may not get if you buy a library commercially &#8211; you might
get only a compiled <B>obj</B> or <B>lib</B> or <B>dll</B>, etc.) looks like
this:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: C04:CLib.cpp {O}</font>
<font color=#009900>// Implementation of example C-like library</font>
<font color=#009900>// Declare structure and functions:</font>
#include <font color=#004488>"CLib.h"</font>
#include &lt;iostream&gt;
#include &lt;cassert&gt; 
<font color=#0000ff>using</font> <font color=#0000ff>namespace</font> std;
<font color=#009900>// Quantity of elements to add</font>
<font color=#009900>// when increasing storage:</font>
<font color=#0000ff>const</font> <font color=#0000ff>int</font> increment = 100;

<font color=#0000ff>void</font> initialize(CStash* s, <font color=#0000ff>int</font> sz) {
  s-&gt;size = sz;
  s-&gt;quantity = 0;
  s-&gt;storage = 0;
  s-&gt;next = 0;
}

<font color=#0000ff>int</font> add(CStash* s, <font color=#0000ff>const</font> <font color=#0000ff>void</font>* element) {
  <font color=#0000ff>if</font>(s-&gt;next &gt;= s-&gt;quantity) <font color=#009900>//Enough space left?</font>
    inflate(s, increment);
  <font color=#009900>// Copy element into storage,</font>
  <font color=#009900>// starting at next empty space:</font>
  <font color=#0000ff>int</font> startBytes = s-&gt;next * s-&gt;size;
  <font color=#0000ff>unsigned</font> <font color=#0000ff>char</font>* e = (<font color=#0000ff>unsigned</font> <font color=#0000ff>char</font>*)element;
  <font color=#0000ff>for</font>(<font color=#0000ff>int</font> i = 0; i &lt; s-&gt;size; i++)
    s-&gt;storage[startBytes + i] = e[i];
  s-&gt;next++;
  <font color=#0000ff>return</font>(s-&gt;next - 1); <font color=#009900>// Index number</font>
}

⌨️ 快捷键说明

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