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

📄 einherc.htm

📁 一个非常适合初学者入门的有关c++的文档
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN" "http://www.w3.org/TR/REC-html40/frameset.dtd">
<HTML LANG="EN">
<HEAD>
<title>Effective C++, 2E | Inheritance and Object-Oriented Design</TITLE>
<LINK REL=STYLESHEET HREF=../INTRO/ECMEC.CSS>

<SCRIPT LANGUAGE="Javascript" SRC="../JAVA/COOKIE.JS"></SCRIPT>
<SCRIPT LANGUAGE="Javascript">var imagemax = 0; setCurrentMax(0);</SCRIPT>
<SCRIPT LANGUAGE="Javascript" SRC="../JAVA/DINGBATS.JS"></SCRIPT>
<SCRIPT>
var dingbase = "EINHEDIR.HTM";
var dingtext = "EC++ Inheritance/OOD, P";
if (self == top) {
 top.location.replace(dingbase + this.location.hash);
}
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" ONLOAD="setResize()">
<!-- SectionName="EC++ Chapter Intro: Inheritance and OOD" -->
<DIV ALIGN="CENTER"><FONT SIZE="-1">Back to <A HREF="./EI34_FR.HTM" TARGET="_top">Item 34: Minimize compilation dependencies between files.</A> &nbsp;&nbsp;<BR>&nbsp;&nbsp;Continue to <A HREF="./EI35_FR.HTM" TARGET="_top">Item 35: Make sure public inheritance models "isa."</A></FONT></DIV>

<P><A NAME="dingp1"></A><A NAME="p153"></A><FONT ID="egtitle">Inheritance and Object-Oriented Design</FONT><SCRIPT>create_link(1);</SCRIPT>
</P>

<A NAME="6896"></A>
<A NAME="6898"></A>
<P><A NAME="dingp2"></A>
Many people are of the opinion that inheritance is what object-oriented programming is all about. Whether that's so is debatable, but the number of Items in the other sections of this book should convince you that as far as effective C++ programming is concerned, you have a lot more tools at your disposal than simply specifying which classes inherit from which other <NOBR>classes.<SCRIPT>create_link(2);</SCRIPT>
</NOBR></P>
<A NAME="6899"></A>
<P><A NAME="dingp3"></A>
Still, designing and implementing class hierarchies is fundamentally different from anything found in the world of C. Certainly it is in the area of inheritance and object-oriented design that you are most likely to radically rethink your approach to the construction of software systems. Furthermore, C++ provides a bewildering assortment of object-oriented building blocks, including public, protected, and private base classes; virtual and nonvirtual base classes; and virtual and nonvirtual member functions. Each of these features interacts not only with one another, but also with the other components of the language. As a result, trying to understand what each feature means, when it should be used, and how it is best combined with the non-object-oriented aspects of C++ can be a daunting <NOBR>endeavor.<SCRIPT>create_link(3);</SCRIPT>
</NOBR></P>
<A NAME="6900"></A>
<P><A NAME="dingp4"></A>
Further complicating the matter is the fact that different features of the language appear to do more or less the same thing. Examples:<SCRIPT>create_link(4);</SCRIPT>

<UL><A NAME="6901"></A>
<A NAME="dingp5"></A><LI>You need a collection of classes with many shared characteristics. Should you use inheritance and have all the classes derived from a common base class, or should you use templates and have them all generated from a common code skeleton?<SCRIPT>create_link(5);</SCRIPT>

<A NAME="6902"></A>
<A NAME="dingp6"></A><LI>Class A is to be implemented in terms of class B. Should A have a data member of type B, or should A privately inherit from B?<SCRIPT>create_link(6);</SCRIPT>

<A NAME="6903"></A>
<A NAME="dingp7"></A><LI><A NAME="p154"></A>You need to design a type-safe homogeneous container class, one not present in the standard library. (See <A HREF="./EI49_FR.HTM#8392" TARGET="_top">Item 49</A> for a list of containers the library <I>does</I> provide.) Should you use templates, or would it be better to build type-safe interfaces around a class that is itself implemented using generic (<CODE>void*</CODE>) pointers?<SCRIPT>create_link(7);</SCRIPT>

</UL></P>
<A NAME="6904"></A>
<P><A NAME="dingp8"></A>
In the Items in this section, I offer guidance on how to answer questions such as these. However, I cannot hope to address every aspect of object-oriented design. Instead, I concentrate on explaining what the different features in C++ really <I>mean</I>, on what you are really <I>saying</I> when you use a particular feature. For example, public inheritance means "isa" (see <A HREF="./EI35_FR.HTM#6914" TARGET="_top">Item 35</A>), and if you try to make it mean anything else, you will run into trouble. Similarly, a virtual function means "interface must be inherited," while a nonvirtual function means "both interface <I>and</I> implementation must be inherited." Failing to distinguish between these meanings has caused many a C++ programmer untold <NOBR>grief.<SCRIPT>create_link(8);</SCRIPT>
</NOBR></P>
<A NAME="6908"></A>
<P><A NAME="dingp9"></A>
If you understand the meanings of C++'s varied features, you'll find that your outlook on object-oriented design shifts. Instead of it being an exercise in differentiating between language constructs, it will properly become a matter of figuring out what it is you want to say about your software system. Once you know what you want to say, you'll be able to translate that into the appropriate C++ features without too much <NOBR>difficulty.<SCRIPT>create_link(9);</SCRIPT>
</NOBR></P>
<A NAME="6909"></A>
<P><A NAME="dingp10"></A>
The importance of saying what you mean and understanding what you're saying cannot be overestimated. The items that follow provide a detailed examination of how to do this effectively. <A HREF="./EI44_FR.HTM#8113" TARGET="_top">Item 44</A> summarizes the correspondence between C++'s object-oriented constructs and what they mean. It serves as a nice capstone for this section, as well as a concise reference for future <NOBR>consultation.<SCRIPT>create_link(10);</SCRIPT>
</NOBR></P>

<DIV ALIGN="CENTER"><FONT SIZE="-1">Back to <A HREF="./EI34_FR.HTM" TARGET="_top">Item 34: Minimize compilation dependencies between files.</A> &nbsp;&nbsp;<BR>&nbsp;&nbsp;Continue to <A HREF="./EI35_FR.HTM" TARGET="_top">Item 35: Make sure public inheritance models "isa."</A></FONT></DIV>

</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -