?? ch10.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<!-- This document was created from RTF source by rtftohtml version 3.0.1 -->
<META NAME="GENERATOR" Content="Symantec Visual Page 1.0">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
<TITLE>Teach Yourself C++ in 21 Days</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<H1 ALIGN="CENTER"><A HREF="ch09.htm" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/htm/ch09.htm"><IMG SRC="../buttons/BLANPREV.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/buttons/BLANPREV.GIF"
WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="javascript:if(confirm('http://www.mcp.com/sams \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://www.mcp.com/sams'" tppabs="http://www.mcp.com/sams"><IMG
SRC="../buttons/BLANHOME.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/buttons/BLANHOME.GIF" WIDTH="37" HEIGHT="37" ALIGN="BOTTOM"
BORDER="0"></A><A HREF="../index.htm" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/index.htm"><IMG SRC="../buttons/BLANTOC.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/buttons/BLANTOC.GIF"
WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="ch11.htm" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/htm/ch11.htm"><IMG SRC="../buttons/BLANNEXT.GIF" tppabs="http://petunia.atomki.hu/pio/Manuals/english/0-672/0-672-31070-8/buttons/BLANNEXT.GIF"
WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A></H1>
<H1></H1>
<UL>
<LI><A HREF="#Heading1">Day 10</A>
<UL>
<LI><A HREF="#Heading2">Advanced Functions</A>
<UL>
<LI><A HREF="#Heading3">Overloaded Member Functions</A>
<LI><A HREF="#Heading4">Listing 10.1. Overloading member functions</A><A HREF="#Heading5">.</A>
<LI><A HREF="#Heading6">Using Default Values</A>
<LI><A HREF="#Heading7">Listing 10.2. Using default values</A><A HREF="#Heading8">.</A>
<LI><A HREF="#Heading9">Choosing Between Default Values and Overloaded Functions</A>
<LI><A HREF="#Heading10">The Default Constructor</A>
<LI><A HREF="#Heading11">Overloading Constructors</A>
<LI><A HREF="#Heading12">Listing 10.3. Overloading the constructor</A><A HREF="#Heading13">.</A>
<LI><A HREF="#Heading14">Initializing Objects</A>
<LI><A HREF="#Heading15">Listing 10.4. A code snippet showing initialization of member
variables</A><A HREF="#Heading16">.</A>
<LI><A HREF="#Heading17">The Copy Constructor</A>
<UL>
<LI><A HREF="#Heading18">Figure 10.1.</A>
<LI><A HREF="#Heading19">Figure 10.2.</A>
</UL>
<LI><A HREF="#Heading20">Listing 10.5. Copy constructors</A><A HREF="#Heading21">.</A>
<UL>
<LI><A HREF="#Heading22">Figure 10.3.</A>
</UL>
<LI><A HREF="#Heading23">Operator Overloading</A>
<LI><A HREF="#Heading24">Listing 10.6. The Counter class</A><A HREF="#Heading25">.</A>
<UL>
<LI><A HREF="#Heading26">Writing an Increment Function</A>
</UL>
<LI><A HREF="#Heading27">Listing 10.7. Adding an increment operator</A><A HREF="#Heading28">.</A>
<UL>
<LI><A HREF="#Heading29">Overloading the Prefix Operator</A>
</UL>
<LI><A HREF="#Heading30">Listing 10.8. Overloading operator</A><A HREF="#Heading31">++.</A>
<UL>
<LI><A HREF="#Heading32">Returning Types in Overloaded Operator Functions</A>
</UL>
<LI><A HREF="#Heading33">Listing 10.9. Returning a temporary object</A><A HREF="#Heading34">.</A>
<UL>
<LI><A HREF="#Heading35">Returning Nameless Temporaries</A>
</UL>
<LI><A HREF="#Heading36">Listing 10.10. Returning a nameless temporary object</A><A
HREF="#Heading37">.</A>
<UL>
<LI><A HREF="#Heading38">Using the this Pointer</A>
</UL>
<LI><A HREF="#Heading39">Listing 10.11. Returning the this pointer</A><A HREF="#Heading40">.</A>
<UL>
<LI><A HREF="#Heading41">Overloading the Postfix Operator</A>
<LI><A HREF="#Heading42">Difference Between Prefix and Postfix</A>
</UL>
<LI><A HREF="#Heading43">Listing 10.12. Prefix and postfix operators</A><A HREF="#Heading44">.</A>
<LI><A HREF="#Heading45">Operator Overloading Unary Operators</A>
<UL>
<LI><A HREF="#Heading46">The Addition Operator</A>
</UL>
<LI><A HREF="#Heading47">Listing 10.13. The Add() function</A><A HREF="#Heading48">.</A>
<UL>
<LI><A HREF="#Heading49">Overloading operator+</A>
</UL>
<LI><A HREF="#Heading50">Listing 10.14. operator</A><A HREF="#Heading51">+.</A>
<LI><A HREF="#Heading52">Operator Overloading: Binary Operators</A>
<UL>
<LI><A HREF="#Heading53">Issues in Operator Overloading</A>
<LI><A HREF="#Heading54">Limitations on Operator Overloading</A>
<LI><A HREF="#Heading55">What to Overload</A>
<LI><A HREF="#Heading56">The Assignment Operator</A>
</UL>
<LI><A HREF="#Heading57">Listing 10.15. An assignment operator</A><A HREF="#Heading58">.</A>
<LI><A HREF="#Heading59">Conversion Operators</A>
<LI><A HREF="#Heading60">Listing 10.16. Attempting to assign a Counter to a USHORT</A><A
HREF="#Heading61">.</A>
<LI><A HREF="#Heading62">Listing 10.17. Converting USHORT to Counter</A><A HREF="#Heading63">.</A>
<UL>
<LI><A HREF="#Heading64">Conversion Operators</A>
</UL>
<LI><A HREF="#Heading65">Listing 10.18. Converting from Counter to unsigned short().</A>
<LI><A HREF="#Heading66">Summary</A>
<LI><A HREF="#Heading67">Q&A</A>
<LI><A HREF="#Heading68">Workshop</A>
<UL>
<LI><A HREF="#Heading69">Quiz</A>
<LI><A HREF="#Heading70">Exercises</A>
</UL>
</UL>
</UL>
</UL>
<P>
<HR SIZE="4">
<H2 ALIGN="CENTER"><A NAME="Heading1"></A><FONT COLOR="#000077">Day 10</FONT></H2>
<H2 ALIGN="CENTER"><A NAME="Heading2"></A><FONT COLOR="#000077">Advanced Functions</FONT></H2>
<P>On Day 5, "Functions," you learned the fundamentals of working with
functions. Now that you know how pointers and references work, there is more you
can do with functions. Today you learn
<UL>
<LI>How to overload member functions.
<P>
<LI>How to overload operators.
<P>
<LI>How to write functions to support classes with dynamically allocated variables.
</UL>
<H3 ALIGN="CENTER"><A NAME="Heading3"></A><FONT COLOR="#000077">Overloaded Member
Functions</FONT></H3>
<P>On Day 5, you learned how to implement function polymorphism, or function overloading,
by writing two or more functions with the same name but with different parameters.
Class member functions can be overloaded as well, in much the same way.</P>
<P>The <TT>Rectangle</TT> class, demonstrated in Listing 10.1, has two <TT>DrawShape()</TT>
functions. One, which takes no parameters, draws the <TT>Rectangle</TT> based on
the class's current values. The other takes two values, <TT>width</TT> and <TT>length</TT>,
and draws the rectangle based on those values, ignoring the current class values.</P>
<P><A NAME="Heading4"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 10.1. Overloading
member functions.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">
1: //Listing 10.1 Overloading class member functions
2: #include <iostream.h>
3:
4: typedef unsigned short int USHORT;
5: enum BOOL { FALSE, TRUE};
6:
7: // Rectangle class declaration
8: class Rectangle
9: {
10: public:
11: // constructors
12: Rectangle(USHORT width, USHORT height);
13: ~Rectangle(){}
14:
15: // overloaded class function DrawShape
16: void DrawShape() const;
17: void DrawShape(USHORT aWidth, USHORT aHeight) const;
18:
19: private:
20: USHORT itsWidth;
21: USHORT itsHeight;
22: };
23:
24: //Constructor implementation
25: Rectangle::Rectangle(USHORT width, USHORT height)
26: {
27: itsWidth = width;
28: itsHeight = height;
29: }
30:
31:
32: // Overloaded DrawShape - takes no values
33: // Draws based on current class member values
34: void Rectangle::DrawShape() const
35: {
36: DrawShape( itsWidth, itsHeight);
37: }
38:
39:
40: // overloaded DrawShape - takes two values
41: // draws shape based on the parameters
42: void Rectangle::DrawShape(USHORT width, USHORT height) const
43: {
44: for (USHORT i = 0; i<height; i++)
45: {
46: for (USHORT j = 0; j< width; j++)
47: {
48: cout << "*";
49: }
50: cout << "\n";
51: }
52: }
53:
54: // Driver program to demonstrate overloaded functions
55: int main()
56: {
57: // initialize a rectangle to 30,5
58: Rectangle theRect(30,5);
59: cout << "DrawShape(): \n";
60: theRect.DrawShape();
61: cout << "\nDrawShape(40,2): \n";
62: theRect.DrawShape(40,2);
63: return 0;
<TT>64: }</TT></FONT></PRE>
<BLOCKQUOTE>
<P>
<HR>
<FONT COLOR="#000077"><B>NOTE:</B></FONT><B> </B>This listing passes <TT>width</TT>
and <TT>height</TT> values to several functions. You should note that sometimes <TT>width</TT>
is passed first and at other times <TT>height</TT> is passed first.
<HR>
</BLOCKQUOTE>
<PRE><FONT COLOR="#0066FF">Output: DrawShape():
******************************
******************************
******************************
******************************
******************************
DrawShape(40,2):
************************************************************
************************************************************
</FONT></PRE>
<P><FONT COLOR="#000077"><B>Analysis:</B></FONT><B> </B>Listing 10.1 represents a
stripped-down version of the Week in Review project from Week 1. The test for illegal
values has been taken out to save room, as have some of the accessor functions. The
main program has been stripped down to a simple driver program, rather than a menu.<BR>
<BR>
The important code, however, is on lines 16 and 17, where <TT>DrawShape()</TT> is
overloaded. The implementation for these overloaded class methods is on lines 32-52.
Note that the version of <TT>DrawShape()</TT> that takes no parameters simply calls
the version that takes two parameters, passing in the current member variables. Try
very hard to avoid duplicating code in two functions. Otherwise, keeping them in
sync when changes are made to one or the other will be difficult and error-prone.</P>
<P>The driver program, on lines 54-64, creates a rectangle object and then calls
<TT>DrawShape()</TT>, first passing in no parameters, and then passing in two <TT>unsigned
short</TT> integers.</P>
<P>The compiler decides which method to call based on the number and type of parameters
entered. One can imagine a third overloaded function named <TT>DrawShape()</TT> that
takes one dimension and an enumeration for whether it is the width or height, at
the user's choice.
<H3 ALIGN="CENTER"><A NAME="Heading6"></A><FONT COLOR="#000077">Using Default Values</FONT></H3>
<P>Just as non-class functions can have one or more default values, so can each member
function of a class. The same rules apply for declaring the default values, as illustrated
in Listing 10.2.
<H3 ALIGN="CENTER"></H3>
<P><A NAME="Heading7"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 10.2. Using default
values.</B></FONT></P>
<PRE><FONT COLOR="#0066FF">
1: //Listing 10.2 Default values in member functions
2: #include <iostream.h>
3:
4: typedef unsigned short int USHORT;
5: enum BOOL { FALSE, TRUE};
6:
7: // Rectangle class declaration
8: class Rectangle
9: {
10: public:
11: // constructors
12: Rectangle(USHORT width, USHORT height);
13: ~Rectangle(){}
14: void DrawShape(USHORT aWidth, USHORT aHeight, BOOL UseCurrentVals = Â FALSE) const;
15:
16: private:
17: USHORT itsWidth;
18: USHORT itsHeight;
19: };
20:
21: //Constructor implementation
22: Rectangle::Rectangle(USHORT width, USHORT height):
23: itsWidth(width), // initializations
24: itsHeight(height)
25: {} // empty body
26:
27:
28: // default values used for third parameter
29: void Rectangle::DrawShape(
30: USHORT width,
31: USHORT height,
32: BOOL UseCurrentValue
33: ) const
34: {
35: int printWidth;
36: int printHeight;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -