📄 page125.html
字号:
<HTML>
<HEAD>
<TITLE>Associations</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="tex2html3453" HREF="page126.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page126.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="tex2html3451" HREF="page109.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page109.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="tex2html3445" HREF="page124.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page124.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="tex2html3455" 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="tex2html3456" 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="SECTION0062110000000000000000">Associations</A></H2>
<A NAME="secadtsassociations"> </A>
<P>
An association is an ordered pair of objects.
The first element of the pair is called the <em>key</em><A NAME=5211> </A>;
the second element is the <em>value</em><A NAME=5213> </A>
associated with the given key.
<P>
Associations are useful for storing
information in a database<A NAME=5214> </A> for later retrieval.
E.g., a database can be viewed as a collection of key-and-value pairs.
The information associated with a given key
is retrieved from the database by searching the database
for an the ordered pair in which the key matches the given key.
<P>
An association is like a container in that it contains two objects--the key and the value.
On the other hand,
an association is fundamentally different from a container
since the contained objects are treated very differently.
Normally, the objects in a container are not distinguished
one from another.
In an association, the two contained objects are distinct--one of them is <em>the key</em>
and the other is a value <em>associated with</em> the key.
<P>
Program <A HREF="page125.html#progassoch" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page125.html#progassoch"><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> gives the declaration
of the concrete object class <tt>Association</tt>.
The declaration of the <tt>Association</tt> class
is like that of <tt>Container</tt> class
in that both of them are derived from the same set of base classes--<tt>Object</tt> and <tt>Ownership</tt>.
<P>
<P><A NAME="5546"> </A><A NAME="progassoch"> </A> <IMG WIDTH=575 HEIGHT=353 ALIGN=BOTTOM ALT="program5223" SRC="img699.gif" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/img699.gif" ><BR>
<STRONG>Program:</STRONG> <tt>Association</tt> Class Definition<BR>
<P>
<P>
An association has two member variables,
<tt>key</tt> and <tt>value</tt>,
both of which are of type <em>pointer</em> to <tt>Object</tt>.
Since pointers are used,
the <tt>Association</tt> class implements <em>indirect containment</em>.
<P>
<BR> <HR>
<UL>
<LI> <A NAME="tex2html3457" HREF="page126.html#SECTION0062111000000000000000" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page126.html#SECTION0062111000000000000000">Implementation</A>
</UL>
<HR><A NAME="tex2html3453" HREF="page126.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page126.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="tex2html3451" HREF="page109.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page109.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="tex2html3445" HREF="page124.html" tppabs="http://dictator.uwaterloo.ca/Bruno.Preiss/books/opus4/html/page124.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="tex2html3455" 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="tex2html3456" 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 + -