📄 index.html
字号:
<!--
$Source: /usr/local/cvsroot/BigC++/17/index.html,v $
$Revision: 1.9 $
Big C++, chptr 17
editor: cols=80, tabstop=2
(c) 2005 John Wiley & Sons
Kurt Schmidt, kschmidt@cs.drexel.edu
NOTES
- 3 spaces are used for each indent in examples
REVISIONS:
11/17/03 - creation?
12/4/03 - added JS for title(), images
12/4/03 - made all auxiliary files local to each chptr
12/16/03 - make use of the setupIframe and iframeWrapCode JS functions
12/17/03 - corrected the iframeWrapCode, added a summary slide,
moved title() to <head>
1/10/04 - fixed spelling and script call errors, KS
$Log: index.html,v $
Revision 1.9 2004/04/26 03:42:45 kurt
Fixed Image/cover.png
Revision 1.8 2004/04/12 05:00:38 kurt
added copyright meta tags
Revision 1.7 2004/03/29 05:49:13 kurt
Pared language, fixed tabs (spaces), added copyright to comments
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name='Copyright' content='2005 John Wiley & Sons'>
<meta name='Author' content='Kurt Schmidt'>
<script language="JavaScript" src="./config.js"></script>
<script language="JavaScript" src="./pageFormat.js"></script>
<script><!-- // Set title on page
title()
//--></script>
</head>
<body>
<h2><font color="#009999" size="+3">Chapter 17 - Operator
Overloading</font></h2>
<font size="+1">
<script><!-- // Set title on page
image( "cover.png" )
//--></script>
</font>
<hr><h2><font color="#009999" size="+2">Chapter Goals</font></h2>
</font>
<hr noshade size="4" color="#009999">
<font size="+1">
<ul>
<li>To learn about operator overloading</li>
<li>To learn the various categories of operators and their uses</li>
<li>To learn how operator overloading is employed in the standard library</li>
<li>To be able to implement operators in your own classes</li>
<li>To learn how to avoid common errors in operator overloading</li>
</ul>
</font>
<hr><h2><font color="#009999">17.1 Operator Overloading</font></h2>
<font size="+1">
<ul>
<li>Convenient shorthand</li>
<li>More intuitive:
<blockquote>
<tt>a + b * c</tt>
<br>vs.
<br><tt>plus(a, times(b, c))</tt>
</blockquote>
</li>
<li>C++ supports a rich set of operators</li>
</ul>
</font>
<hr><h2><font color="#009999">17.1 Operator Overloading</font></h2>
<font size="+1">
<ul>
<li>Programmer may define his/her own
<li>Powerful and subtle feature of C++</li>
<li>Advantages:
<ul>
<li>Easier to remember</li>
<li>Reuse of existing code</li>
<li>Concise description of the task</li>
</ul>
</li>
<li>Caveat:
<ul>
<li>Lack of context may disguise meaning</li>
</ul>
</li>
</ul>
</font>
<hr><h2><font color="#009999">17.1 Operator Overloading</font></h2>
<font size="+1">
To define an operator:
<ul>
<li>Can't redefine existing behavior</li>
<li>Can't change precedence, associativity, or arity</li>
<li>Can't invent new symbols</li>
</ul>
<table border="1" width="80%">
<tr>
<th width="100%" colspan="7">
<p align="center">Overloadable Operators</p>
</th>
</tr>
<tr>
<td width="14%" align="center">+</td>
<td width="14%" align="center">-</td>
<td width="14%" align="center">*</td>
<td width="14%" align="center">/</td>
<td width="14%" align="center">%</td>
<td width="15%" align="center">^</td>
<td width="15%" align="center">&</td>
</tr>
<tr>
<td width="14%" align="center">|</td>
<td width="14%" align="center">~</td>
<td width="14%" align="center">!</td>
<td width="14%" align="center">=</td>
<td width="14%" align="center"><</td>
<td width="15%" align="center">></td>
<td width="15%" align="center">+=</td>
</tr>
<tr>
<td width="14%" align="center">-=</td>
<td width="14%" align="center">*=</td>
<td width="14%" align="center">/=</td>
<td width="14%" align="center">%=</td>
<td width="14%" align="center">^=</td>
<td width="15%" align="center">&=</td>
<td width="15%" align="center">|=</td>
</tr>
<tr>
<td width="14%" align="center"><<</td>
<td width="14%" align="center">>></td>
<td width="14%" align="center"><<=</td>
<td width="14%" align="center">>>=</td>
<td width="14%" align="center">==</td>
<td width="15%" align="center">!=</td>
<td width="15%" align="center"><=</td>
</tr>
<tr>
<td width="14%" align="center">>=</td>
<td width="14%" align="center">&&</td>
<td width="14%" align="center">||</td>
<td width="14%" align="center">++</td>
<td width="14%" align="center">--</td>
<td width="15%" align="center">->*</td>
<td width="15%" align="center">.</td>
</tr>
<tr>
<td width="14%" align="center">-></td>
<td width="14%" align="center">[]</td>
<td width="14%" align="center">()</td>
<td width="14%" align="center">new</td>
<td width="14%" align="center">new[]</td>
<td width="15%" align="center">delete</td>
<td width="15%" align="center">delete[]</td>
</tr>
</table>
</font>
<hr><h2><font color="#009999">17.1 Operator Functions</font></h2>
<font size="+1">
<ul>
<li>Can be:
<ol>
<li>Simple (non-member) functions</li>
<li>Member functions</li>
</ol>
</li>
<li>To name function: precede the operator symbol with <tt>operator</tt></li>
</ul>
</font>
<hr><h2><font color="#009999">17.1 Operator Functions
(cont.)</font></h2>
<font size="+1">
<table border="1" cellpadding="4" bgcolor="#00cccc">
<tr>
<td bgcolor="#ffffff"><font size="+1"><font color="#009999">Syntax 17.2 :
Overloading Operator Definition</font>
<pre><b><i>return_type</i></b> operator<i><b>operator_symbol</b></i>( <b><i>parameters</i></b> )
{
<b><i>statements</i></b>
}</pre>
<table border="0" cellpadding="4">
<tr>
<td valign="top"><font size="+1" color="#009999">
Example:</font></td>
<td><font size="+1">
<pre>int operator-( Time a, Time b )
{
return a.seconds_from( b )
}</pre>
</font>
</td>
</tr>
<tr>
<td><font size="+1" color="#009999">Purpose:</font></td>
<td><font size="+1">Supply the implementation of an overloaded
operator.</font></td>
</tr>
</table>
</font>
</td>
</tr>
</table>
</font>
<hr><h2><font color="#009999">17.1 Example - Operator
Functions</font></h2>
<font size="+1">
<ul>
<li>The <i>difference</i> between two <tt>Time</tt> objects is the
number of seconds between them
<blockquote>
<pre>int operator-( Time a, Time b )
{
return a.seconds_from( b );
}</pre>
</blockquote>
</li>
<li>Use the <tt>-</tt> operator instead of calling <tt>seconds_from()</tt>:
<blockquote>
<pre>Time now;
Time morning( 9, 0, 0 );
int seconds_elapsed = now - morning;</pre>
</blockquote>
</li>
<li><tt>operator-</tt> is a nonmember function with two parameters</li>
</ul>
</font>
<hr><h2><font color="#009999">17.1 Operator Overloading</font></h2>
<font size="+1">
Avoid ambiguous behavior:
<blockquote>
<pre><i>some_return_type</i> operator+(Time a, Time b);</pre></blockquote>
<ul>
<li><tt>Time</tt> objects represent a point in time, not a
duration
<ul>
<li>what does 3 P.M. + 3 P.M. mean?</li>
</ul>
</li>
</ul>
</font>
<hr><h2><font color="#009999">17.1 Operator Overloading (cont.)
</font></h2>
<font size="+1">
<ul>
<li>Adding seconds to a <tt>Time</tt> <i>does</i> make sense:
<blockquote>
<pre>Time operator+(Time a, int sec)
{
Time r = a;
r.add_seconds(sec);
return r;
}</pre>
</blockquote>
</li>
</ul>
<p>Caveat: Units are not implied by the context. Add minutes? Seconds?</p>
<ul>
<li>A function named <tt>add_seconds</tt> is clear</li>
</ul>
</font>
<hr><h2><font color="#009999">17.1 Operator Member
Functions</font></h2>
<font size="+1">
<table border="1" cellpadding="4" bgcolor="#00cccc">
<tr>
<td bgcolor="#ffffff"><font size="+1"><font color="#009999">Syntax 17.2 :
Overloading Operator Member Function Definition</font>
<pre><b><i>return_type</i></b> <i><b>ClassName</b></i>::operator<i><b>operator_symbol</b></i>( <b><i>parameters</i></b> )
{
<b><i>statements</i></b>
}</pre>
<table border="0" cellpadding="4">
<tr>
<td valign="top"><font size="+1" color="#009999">
Example:</font></td>
<td><font size="+1">
<pre>Time Time::operator+( int sec )
{
Time r = *this;
r.add_seconds( sec );
return r;
}</pre>
</font>
</td>
</tr>
<tr>
<td><font size="+1" color="#009999">Purpose:</font></td>
<td><font size="+1">Supply the implementation of an overloaded
operator member function.</font></td>
</tr>
</table>
</font>
</td>
</tr>
</table>
</font>
<hr><h2><font color="#009999">17.1 Operator Member
Functions</font></h2>
<font size="+1">
<ul>
<li>First interpreted as a member function of the left-most operand</li>
<li>Subsequent arguments are passed to the member function:
<blockquote><tt>Time later = now + 60;</tt></blockquote>
becomes
<blockquote><tt>Time later = now.operator+( 60 );</tt></blockquote>
</li>
<li>E.g., a binary operator has only one argument</li>
</ul>
</font>
<hr><h2><font color="#009999">17.1 Example - Operator Member
Functions</font></h2>
<font size="+1">
The addition operator as a member function of the <tt>Time</tt> class:
<blockquote>
<pre>class Time
{
...
Time operator+( int sec ) const;
};
Time Time::operator+( int sec ) const
{
Time r = *this; <font color="#0000cc">// Copy the implicit parameter</font>
r.add_seconds( sec );
return r;
}</pre>
</blockquote>
</font>
<hr><h2><font color="#009999">17.1 Operator Overloading - member vs.
global</font></h2>
<font size="+1">
<ul>
<li>Some operators (e.g., assignment) are required to be members</li>
<li>Usually programmer has the choice</li>
<li>Keep in mind:
<ol>
<li>Non-members have only public access (or, make it a friend)</li>
<li>No implicit conversion for implicit parameters</li>
</ol>
</li>
<li>Member function is preferable if
<ul>
<li>The left argument is modified (<tt>+=</tt>)</li>
<li>The data fields are not easily accessible</li>
</ul>
</li>
</ul>
</font>
<hr><h2><font color="#009999">17.2 Case Study: Fractional
Numbers</font></h2>
<font size="+1">
<p>New type to represent a ratio of 2 integers:</p>
<blockquote><tt>
Fraction a( 3, 4 ); <font color="#0000cc">// Represents 3/4</font><br>
Fraction b( 7 ); <font color="#0000cc">// Represents 7/1</font><br>
</tt></blockquote>
<p>Fractions should behave as other numbers:</p>
<blockquote>
<pre>Fraction c( 1, 2 );
if( a < b )
c = b - a;
else
c = a - b;
cout << "Value is " << c << "\n";</pre>
</blockquote>
</font>
<hr><h2><font color="#009999">17.2 Case Study: Fractional
Numbers</font></h2>
<font size="+1">
<p>Should be able to mix w/other numbers:</p>
<blockquote>
<pre>c = a + 3; <font color="#0000cc">// Should mean same as addition of 3/1</font>
double x = 2.5 * a; <font color="#0000cc">// Should convert fraction to a double</font></pre>
</blockquote>
</font>
<hr><h2><font color="#009999">17.2 Case Study: Fractional Numbers -
Construction</font></h2>
<font size="+1">
<ul>
<li><tt>normalize</tt> (fraction.h, line 70):
<ul>
<li>fraction is in lowest terms</li>
<li>denominator is not negative</li>
</ul>
</li>
<li>Default constructor (line 14) sets value to 0</li>
<li>Constructor (line 20) sets value to t/1</li>
<li>Constructor (line 27) sets value to t/b, calls <tt>normalize</tt></li>
</ul>
</font>
<hr><h2><font color="#009999">17.2 Case Study: Fractional Numbers
(<tt>fraction.h</tt>)</font></h2>
<font size="+1">
<script><!--
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -