构造函数和析构函数_第1页
构造函数和析构函数_第2页
构造函数和析构函数_第3页
构造函数和析构函数_第4页
构造函数和析构函数_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、构造函数和析构函数数 据 结 构 构造函数和析构函数 构造函数是在创建类的对象时自动调用的。 类的数据成员不能在定义类的时候初始化,而必须在类的构造函数中初始化,或者在建立类的对象后再设置它们的值。 构造函数不能自动返回值类型和返回值。可以重载构造函数以便用各种方法初始化类的对象。构造函数可以使用默认参数。 给构造函数提供默认参数的好处是:即使在调用构造函数的时候没有提供参数值,也会确保按照默认的参数值对对象进行初始化。 当一个构造函数所有参数都是默认参数时,该构造函数是默认的构造函数,也就是说,在调用这样的构造函数时可以不提供任何参数。例2-3 初始化类Time的对象 /TIME2.H/类T

2、ime的声明/成员函数在TIME2.cpp中定义# ifndef TIME2_H# define TIME2_Hclass Time public: Time(int=0, int=0, int=0); /默认构造函数 void SetTime(int,int,int); void printStandardTime(); private: int hour; int minute; int second;# endif/TIME2.cpp/类Time的成员函数的定义# include /初始化私有数据成员的构造函数/默认值是0Time: Time(int hr, int min, int s

3、ec) hour=(hr=0 & hr=0 & min=0 & sec=0 & h=0 & m=0 & s60)?s:0 ;void Time : printStandardTime() cout (hour=0 | hour=12)?12:hour % 12) “: “(minute10? “0“ : “)minute “: “(second10? “0“: “)second (hour12? “AM“: “PM“);# include # include “TIME2.H”main() Time t1,t2(2),t3(15,30),t4(12,20,40),t5(28,70,99); c

4、out“Constructed with all arguments defaulted:n“;t1.printStandardTime();cout“n hour specified; minute and second defaulted:n“;t2.printStandardTime();cout“n hour and minute specified; minute defaulted:n“; t3.printStandardTime(); cout“n hour, minute and second specified :n“;t4.printStandardTime();cout“

5、n all invalid values specified :n“;t5.printStandardTime();cout endl;return 0;输出结果:Constructed with all arguments defaulted12:00:00 AMhour specified; minute and second defaulted:2:00:00 AMhour and minute specified; minute defaulted:3:30:00 PMhour, minute and second specified :12:20:40 PMall invalid v

6、alues specified :12:00:00 AM 如果没有为类定义构造函数,那么编译器就会为该类添加一个默认构造函数。默认构造函数并不作任何初始化工作,所以在建立该类的对象时不能保证有一个确定的初始状态。 析构函数是类的一个特殊的成员函数,析构函数的标志是在类的名字的前面加上字符“ ”。 析构函数是构造函数的反向操作。当撤销一个类的对象时,该类的析构函数就自动被调用,在系统回收对象所占用的内存空间之前先做的一些清理工作。析构函数没有参数和返回值。 一个类只能有一个析构函数,重载析构函数是不允许的。 当类的对象包含动态分配的内存时,为类提供析构函数是合适的。 析构函数和构造函数通常是被自

7、动调用的,其调用顺序取决于建立和撤销对象的顺序。 一般来说,调用析构函数的顺序与调用构造函数的顺序相反。但是,对象的作用域会改变析构函数的顺序。对声明为全局的对象来说,其构造函数在声明该对象的时候调用, 相应的析构函数在程序退出该对象的作用域的时候被调用。 静态局部对象的构造函数只在声明该对象的时候调用一次,相应的析构函数在程序终止的时候被调用。 例2-4 类ConstructAndDestruct /CONSTRUCT.H/类ConstructAndDestruct的定义/成员函数在CONSTRUCT.CPP 中定义# ifndef CONSTRUCT_H# define CONSTRUCT

8、_Hclass ConstructAndDestruct public:ConstructAndDestruct(int); /构造函数ConstructAndDestruct(); /析构函数private:int data;# endif (接下页)/CONSTRUCT.CPP/类ConstructAndDestruct的成员函数的定义# include# include “construct.h“ConstructAndDestruct: ConstructAndDestruct(int value)data=value;cout“Object“data“constructor“;Con

9、structAndDestruct: ConstructAndDestruct()cout“Object“data“destructor“endl; / Example 2-4.CPP/演示构造函数和析构函数的调用顺序# include# include “construct.h“void construct(void); /函数原型ConstructAndDestruct first(1); /全局对象 (接下页)main () cout“(global construct before main) n“; ConstructAndDestruct second(2); /局部对象cout“

10、(local automatic in main) n“; static ConstructAndDestruct third(3); /局部对象 cout“(local static in main) n“; construct(); /调用建立对象的函数 ConstructAndDestruct fourth(4); /局部对象 cout“(local automatic in main) n“; return 0; (接下页)/建立函数的对象void construct (void) ConstructAndDestruct fifth(5);cout“(local automatic

11、in construct) n“;ConstructAndDestruct sixth(6);cout“(local static in construct) n“;ConstructAndDestruct seventh(7);cout“(local automatic in construct) n“;输出结果: Object 1 constructor (global construct before main)Object 2 constructor (local automatic in main)Object 3 constructor (local static in main)Object 5 constructor (local automatic in construct)Object 6 constructor (local static in construct)Object 7 constructor (local automatic in construct) (接下页)Object 7 destr

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论