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

📄 ch19.htm

📁 vc的电子书
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<HR>


</BLOCKQUOTE>

<P><A NAME="Heading18"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 19.4. Using
operator ostream.</B></FONT>
<PRE><FONT COLOR="#0066FF">1:     #include &lt;iostream.h&gt;
2:
3:     const int DefaultSize = 10;
4:
5:     template &lt;class T&gt;  // declare the template and the parameter
6:     class Array            // the class being parameterized
7:     {
8:     public:
9:        // constructors
10:       Array(int itsSize = DefaultSize);
11:       Array(const Array &amp;rhs);
12:       ~Array() { delete [] pType; }
13:
14:       // operators
15:       Array&amp; operator=(const Array&amp;);
16:       T&amp; operator[](int offSet) { return pType[offSet]; }
17:       const T&amp; operator[](int offSet) const 
18:        { return pType[offSet]; }
19:       // accessors
20:       int GetSize() const { return itsSize; }
21:
22:       friend ostream&amp; operator&lt;&lt; (ostream&amp;, Array&lt;T&gt;&amp;);
23:
24:    private:
25:       T *pType;
26:       int  itsSize;
27:    };
28:
29:    template &lt;class T&gt;
30:    ostream&amp; operator&lt;&lt; (ostream&amp; output, Array&lt;T&gt;&amp; theArray)
31:    {
32:       for (int i = 0; i&lt;theArray.GetSize(); i++)
33:          output &lt;&lt; &quot;[&quot; &lt;&lt; i &lt;&lt; &quot;] &quot; &lt;&lt; theArray[i] &lt;&lt; endl; return output;
34:    }
35:
36:    enum BOOL { FALSE, TRUE};
37:
38:    int main()
39:    {
40:       BOOL Stop = FALSE;       // flag for looping
41:       int offset, value;
42:       Array&lt;int&gt; theArray;
43:
44:       while (!Stop)
45:       {
46:          cout &lt;&lt; &quot;Enter an offset (0-9) &quot;;
47:          cout &lt;&lt; &quot;and a value. (-1 to stop): &quot; ;
47:          cin &gt;&gt; offset &gt;&gt; value;
48:
49:          if (offset &lt; 0)
50:             break;
51:
52:          if (offset &gt; 9)
53:          {
54:             cout &lt;&lt; &quot;***Please use values between 0 and 9.***\n&quot;;
55:             continue;
56:          }
57:
58:          theArray[offset] = value;
59:       }
60:
61:       cout &lt;&lt; &quot;\nHere's the entire array:\n&quot;;
62:       cout &lt;&lt; theArray &lt;&lt; endl;
63:     return 0;
<TT>64: }</TT></FONT>
<FONT COLOR="#0066FF">
Output: Enter an offset (0-9) and a value. (-1 to stop): 1 10
Enter an offset (0-9) and a value. (-1 to stop): 2 20
Enter an offset (0-9) and a value. (-1 to stop): 3 30
Enter an offset (0-9) and a value. (-1 to stop): 4 40
Enter an offset (0-9) and a value. (-1 to stop): 5 50
Enter an offset (0-9) and a value. (-1 to stop): 6 60
Enter an offset (0-9) and a value. (-1 to stop): 7 70
Enter an offset (0-9) and a value. (-1 to stop): 8 80
Enter an offset (0-9) and a value. (-1 to stop): 9 90
Enter an offset (0-9) and a value. (-1 to stop): 10 10
***Please use values between 0 and 9.***
Enter an offset (0-9) and a value. (-1 to stop): -1 -1

Here's the entire array:
[0] 0
[1] 10
[2] 20
[3] 30
[4] 40
[5] 50
[6] 60
[7] 70
[8] 80
[9] 90
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>On line 22, the function
template <TT>operator&lt;&lt;()</TT> is declared to be a friend of the <TT>Array</TT>
class template. Because <TT>operator&lt;&lt;()</TT> is implemented as a template
function, every instance of this parameterized array type will automatically have
an <TT>operator&lt;&lt;()</TT>. The implementation for this operator starts on line
29. Every member of an array is called in turn. This only works if there is an <TT>operator&lt;&lt;</TT>
defined for every type of object stored in the array.
<H4 ALIGN="CENTER"><A NAME="Heading20"></A><FONT COLOR="#000077">A Type-Specific
Template Friend Class or Function</FONT></H4>
<P>Although the insertion operator shown in Listing 19.4 works, it is still not quite
what is needed. Because the declaration of the friend operator on line 29 declares
a template, it will work for any instance of <TT>Array</TT> and any insertion operator
taking an array of any type.</P>
<P>The insertion operator template shown in Listing 19.4 makes all instances of the
insertion <TT>operator&lt;&lt;</TT> a friend of any instance of <TT>Array</TT>, whether
the instance of the insertion operator is an integer, an <TT>Animal</TT>, or a <TT>Car</TT>.
It makes no sense, however, for an <TT>Animal</TT> insertion operator to be a friend
to the insertion operator for an integer array.</P>
<P>What is needed is for the insertion operator for an array of <TT>int</TT> to be
a friend to the <TT>Array</TT> of <TT>int</TT> class, and for the insertion operator
of an array of <TT>Animals</TT> to be a friend to the <TT>Array</TT> of animals instance.</P>
<P>To accomplish this, modify the declaration of the insertion operator on line 29
of Listing 19.4, and remove the words <TT>template &lt;class T&gt;</TT>. That is,
change line 30 to read</P>
<PRE><FONT COLOR="#0066FF">friend ostream&amp; operator&lt;&lt; (ostream&amp;, Array&lt;T&gt;&amp;);
</FONT></PRE>
<P>This will use the type (<TT>T</TT>) declared in the template of <TT>Array</TT>.
Thus, the <TT>operator&lt;&lt;</TT> for an integer will only work with an array of
integers, and so forth.
<H3 ALIGN="CENTER"><A NAME="Heading21"></A><FONT COLOR="#000077">Using Template Items</FONT></H3>
<P>You can treat template items as you would any other type. You can pass them as
parameters, either by reference or by value, and you can return them as the return
values of functions, also by value or by reference. Listing 19.5 demonstrates how
to pass template objects.</P>

<P><A NAME="Heading22"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 19.5. Passing
template objects to and from functions.</B></FONT>
<PRE><FONT COLOR="#0066FF">1:     #include &lt;iostream.h&gt;
2:
3:     const int DefaultSize = 10;
4:
5:     // A trivial class for adding to arrays
6:     class Animal
7:     {
8:     public:
9:     // constructors
10:         Animal(int);
11:         Animal();
12:         ~Animal();
13:
14:         // accessors
15:         int GetWeight() const { return itsWeight; }
16:         void SetWeight(int theWeight) { itsWeight = theWeight; }
17:
18:          // friend operators
19:         friend ostream&amp; operator&lt;&lt; (ostream&amp;, const Animal&amp;);
20:
21:    private:
22:         int itsWeight;
23:    };
24:
25:    // extraction operator for printing animals
26:    ostream&amp; operator&lt;&lt; 
27:        (ostream&amp; theStream, const Animal&amp; theAnimal)
28    {
29:    theStream &lt;&lt; theAnimal.GetWeight();
30:    return theStream;
31:    }
32:
33:    Animal::Animal(int weight):
34:    itsWeight(weight)
35:    {
36:       // cout &lt;&lt; &quot;Animal(int)\n&quot;;
37:    }
38:
39:    Animal::Animal():
40:    itsWeight(0)
41:    {
42:       // cout &lt;&lt; &quot;Animal()\n&quot;;
43:    }
44:
45:    Animal::~Animal()
46:    {
47:      // cout &lt;&lt; &quot;Destroyed an animal...\n&quot;;
48:    }
49:
50:    template &lt;class T&gt;  // declare the template and the parameter
51:    class Array            // the class being parameterized
52:    {
53:    public:
54:       Array(int itsSize = DefaultSize);
55:       Array(const Array &amp;rhs);
56:       ~Array() { delete [] pType; }
57:
58:       Array&amp; operator=(const Array&amp;);
59:       T&amp; operator[](int offSet) { return pType[offSet]; }
60:       const T&amp; operator[](int offSet) const 
61:          { return pType[offSet]; }
62:       int GetSize() const { return itsSize; }
63
64:      // friend function
65:      friend ostream&amp; operator&lt;&lt; (ostream&amp;, const Array&lt;T&gt;&amp;);
66:
67:    private:
68:       T *pType;
69:       int  itsSize;
70:    };
71:
70:    template &lt;class T&gt;
72:    ostream&amp; operator&lt;&lt; (ostream&amp; output, const Array&lt;T&gt;&amp; theArray)
73:    {
74:       for (int i = 0; i&lt;theArray.GetSize(); i++)
75:          output &lt;&lt; &quot;[&quot; &lt;&lt; i &lt;&lt; &quot;] &quot; &lt;&lt; theArray[i] &lt;&lt; endl;
76:       return output;
77:    }
78:
79:    void IntFillFunction(Array&lt;int&gt;&amp; theArray);
80:    void AnimalFillFunction(Array&lt;Animal&gt;&amp; theArray);
81:    enum BOOL {FALSE, TRUE};
82:
84:    int main()
85:    {
86:       Array&lt;int&gt; intArray;
87:       Array&lt;Animal&gt; animalArray;
88:       IntFillFunction(intArray);
87:       AnimalFillFunction(animalArray);
89:       cout &lt;&lt; &quot;intArray...\n&quot; &lt;&lt; intArray;
90:       cout &lt;&lt; &quot;\nanimalArray...\n&quot; &lt;&lt; animalArray &lt;&lt; endl;
91:       return 0;
92:    }
93:
94:    void IntFillFunction(Array&lt;int&gt;&amp; theArray)
95:    {
96:       BOOL Stop = FALSE;
97:       int offset, value;
98:       while (!Stop)
99:       {
100:          cout &lt;&lt; &quot;Enter an offset (0-9) &quot;;
101:          cout &lt;&lt; &quot;and a value. (-1 to stop): &quot; ;
102:          cin &gt;&gt; offset &gt;&gt; value;
103:         if (offset &lt; 0)
104:            break;
105:         if (offset &gt; 9)
106:         {
107:            cout &lt;&lt; &quot;***Please use values between 0 and 9.***\n&quot;;
108:            continue;
109:         }
110:         theArray[offset] = value;
111:      }
112:   }
113:
114:
115:   void AnimalFillFunction(Array&lt;Animal&gt;&amp; theArray)
116:   {
117:      Animal * pAnimal;
118:      for (int i = 0; i&lt;theArray.GetSize(); i++)
119:      {
120:         pAnimal = new Animal;
121:         pAnimal-&gt;SetWeight(i*100);
122:         theArray[i] = *pAnimal;
123:         delete pAnimal;  // a copy was put in the array
124:      }
<TT>125: }</TT></FONT>
<FONT COLOR="#0066FF">
Output: Enter an offset (0-9) and a value. (-1 to stop): 1 10
Enter an offset (0-9) and a value. (-1 to stop): 2 20
Enter an offset (0-9) and a value. (-1 to stop): 3 30
Enter an offset (0-9) and a value. (-1 to stop): 4 40
Enter an offset (0-9) and a value. (-1 to stop): 5 50
Enter an offset (0-9) and a value. (-1 to stop): 6 60
Enter an offset (0-9) and a value. (-1 to stop): 7 70
Enter an offset (0-9) and a value. (-1 to stop): 8 80
Enter an offset (0-9) and a value. (-1 to stop): 9 90
Enter an offset (0-9) and a value. (-1 to stop): 10 10
***Please use values between 0 and 9.***
Enter an offset (0-9) and a value. (-1 to stop): -1 -1

intArray:...
[0] 0
[1] 10
[2] 20
[3] 30
[4] 40
[5] 50
[6] 60
[7] 70
[8] 80
[9] 90

animalArray:...
[0] 0
[1] 100
[2] 200
[3] 300
[4] 400
[5] 500
[6] 600
[7] 700
[8] 800
[9] 900
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>Most of the <TT>Array</TT>
class implementation is left out to save space. The <TT>Animal</TT> class is declared
on lines 6-23. Although this is a stripped-down and simplified class, it does provide
its own insertion operator (<TT>&lt;&lt;</TT>) to allow the printing of <TT>Animal</TT>s.
Printing simply prints the current weight of the <TT>Animal</TT>.<BR>

⌨️ 快捷键说明

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