📄 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://www.mcp.com/814147200/0-672/0-672-31070-8/htm/ch09.htm"><IMG SRC="BLANPREV.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANPREV.GIF"WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="tppmsgs/msgs0.htm#1" tppabs="http://www.mcp.com/sams"><IMGSRC="BLANHOME.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANHOME.GIF" WIDTH="37" HEIGHT="37" ALIGN="BOTTOM"BORDER="0"></A><A HREF="index.htm" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/index.htm"><IMG SRC="BLANTOC.GIF" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/buttons/BLANTOC.GIF"WIDTH="37" HEIGHT="37" ALIGN="BOTTOM" BORDER="0"></A><A HREF="ch11.htm" tppabs="http://www.mcp.com/814147200/0-672/0-672-31070-8/htm/ch11.htm"><IMG SRC="BLANNEXT.GIF" tppabs="http://www.mcp.com/814147200/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 withfunctions. Now that you know how pointers and references work, there is more youcan 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 MemberFunctions</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 onthe 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. Overloadingmember functions.</B></FONT></P><PRE><FONT COLOR="#0066FF">1: //Listing 10.1 Overloading class member functions2: #include <iostream.h>3:4: typedef unsigned short int USHORT;5: enum BOOL { FALSE, TRUE};6:7: // Rectangle class declaration8: class Rectangle9: {10: public:11: // constructors12: Rectangle(USHORT width, USHORT height);13: ~Rectangle(){}14:15: // overloaded class function DrawShape16: void DrawShape() const;17: void DrawShape(USHORT aWidth, USHORT aHeight) const;18:19: private:20: USHORT itsWidth;21: USHORT itsHeight;22: };23:24: //Constructor implementation25: Rectangle::Rectangle(USHORT width, USHORT height)26: {27: itsWidth = width;28: itsHeight = height;29: }30:31:32: // Overloaded DrawShape - takes no values33: // Draws based on current class member values34: void Rectangle::DrawShape() const35: {36: DrawShape( itsWidth, itsHeight);37: }38:39:40: // overloaded DrawShape - takes two values41: // draws shape based on the parameters42: void Rectangle::DrawShape(USHORT width, USHORT height) const43: {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 functions55: int main()56: {57: // initialize a rectangle to 30,558: 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 astripped-down version of the Week in Review project from Week 1. The test for illegalvalues has been taken out to save room, as have some of the accessor functions. Themain 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> isoverloaded. 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 callsthe version that takes two parameters, passing in the current member variables. Tryvery hard to avoid duplicating code in two functions. Otherwise, keeping them insync 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>unsignedshort</TT> integers.</P><P>The compiler decides which method to call based on the number and type of parametersentered. One can imagine a third overloaded function named <TT>DrawShape()</TT> thattakes one dimension and an enumeration for whether it is the width or height, atthe 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 memberfunction of a class. The same rules apply for declaring the default values, as illustratedin Listing 10.2.<H3 ALIGN="CENTER"></H3><P><A NAME="Heading7"></A><FONT SIZE="4" COLOR="#000077"><B>Listing 10.2. Using defaultvalues.</B></FONT></P><PRE><FONT COLOR="#0066FF">1: //Listing 10.2 Default values in member functions2: #include <iostream.h>3:4: typedef unsigned short int USHORT;5: enum BOOL { FALSE, TRUE};6:7: // Rectangle class declaration8: class Rectangle9: {10: public:11: // constructors12: 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 implementation22: Rectangle::Rectangle(USHORT width, USHORT height):23: itsWidth(width), // initializations24: itsHeight(height)25: {} // empty body26:27:28: // default values used for third parameter29: void Rectangle::DrawShape(30: USHORT width,31: USHORT height,32: BOOL UseCurrentValue33: ) const34: {35: int printWidth;36: int printHeight;37:38: if (UseCurrentValue == TRUE)39: {40: printWidth = itsWidth; // use current class values41: printHeight = itsHeight;42: }43: else44: {45: printWidth = width; // use parameter values46: printHeight = height;47: }48:49:50: for (int i = 0; i<printHeight; i++)51: {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -