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

📄 [19] inheritance basics, c++ faq lite.htm

📁 c++faq。里面有很多关于c++的问题的解答。
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0059)http://www.sunistudio.com/cppfaq/basics-of-inheritance.html -->
<HTML><HEAD><TITLE>[19] Inheritance basics, C++ FAQ Lite</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META http-equiv=Content-Language content=zh-cn>
<META content=basics-of-inheritance.html name=FILENAME>
<META content="[19] Inheritance basics, C++ FAQ Lite" name=ABSTRACT>
<META content=cline@parashift.com name=OWNER>
<META content="Marshall Cline, cline@parashift.com" name=AUTHOR>
<META content="MSHTML 6.00.2462.0" name=GENERATOR>
<META content=FrontPage.Editor.Document name=ProgId><LINK rev=made 
href="mailto:cline@parashift.com"><LINK 
href="[19] Inheritance basics, C++ FAQ Lite.files/cpp-faq.css" type=text/css 
rel=stylesheet></HEAD>
<BODY>
<H1><A name=top></A>[19] 继承 — 基础<BR><SMALL><SMALL>(Part of <A 
href="http://www.sunistudio.com/cppfaq/index.html"><EM>C++ FAQ Lite</EM></A>, <A 
href="http://www.sunistudio.com/cppfaq/copy-permissions.html#[1.2]">Copyright&nbsp;&copy; 
1991-2001</A>, <A href="http://www.parashift.com/" target=OutsideTheFAQ>Marshall 
Cline</A>, <A 
href="mailto:cline@parashift.com">cline@parashift.com</A>)</SMALL></SMALL></H1>
<P>简体中文版翻译:<A href="http://www.sunistudio.com/nicrosoft">申旻</A>,<A 
href="mailto:nicrosoft@sunistudio.com">nicrosoft@sunistudio.com</A>(<A 
href="http://www.sunistudio.com/">东日制作室</A>,<A 
href="http://www.sunistudio.com/asp/sunidoc.asp">东日文档</A>)</P>
<HR>

<H3>FAQs in section [19]:</H3>
<UL>
  <LI><A 
  href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html#[19.1]">[19.1] 
  对于C++,继承是否重要?</A> 
  <LI><A 
  href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html#[19.2]">[19.2] 
  何时该使用继承?</A> 
  <LI><A 
  href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html#[19.3]">[19.3] 
  在C++中如何表达继承?</A> <IMG alt=UPDATED! 
  src="[19] Inheritance basics, C++ FAQ Lite.files/updated.gif"> 
  <LI><A 
  href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html#[19.4]">[19.4] 
  将一个派生类型的指针转换成它的基类型可以吗?</A> 
  <LI><A 
  href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html#[19.5]">[19.5] 
  <TT>public:</TT>, <TT>private:</TT>和 <TT>protected:</TT>有什么不同?</A> 
  <LI><A 
  href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html#[19.6]">[19.6] 
  为什么派生类不能访问基类的<TT>private:</TT>成员?</A> 
  <LI><A 
  href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html#[19.7]">[19.7] 
  如何才能在改变类的内在部分时,保护其派生类不被破坏?</A> <IMG alt=UPDATED! 
  src="[19] Inheritance basics, C++ FAQ Lite.files/updated.gif"> </LI></UL>
<P>
<HR>

<P><A name=[19.1]></A>
<DIV class=FaqTitle>
<H3>[19.1] 对于C++,继承是否重要?</H3></DIV>
<P>是。 
<P>继承是面向对象编程和抽象数据类型(ADT)编程的区分标志 
<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html#top">Top</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html#bottom">Bottom</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/const-correctness.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/virtual-functions.html">Next&nbsp;section</A> 
]</SMALL> 
<HR>

<P><A name=[19.2]></A>
<DIV class=FaqTitle>
<H3>[19.2] 何时该使用继承?</H3></DIV>
<P>作为一种特化的机制。
<P>人们抽象事物有两种角度:“部分”和“种类”。Ford&nbsp;Taurus是一种(is-a-kind-of-a)汽车,并且&nbsp;Ford&nbsp;Taurus&nbsp;有(has-a)引擎,轮胎等。“部分”层次已经随着ADT风格而成为软件系统的一部分。继承则增加了另一种分解的角度。 

<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html#top">Top</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html#bottom">Bottom</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/const-correctness.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/virtual-functions.html">Next&nbsp;section</A> 
]</SMALL> 
<HR>

<P><A name=[19.3]></A>
<DIV class=FaqTitle>
<H3>[19.3] 在C++中如何表达继承? <IMG alt=UPDATED! 
src="[19] Inheritance basics, C++ FAQ Lite.files/updated.gif"></H3></DIV><SMALL><EM>[Recently 
added "derived class of" to the list of synonyms (on 7/00). <A 
href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html#[19.7]">Click 
here to go to the next FAQ in the "chain" of recent changes<!--rawtext:[19.7]:rawtext--></A>.]</EM></SMALL> 
<P>通过 <TT>:public</TT>&nbsp; 语法: 
<P>
<DIV 
class=CodeBlock><TT>&nbsp;class&nbsp;Car&nbsp;:&nbsp;public&nbsp;Vehicle&nbsp;{<BR>&nbsp;public:<BR>&nbsp;&nbsp;&nbsp;</TT><EM>//&nbsp;...</EM><TT><BR>&nbsp;}; 
</TT></DIV>
<P>我们有几种方式声明以上的关系:<BR>
<UL>
  <LI><TT>Car</TT> 
  是&nbsp;“一种”("a&nbsp;kind&nbsp;of&nbsp;a")<TT>Vehicle(交通工具)</TT> 
  <LI><TT>Car</TT> 起源于("derived&nbsp;from")<TT>Vehicle</TT> 
  <LI><TT>Car</TT> 是一种特殊化的("a&nbsp;specialized") <TT>Vehicle</TT> 
  <LI><TT>Car</TT> 是<TT>Vehicle</TT>的一个子类("subclass") 
  <LI><TT>Car</TT> 是<TT>Vehicle</TT>的一个派生类(“derived class”) 
  <LI><TT>Vehicle</TT> 是<TT>Car</TT>的基类("base&nbsp;class") 
  <LI><TT>Vehicle</TT> 是<TT>Car</TT> 的超类("superclass")(这在C++ 社群中不常用) </LI></UL>
<P>(注意:&nbsp;本&nbsp;FAQ&nbsp;的论述仅与公有继承(<TT>public</TT> inheritance)有关;&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/private-inheritance.html">私有和保护继承</A>并不相同) 

<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html#top">Top</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html#bottom">Bottom</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/const-correctness.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/virtual-functions.html">Next&nbsp;section</A> 
]</SMALL> 
<HR>

<P><A name=[19.4]></A>
<DIV class=FaqTitle>
<H3>[19.4] 将一个派生类型的指针转换成它的基类型可以吗?</H3></DIV>
<P>可以。<BR><BR>派生类对象是基类对象的一种。因此从派生类指针到基类指针的转换是非常安全的,并且始终会发生。例如,如果有一个&nbsp;car&nbsp;类型的指针,而实际上指向了&nbsp;vehicle,这种从&nbsp;<TT>Car*</TT>&nbsp;到&nbsp;<TT>Vehicle* 
</TT>的转换是非常安全的和常规的:<BR>
<DIV 
class=CodeBlock><TT>&nbsp;void&nbsp;f(Vehicle*&nbsp;v);<BR>&nbsp;void&nbsp;g(Car*&nbsp;c)&nbsp;{&nbsp;f(c);&nbsp;}&nbsp;&nbsp;</TT><EM>// 
</EM><I>非常安全;&nbsp;不用转换</I> </DIV>
<P>(注意:&nbsp;本&nbsp;FAQ&nbsp;的论述仅与公有继承(<TT>public</TT> inheritance)有关;&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/private-inheritance.html">私有和保护继承</A>并不相同) 

<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html#top">Top</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html#bottom">Bottom</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/const-correctness.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/virtual-functions.html">Next&nbsp;section</A> 
]</SMALL> 
<HR>

<P><A name=[19.5]></A>
<DIV class=FaqTitle>
<H3>[19.5] <TT>public:</TT>, <TT>private:</TT>和 
<TT>protected:</TT>有什么不同?</H3></DIV>
<UL>
  <LI>在类的<TT>private:</TT>节中声明的成员(无论数据成员或是成员函数)仅仅能被类的成员函数和<A 
  href="http://www.sunistudio.com/cppfaq/friends.html">友元</A>访问。 
  <LI>在类的<TT>protected:</TT> 节中声明的成员(无论数据成员或是成员函数)仅仅能被类的成员函数,<A 
  href="http://www.sunistudio.com/cppfaq/friends.html">友元</A>以及子类的成员函数和<A 
  href="http://www.sunistudio.com/cppfaq/friends.html">友元</A>访问。 
  <LI>在类的<TT>public:</TT>节中声明的成员(无论数据成员或是成员函数)能被任何人访问。 </LI></UL>
<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html#top">Top</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html#bottom">Bottom</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/const-correctness.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/virtual-functions.html">Next&nbsp;section</A> 
]</SMALL> 
<HR>

<P><A name=[19.6]></A>
<DIV class=FaqTitle>
<H3>[19.6] 为什么派生类不能访问基类的<TT>private:</TT>成员?</H3></DIV>
<P>为了使派生类在将来基类改变时不受影响。<BR><BR>派生类无法访问基类的私有成员。这样在对基类私有成员作任何改变时,就有效地锁定了派生类。 
<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html#top">Top</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html#bottom">Bottom</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/const-correctness.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/virtual-functions.html">Next&nbsp;section</A> 
]</SMALL> 
<HR>

<P><A name=[19.7]></A>
<DIV class=FaqTitle>
<H3>[19.7] 如何才能在改变类的内在部分时,保护其派生类不被破坏?<IMG alt=UPDATED! 
src="[19] Inheritance basics, C++ FAQ Lite.files/updated.gif"></H3></DIV><SMALL><EM>[Recently 
renamed "subclass" to "derived class" (on 7/00). <A 
href="http://www.sunistudio.com/cppfaq/proper-inheritance.html#[21.4]">Click 
here to go to the next FAQ in the "chain" of recent changes<!--rawtext:[21.4]:rawtext--></A>.]</EM></SMALL> 
<P>类有两套截然不同的接口,它们分别面向两个截然不同的客户:</P>
<UL>
  <LI>有为无关类服务的<TT>public:</TT> 接口 
  <LI>有为派生类服务的<TT>protected:</TT>&nbsp;接口 </LI></UL>
<P>除非你期望你的所有子类全部由你自己的团队建立,否则你应该考虑让基类部分成为<TT>private:</TT>,并且用&nbsp;<TT>protected:</TT>来内联供子类访问基类私有数据的访问函数。使用这种方法,私有部分可以被改变,但是派生类的代码不会被破坏(除非你改变了<TT>protected</TT>的访问函数)。 

<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html#top">Top</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html#bottom">Bottom</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/const-correctness.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/virtual-functions.html">Next&nbsp;section</A> 
]</SMALL> 
<HR>

<P><A name=bottom></A><A href="mailto:cline@parashift.com"><IMG height=26 
alt=E-Mail src="[19] Inheritance basics, C++ FAQ Lite.files/mbox.gif" 
width=89>&nbsp;E-mail the author</A><BR>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/index.html"><EM>C++ FAQ Lite</EM></A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/index.html#table-of-contents">Table&nbsp;of&nbsp;contents</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/subject-index.html">Subject&nbsp;index</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/copy-permissions.html#[1.1]">About&nbsp;the&nbsp;author</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/copy-permissions.html#[1.2]">&copy;</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/on-line-availability.html#[2.2]">Download&nbsp;your&nbsp;own&nbsp;copy</A>&nbsp;]<BR><SMALL>Revised 
Apr 8, 2001</SMALL> </P></BODY></HTML>

⌨️ 快捷键说明

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