📄 right3-3.htm
字号:
对象的初始化</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">(1)
<font FACE="??ì?,SimSun" LANG="ZH-CN">构造函数和析构函数</font></font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">构造函数和析构函数是类中的两个具有特殊用途的成员函数。构造函数的作用是在创建对象时使用给定的值将对象初始化。析构函数的作用是用来释放一个对象,在对象被删除之前,由它来做一些清理工作。</font></p>
<font SIZE="3">
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">构造函数和析构函数与类同名,但析构函数前面必须加<font FACE="??ì?,SimSun" LANG="ZH-CN" SIZE="3" color="#FF6600"><b>~</b></font>号。</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">说明:</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">①
构造函数和析构函数都是成员函数,可以在内部定义也可以在外部定义。</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">②
构造函数和析构函数都不需要指定函数类型,也不允许有返回值。析构函数不需要参数。</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">③
构造函数可以重载,析构函数不允许重载。</font></p>
<font SIZE="3">
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">④
在程序中不能随意调用构造函数和析构函数,而只能由系统调用。</font></p>
<p style="line-height: 100%; text-indent: 0; margin: 0" align="left"> </p>
<p style="line-height: 100%; text-indent: 0; margin: 0" align="left"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN"><a href="example3-5.htm" target="_blank">【例</a></font><a href="example3-5.htm" target="_blank">3-5<font FACE="??ì?,SimSun" LANG="ZH-CN">】</font></a><font FACE="??ì?,SimSun" LANG="ZH-CN">分析下面程序的运行结果。</font></font></p>
<p style="line-height: 100%; text-indent: 0; margin: 0" align="left"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN"><a href="example3-6.htm" target="_blank">【例</a></font><a href="example3-6.htm" target="_blank">3-6<font FACE="??ì?,SimSun" LANG="ZH-CN">】</font></a><font FACE="??ì?,SimSun" LANG="ZH-CN">指出下面程序中的错误。</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">(2)
<font FACE="??ì?,SimSun" LANG="ZH-CN">构造函数的执行顺序</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN"><a href="example3-7.htm" target="_blank">【例</a></font><a href="example3-7.htm" target="_blank">3-7<font FACE="??ì?,SimSun" LANG="ZH-CN">】</font></a><font FACE="??ì?,SimSun" LANG="ZH-CN">请看下面程序的运行结果。</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">(3)
<font FACE="??ì?,SimSun" LANG="ZH-CN">析构函数的执行顺序</font></font></p>
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN"><a href="example3-8.htm" target="_blank">【例</a></font><a href="example3-8.htm" target="_blank">3-8<font FACE="??ì?,SimSun" LANG="ZH-CN">】</font></a><font FACE="??ì?,SimSun" LANG="ZH-CN">请看下面程序的运行结果。</font></font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"> </p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">(4)
<font FACE="??ì?,SimSun" LANG="ZH-CN">构造函数中的初始化表</font></font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">在构造函数中对数据成员进行初始化时,可以采用两种方法,一种是在函数体中使用赋值语句把表达式的值直接赋值给成员变量,<font color="#008000">另一种是用初始化表进行初始化</font>。</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">例如:类</font>Cdate<font FACE="??ì?,SimSun" LANG="ZH-CN">的构造函数</font></font></p>
<blockquote>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">Cdate(unsigned
y, unsigned m, unsigned d)</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">{</font></p>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">year=y;</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">month=m;</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">day=d;</font></p>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">}</font></p>
</blockquote>
</blockquote>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">可以表示成带有初始化表的构造函数</font></p>
<blockquote>
<blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2" color="#008000">Cdate(unsigned
y, unsigned m, unsigned d):year(y),month(m),day(d){}</font></p>
</blockquote>
</blockquote>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2">(5)
<font FACE="??ì?,SimSun" LANG="ZH-CN">重载构造函数</font></font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">成员函数除析构函数外都可以重载,下面举例说明如何重载构造函数。</font></p>
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN"><a href="example3-9.htm" target="_blank">【例</a></font><a href="example3-9.htm" target="_blank">3-9<font FACE="??ì?,SimSun" LANG="ZH-CN">】</font></a><font FACE="??ì?,SimSun" LANG="ZH-CN">分析下面程序的执行结果。</font></font></p>
<p style="line-height: 100%; text-indent: 0; margin: 0" align="left"> </p>
<p style="line-height: 100%; text-indent: 0; margin: 0" align="left"><font size="2">(6)
<font FACE="??ì?,SimSun" LANG="ZH-CN">默认构造函数和默认析构函数</font></font></p>
<p style="line-height: 100%; text-indent: 0; margin: 0" align="left"><font size="2" color="#FF6600"><font FACE="??ì?,SimSun" LANG="ZH-CN">注意:</font></font></p>
<p style="line-height: 100%; text-indent: 0; margin: 0" align="left"><font size="2" color="#FF6600"><font FACE="??ì?,SimSun" LANG="ZH-CN">用默认构造函数对对象初始化时,如果对象是局部的,系统将不做任何初始化工作;如果对象是全局的或静态的,系统将对所有数据成员都初始化为</font>0<font FACE="??ì?,SimSun" LANG="ZH-CN">或空。</font></font></p>
<p ALIGN="justify" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><font FACE="oúì?,SimHei" LANG="ZH-CN" size="2">4.
全局对象和静态对象</font></p>
<font SIZE="3">
<p ALIGN="JUSTIFY" style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">全局对象是在函数体外定义的对象,与全局变量一样,全局对象在主函数运行前被构造。</font></p>
<font SIZE="3">
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font FACE="??ì?,SimSun" LANG="ZH-CN" size="2">静态对象和静态变量一样,文件作用域的静态对象在主函数开始运行前构造完毕;块作用域的静态对象在首次进入到定义该静态对象的函数时进行构造。</font></p>
<p style="line-height: 100%; text-indent: 0; margin: 0" align="left"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN"><a href="example3-9.htm" target="_blank">【例</a></font><a href="example3-9.htm" target="_blank">3-10<font FACE="??ì?,SimSun" LANG="ZH-CN">】</font></a><font FACE="??ì?,SimSun" LANG="ZH-CN">指出下面程序的输出结果。</font></font></p>
<p ALIGN="justify" style="line-height: 200%; margin-top: 0; margin-bottom: 0"><font FACE="oúì?,SimHei" LANG="ZH-CN" size="2">5.
this指针</font></p>
<font SIZE="3">
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"></font><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN">一个类中的每个成员函数中都默认有一个所属类的指针参数,其名字由系统规定为</font>this<font FACE="??ì?,SimSun" LANG="ZH-CN">。</font></font></p>
<p style="line-height: 100%; text-indent: 0; margin: 0" align="left"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN"><a href="example3-11.htm" target="_blank">【例</a></font><a href="example3-11.htm" target="_blank">3-11<font FACE="??ì?,SimSun" LANG="ZH-CN">】</font></a><font FACE="??ì?,SimSun" LANG="ZH-CN">下面类的定义中显式使用了</font>this<font FACE="??ì?,SimSun" LANG="ZH-CN">指针。</font></font></p>
<p style="line-height: 100%; text-indent: 0; margin: 0" align="left"><font size="2"><font FACE="??ì?,SimSun" LANG="ZH-CN"><a href="example3-12.htm" target="_blank">【例</a></font><a href="example3-12.htm" target="_blank">3-12<font FACE="??ì?,SimSun" LANG="ZH-CN">】</font></a><font FACE="??ì?,SimSun" LANG="ZH-CN">指出下面程序的输出结果。</font></font></p>
<hr size="1" color="#008000">
<p style="line-height: 100%; margin-top: 0; margin-bottom: 0"> <span style="position: absolute; left: 30; top: 1763"><a href="right3-2.htm" target="_self"><img border="0" src="rightd1.gif" width="113" height="70"></a></span>
<span style="position: absolute; left: 491; top: 1772"><a href="right3-4.htm" target="_self"><img border="0" src="rightd2.gif" width="124" height="63"></a></span></p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -