📄 page597.html
字号:
<HTML>
<HEAD>
<TITLE>Member Variables and Member Functions</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="tex2html9283" HREF="page598.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page598.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="tex2html9281" 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="tex2html9275" HREF="page596.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page596.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="tex2html9285" 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="tex2html9286" 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="SECTION0018310000000000000000">Member Variables and Member Functions</A></H2>
<P>
A class groups a set of values and a set of operations.
The values and the operations of a class are called its <em>members</em>.
<em>Member variables</em> implement the values
and <em>member functions</em> implement the operations.
<P>
Suppose we wish to define a class to represent
<em>complex numbers</em><A NAME=57225> </A>.
The <tt>Complex</tt> class definition shown in Program <A HREF="page597.html#progcomplex1h" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page597.html#progcomplex1h"><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>
illustrates how this can be done.
Two member variables, <tt>real</tt> and <tt>imag</tt>, are declared.
These represent the real and imaginary parts
of a complex numbers (respectively).
<P>
<P><A NAME="57726"> </A><A NAME="progcomplex1h"> </A> <IMG WIDTH=575 HEIGHT=410 ALIGN=BOTTOM ALT="program57230" SRC="img2601.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img2601.gif" ><BR>
<STRONG>Program:</STRONG> <tt>Complex</tt> Class Definition<BR>
<P>
<P>
Every object instance of the <tt>Complex</tt>
class contains its own member variables.
Consider the following variable declarations:
<PRE>Complex c;
Complex d;</PRE>
Both <tt>c</tt> and <tt>d</tt> are instances of the complex class.
Therefore, each of them has a <tt>real</tt> and <tt>imag</tt> member variable.
The member variables of an object are accessed using the <em>dot</em> operator.
E.g., <tt>c.real</tt> refers to the <tt>real</tt> member variable of <tt>c</tt>
and <tt>d.imag</tt> refers to the <tt>imag</tt> member of <tt>d</tt>.
<P>
The <tt>Complex</tt> class definition also contains
a list of function prototypes
which declare the <em>member functions</em> of the class.
In general,
a member function performs some operation on an instances of the class.
Again, the <em>dot</em> operator is used to specify the object on which
the operation is performed.
E.g., <tt>c.SetReal(1.0)</tt> invokes the <tt>SetReal</tt> function on <tt>c</tt>
and <tt>d.Put(cout)</tt> invokes the <tt>Put</tt> function on <tt>d</tt>.
<P>
<HR><A NAME="tex2html9283" HREF="page598.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page598.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="tex2html9281" 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="tex2html9275" HREF="page596.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page596.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="tex2html9285" 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="tex2html9286" 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 + -