?? 010.htm
字號(hào):
<HTML><HEAD><meta http-equiv="Content-Type" content="text/html; charset=GB2312"><TITLE>-->Learn C++ in 21 Days-->Day 10</TITLE>
<META NAME="888" CONTENT=" Learn C++ in 21 Days Day 10">
<META NAME="888" CONTENT=" - Learn C++ in 21 Days - Day 10">
<style>
<!--
#page {position:absolute; z-index:0; left:0px; top:0px}
.tt3 {font: 9pt/12pt "宋體"}
.tt2 {font: 12pt/15pt "宋體"}
a {text-decoration:none}
a:hover {color: blue;text-decoration:underline}
-->
</style>
</HEAD>
<p align="center"><script src="../../../1.js"></script> </p>
<TD CLASS="tt3" VALIGN="top" width="8%" bgcolor="#ffffff"><strong><A HREF="011.htm"><FONT style="FONT-SIZE: 9pt">后一頁(yè)</font></A><BR>
<A HREF="009.htm"><FONT style="FONT-SIZE: 9pt">前一頁(yè)</font></A><BR>
<A HREF="index.html"><FONT style="FONT-SIZE: 9pt">回目錄</font></A><BR>
<TD class="tt2" bgcolor="#ffffff" width="100%"><center><B><FONT style="FONT-SIZE: 16.5pt" COLOR="#FF6666" FACE="楷體_GB2312">Day 10</FONT></B></center>
<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>
<BR>
<P>
<HR SIZE="4">
<BR>
<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
<BR>
<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>
<BR>
<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" font style="font-size:10pt">
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>
<BR>
<BR>
<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>
<BR>
<BR>
</BLOCKQUOTE>
<BR>
<PRE><FONT COLOR="#0066FF" font style="font-size:10pt">Output: DrawShape():
******************************
******************************
******************************
******************************
******************************
<BR>
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" font style="font-size:10pt">
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;
37:
38: if (UseCurrentValue == TRUE)
39: {
40: printWidth = itsWidth; // use current class values
41: printHeight = itsHeight;
42: }
43: else
44: {
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -