⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 page603.html

📁 wqeqwvrw rkjqhwrjwq jkhrjqwhrwq jkhrwq
💻 HTML
字号:
<HTML>
<HEAD>
<TITLE>Accessors and 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="tex2html9355" HREF="page604.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page604.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="tex2html9353" HREF="page596.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page596.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="tex2html9349" HREF="page602.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page602.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="tex2html9357" 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="tex2html9358" 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>
<H2><A NAME="SECTION0018330000000000000000">Accessors and Mutators</A></H2>
<P>
An <em>accessor</em><A NAME=57334>&#160;</A> is a member function that
accesses the contents of an object but does not modify that object.
In the simplest case,
an accessor just returns the value of one of the member variables.
In general,
an accessor performs some computation using the member variables
as long as that computation does not modify any of the member variables.
<P>
Program&nbsp;<A HREF="page603.html#progcomplex2c" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page603.html#progcomplex2c"><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 the five accessor member functions
of the <tt>Complex</tt> class--<tt>Real</tt>, <tt>Imag</tt>, <tt>R</tt>, <tt>Theta</tt> and <tt>Put</tt>.
The fact that these functions are accessors is indicated by the
<tt>const</tt> keyword at the end of the function declarator.
The <tt>const</tt> signifies that this member function is not permitted
to modify the object on which it operates.
In fact, the C++ compiler will not allow any operations
that could modify any of the member variables of the object.
<P>
<P><A NAME="57741">&#160;</A><A NAME="progcomplex2c">&#160;</A> <IMG WIDTH=575 HEIGHT=279 ALIGN=BOTTOM ALT="program57344" SRC="img2603.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img2603.gif"  ><BR>
<STRONG>Program:</STRONG> <tt>Complex</tt> Class Accessors<BR>
<P>
<P>
By defining suitable accessors,
it is possible to hide the implementation of the class from the user
of that class.
Consider the following statements:
<PRE>cout &lt;&lt; c.real &lt;&lt; endl;
cout &lt;&lt; c.Real() &lt;&lt; endl;</PRE>
The first statement depends on the implementation of the <tt>Complex</tt> class.
If we change the implementation of the class from
the one given (which uses rectangular coordinates)
to one that uses polar coordinates,
then the first statement above must also be changed.
On the other hand,
the second statement does not need to be modified,
provided we reimplement the <tt>Real</tt> member function
when we switch to polar coordinates.
<P>
<BR> <HR>
<UL> 
<LI> <A NAME="tex2html9359" HREF="page604.html#SECTION0018331000000000000000" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page604.html#SECTION0018331000000000000000">Mutators</A>
<LI> <A NAME="tex2html9360" HREF="page605.html#SECTION0018332000000000000000" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page605.html#SECTION0018332000000000000000">Member Access Control</A>
</UL>
<HR><A NAME="tex2html9355" HREF="page604.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page604.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="tex2html9353" HREF="page596.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page596.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="tex2html9349" HREF="page602.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page602.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="tex2html9357" 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="tex2html9358" 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 &#169; 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 + -