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

📄 [7] classes and objects, 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=(0057)http://www.sunistudio.com/cppfaq/classes-and-objects.html -->
<HTML><HEAD><TITLE>[7] Classes and objects, C++ FAQ Lite</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META http-equiv=Content-Language content=zh-cn>
<META content=classes-and-objects.html name=FILENAME>
<META content="[7] Classes and objects, 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="[7] Classes and objects, C++ FAQ Lite.files/cpp-faq.css" type=text/css 
rel=stylesheet></HEAD>
<BODY>
<H1><A name=top></A>[7] 类和对象<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 [7]:</H3>
<UL>
  <LI><A 
  href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#[7.1]">[7.1] 
  类是什么</A><A 
  href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#[7.1]">?</A> 
  <LI><A 
  href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#[7.2]">[7.2] 
  对象是什么?</A> 
  <LI><A 
  href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#[7.3]">[7.3] 
  什么样的接口是“好”的?</A> 
  <LI><A 
  href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#[7.4]">[7.4] 
  封装是什么?</A> 
  <LI><A 
  href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#[7.5]">[7.5] 
  C++是如何在安全性和可用性间取得平衡的?</A> 
  <LI><A 
  href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#[7.6]">[7.6] 
  </A><A 
  href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#[7.6]">我如何才能防止其它程序员查看我的类的私有部分而破坏封装?</A> 

  <LI><A 
  href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#[7.7]">[7.7] 
  封装是一种安全装置吗?</A> 
  <LI><A 
  href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#[7.8]">[7.8] 
  关键字 struct 和 class 有什么区别?</A> </LI></UL>
<P>
<HR>

<P><A name=[7.1]></A>
<DIV class=FaqTitle>
<H3>[7.1] 类是什么?</H3></DIV>
<P>面向对象软件的基本组成物<BR><BR>类定义数据类型,就如同 C 中的结构。从计算机科学的角度来理解,类型由状态集合和转换这些状态的操作集合组成。因为 
<TT>int</TT> 既有状态集合,也有象 <TT>i&nbsp;+&nbsp;j</TT> 或 <TT>i++</TT> 等这样的操作,所以 
<TT>int</TT> 是一种类型。同样,类提供了一组操作集合(通常是 <TT>public:</TT> 
)和一组描述类型实例所拥有的抽象值的数据集合。<BR><BR>可以将 <TT>int</TT> 看作为一个有 operator++ 
等成员函数的类。(<TT>int</TT> 实际并不是一个类,但是基本类似:一个类是一种类型,就如同 <TT>int</TT> 
是一种类型)<BR><BR>注意: C 程序员可以将类看作为成员默认为私有的结构。但是,如果那是你对类的全部认识,那么你可能要经历个人的典范转变了。 
<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#top">Top</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#bottom">Bottom</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/big-picture.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/references.html">Next&nbsp;section</A> 
]</SMALL> 
<HR>

<P><A name=[7.2]></A>
<DIV class=FaqTitle>
<H3>[7.2] 对象是什么?</H3></DIV>
<P>和语义有关的存储区域<BR><BR>当我们声明了<TT>int&nbsp;i</TT>,我们说:“<TT>i</TT>&nbsp;是&nbsp;<TT>int 
</TT>类型的一个对象”。在&nbsp;OO/C++&nbsp;中,“对象”通常意味着“类的一个实例”。因此,类定义多个对象(实例)的可能的行为。 
<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#top">Top</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#bottom">Bottom</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/big-picture.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/references.html">Next&nbsp;section</A> 
]</SMALL> 
<HR>

<P><A name=[7.3]></A>
<DIV class=FaqTitle>
<H3>[7.3] 什么样的接口是“好”的?</H3></DIV>
<P>提供了一个将“块”状的软件简化了的视图,并且以“用户”的词汇表达的接口。(“块”通常是一个或<A 
href="http://www.sunistudio.com/cppfaq/friends.html#[14.2]">一组紧密相连的类</A>;“用户”是指其它的开发者而不是最终客户)
<P>  
<UL>
  <LI>“简化了的视图”指隐藏不必要的细节。这样可以减少用户的错误率。 
  <LI>“用户的词汇”指用户不需要学习新的词汇或概念,这样可以降低用户的学习曲线。 </LI></UL>
<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#top">Top</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#bottom">Bottom</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/big-picture.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/references.html">Next&nbsp;section</A> 
]</SMALL> 
<HR>

<P><A name=[7.4]></A>
<DIV class=FaqTitle>
<H3>[7.4] 封装是什么?</H3></DIV>
<P>防止未授权地访问一些信息和功能。
<P>省钱的关键是从软件“块”的稳定部分中分离出可变的部分。封装给这个“块”安置了防火墙,它可以防止其它“块”访问可变的部分;其它“块”仅仅能够访问稳定的部分。这样做,当可变的部分改变后,可以防止其它“块”被破坏。在面向对象软件的概念中,“块(chunk)”通常指一个或<A 
href="http://www.sunistudio.com/cppfaq/friends.html#[14.2]">一组紧密相连的类</A>。
<P>“可变的部分”是实现的细节。如果“块”是单个类,那么可变的部分通常用 <A 
href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html#[19.5]"><TT>private:</TT> 
和/或 <TT>protected:</TT> 关键字</A>来封装。如果“块”是<A 
href="http://www.sunistudio.com/cppfaq/friends.html#[14.2]">一组紧密相连的类</A>,封装可被用来拒绝对组中全部类的访问。<A 
href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html">继承</A>也能被用来作为<A 
href="http://www.sunistudio.com/cppfaq/abcs.html#[22.2]">封装的一种形式</A>。
<P>“稳定的部分”是接口。好的接口提供了一个<A 
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#[7.3]">以用户的词汇简化了的视图</A>,并且被<A 
href="http://www.sunistudio.com/cppfaq/operator-overloading.html#[13.10]">从外到里的设计</A>。(此处的“用户”是指其它开发者,而不是购买完整应用的最终用户)。如果“块”是单个类,接口仅仅是类的 
public: 成员函数和<A 
href="http://www.sunistudio.com/cppfaq/friends.html">友元</A>函数。如果“块”是<A 
href="http://www.sunistudio.com/cppfaq/friends.html#[14.2]">一组紧密相连的类</A>,那么接口可以包括模块中的多个类。 

<P>设计一个清晰的接口并且<A 
href="http://www.sunistudio.com/cppfaq/abcs.html#[22.1]">将实现和接口分离</A>,只不过是允许用户使用接口。而封装实现可以强迫用户使用接口。 

<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#top">Top</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#bottom">Bottom</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/big-picture.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/references.html">Next&nbsp;section</A> 
]</SMALL> 
<HR>

<P><A name=[7.5]></A>
<DIV class=FaqTitle>
<H3>[7.5] C++是如何在安全性和可用性间取得平衡的?</H3></DIV>
<P>在 C 中,<A 
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#[7.4]">封装</A>是通过在编辑单元或模块中,将对象声明为静态来完成的。这样做防止了其他模块访问静态区域。(顺便说一句,现在这种做法是被遗弃的:不要在 
C++中这样做) 
<P>不幸的是,由于没有对一个模块的静态数据产生多个实例的直接支持,这种处理方法不支持数据的多个实例。在 C 
中如果需要多个实例,那么程序员一般使用结构。但是很不幸,C 的结构不支持<A 
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#[7.4]">封装</A>。这增加了在安全性(信息隐藏)和可用性(多实例)之间取得平衡的难度。 

<P>在 C++中,你可以利用类来同时获得多实例和封装性。类的 public: 部分包含了类的接口,它们通常由类的 public: 成员函数和它的<A 
href="http://www.sunistudio.com/cppfaq/friends.html">友元</A>函数组成。类的 <A 
href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html#[19.5]"><TT>private:</TT> 
和/或 <TT>protected:</TT></A> 部分包含了类的实现,而通常数据就在这里。
<P>最终的结果就象是“封装了的结构”。这样就易于在安全性(信息隐藏)和可用性(多实例)间取得平衡。 
<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#top">Top</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#bottom">Bottom</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/big-picture.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/references.html">Next&nbsp;section</A> 
]</SMALL> 
<HR>

<P><A name=[7.6]></A>
<DIV class=FaqTitle>
<H3>[7.6] 我如何才能防止其它程序员查看我的类的私有部分而破坏封装?</H3></DIV>
<P>不必这么做——封装是对于代码而言的,而不是对人。 
<P>只要其它程序员写的代码不依赖于他们的所见,那么即使它们看了你的类的 <A 
href="http://www.sunistudio.com/cppfaq/basics-of-inheritance.html#[19.5]"><TT>private:</TT> 
和/或 <TT>proteced:</TT></A> 
部分,也不会破坏封装。换句话说,封装不会阻止人认识类的内部。封装只是阻止他们写出依赖类内部实现的代码。你的公司不必为维护你眼睛看到的支付维护成本,但是必须为维护你的指尖写出的代码支付维护成本。正如你知道的,倘若他们写的代码依赖于接口而不是实现,就不会增加维护成本。
<P>此外,这很少成为一个问题。我想不会有故意试图访问类的私有部分的程序员。My recommendation in such cases would be 
to change the programmer, not the code" [<A 
href="mailto:kanze@gabi-soft.fr">James Kanze</A>; used with permission]. 
<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#top">Top</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#bottom">Bottom</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/big-picture.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/references.html">Next&nbsp;section</A> 
]</SMALL> 
<HR>

<P><A name=[7.7]></A>
<DIV class=FaqTitle>
<H3>[7.7] 封装是一种安全装置吗?</H3></DIV>
<P>不。 
<P>封装 != 安全。 
<P>封装要防止的是错误,而不是间谍。 
<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#top">Top</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#bottom">Bottom</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/big-picture.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/references.html">Next&nbsp;section</A> 
]</SMALL> 
<HR>

<P><A name=[7.8]></A>
<DIV class=FaqTitle>
<H3>[7.8] 关键字 struct 和 class 有什么区别?</H3></DIV>
<P>struct 的成员默认是公有的,而类的成员默认是私有的。注意:你应该明白地声明你的类成员为公有的、私有的、或者是保护的,而不是依赖于默认属性 
<P>struct 和 class 在其他方面是功能相当的。 
<P>好,清晰的技术谈论够多了。从感情上讲,大多数的开发者感到类和结构有很大的差别。感觉上结构仅仅象一堆缺乏封装和功能的开放的内存位,而类就象活的并且可靠的社会成员,它有智能服务,有牢固的封装屏障和一个良好定义的接口。既然大多数人都这么认为,那么只有在你的类有很少的方法并且有公有数据(这种事情在良好设计的系统中是存在的!)时,你也许应该使用 
struct 关键字,否则,你应该使用 class 关键字。&nbsp; 
<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#top">Top</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html#bottom">Bottom</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/big-picture.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/references.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="[7] Classes and objects, 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 + -