📄 page604.html
字号:
<HTML>
<HEAD>
<TITLE>Mutators</TITLE>
</HEAD>
<BODY bgcolor="#FFFFFF">
<img src="cover75.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cover75.gif" alt="Logo" align=right>
<b>Data Structures and Algorithms
with Object-Oriented Design Patterns in C++</b><br>
<A NAME="tex2html9369" HREF="page605.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page605.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="next_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/next_motif.gif"></A> <A NAME="tex2html9367" HREF="page603.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page603.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="up_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/up_motif.gif"></A> <A NAME="tex2html9361" HREF="page603.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page603.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/previous_motif.gif"></A> <A NAME="tex2html9371" HREF="page9.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page9.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/contents_motif.gif"></A> <A NAME="tex2html9372" HREF="page620.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page620.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="index_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/index_motif.gif"></A> <BR><HR>
<H3><A NAME="SECTION0018331000000000000000">Mutators</A></H3>
<P>
A <em>mutator</em><A NAME=57364> </A> is a member function that can modify an object.
Every non-<tt>const</tt> member function of a class is a mutator.
In the simplest case,
a mutator just assigns a new value to one of the member variables.
In general,
a mutator performs some computation
and modifies any number of member variables.
<P>
Program <A HREF="page604.html#progcomplex3c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page604.html#progcomplex3c"><IMG ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A> defines three mutators for the <tt>Complex</tt> class.
The first two, <tt>SetReal</tt> and <tt>SetImag</tt>,
simply assign new values to the <tt>real</tt> and <tt>imag</tt> member variables,
respectively.
<P>
<P><A NAME="57747"> </A><A NAME="progcomplex3c"> </A> <IMG WIDTH=575 HEIGHT=238 ALIGN=BOTTOM ALT="program57372" SRC="img2604.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img2604.gif" ><BR>
<STRONG>Program:</STRONG> <tt>Complex</tt> Class Mutators<BR>
<P>
<P>
Program <A HREF="page604.html#progcomplex3c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page604.html#progcomplex3c"><IMG ALIGN=BOTTOM ALT="gif" SRC="cross_ref_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/cross_ref_motif.gif"></A> also shows how
the assignment operator, <tt>operator=</tt>, can be
<em>overloaded</em><A NAME=57387> </A><A NAME=57388> </A>.
By overloading the assignment operator in this way
the function is called whenever
a complex number is assigned to another like this:
<PRE>c = d;</PRE>
<P>
If the programmer does not define an assignment operator explicitly,
the C++ compiler generates one.
The compiler-generated assignment operator simply assigns one-by-one
the member variables of the class
by invoking their respective assignment operators.
<P>
<HR><A NAME="tex2html9369" HREF="page605.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page605.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="next_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/next_motif.gif"></A> <A NAME="tex2html9367" HREF="page603.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page603.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="up_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/up_motif.gif"></A> <A NAME="tex2html9361" HREF="page603.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page603.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="previous_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/previous_motif.gif"></A> <A NAME="tex2html9371" HREF="page9.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page9.html"><IMG WIDTH=65 HEIGHT=24 ALIGN=BOTTOM ALT="contents" SRC="contents_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/contents_motif.gif"></A> <A NAME="tex2html9372" HREF="page620.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page620.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="index_motif.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/index_motif.gif"></A> <P><ADDRESS>
<img src="bruno.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/icons/bruno.gif" alt="Bruno" align=right>
<a href="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html \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://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/copyright.html">Copyright © 1997</a> by <a href="javascript:if(confirm('http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html \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://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html'" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/signature.html">Bruno R. Preiss, P.Eng.</a> All rights reserved.
</ADDRESS>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -