析构函数

       构造函数(Constructor):随着对象的创建而自动被调用

       析构函数(Destructor):随着对象的消亡而自动被调用

 

        析构函数是一种随着对象消亡而自动被调用的函数,它的主要用途是释放动态申请的资源。它没有返回类型,没有参数,也没有重载。析构函数的函数名也是指定的,是在构造函数名之前加一个~符号。


链表结点类:

//node.h
class Node//定义一个链表结点类
{
private:
   int idata;//存储数据
   char cdata;//存储数据
   Node* prior;//前驱结点
   Node* next;//后继结点
public:
   Node();//构造函数的声明
   Node(Node&);//结点拷贝构造函数
   Node(int,char ='0');//构造函数重载1
   Node(int,char,Node*,Node*);//构造函数重载2
   ~Node();//结点析构函数
   int readi() const;//读取idata
   char readc() const;//读取cdata
   Node* readp() const;//读取上一个结点的位置
   Node* readn() const;//读取下一个结点的位置
   bool set(int);//重载,通过该函数修改idata
   bool set(char);//重载,通过该函数修改cdata
   bool setp(Node*);//通过该函数设置前驱结点
   bool setn(Node*);//通过该函数设置后继结点
};
//node.cpp
#include"node.h"
#include<iostream>

using namespace std;
int Node::readi() const//成员函数的定义
{
   return idata;
}
char Node::readc() const
{
   return cdata;
}
Node* Node::readp() const
{
   return prior;
}
Node* Node::readn() const
{
   return next;
}
bool Node::set(int i)//重载成员函数定义
{
   idata=i;
   return true;
}
bool Node::set(char c)
{
   cdata=c;
   return true;
}
bool Node::setp(Node* p)
{
   prior=p;
   return true;
}
bool Node::setn(Node* n)
{
   next=n;
   return true;
}
Node::Node()//构造函数0的定义
{
   cout<<"Node constructor is running..."<<endl;//提示构造函数运行
   idata=0;//初始化idata
   cdata='0';//初始化cdata
   prior=NULL;//初始化前驱结点指针
   next=NULL;//初始化后续结点指针
}
Node::Node(int i,char c)//构造函数重载1,默认参数只需要在函数原型中出现
{
   cout<<"Node constructor is running..."<<endl;
   idata=i;
   cdata=c;
   prior=NULL;
   next=NULL;
}
Node::Node(int i,char c,Node* p,Node* n)//构造函数重载2
{
   cout<<"Node constructor is running..."<<endl;
   idata=i;
   cdata=c;
   prior=p;
   next=n;
}
Node::Node(Node& n)
{
   idata=n.idata;//可以读出同类对象的私有成员数据
   cdata=n.cdata;
   prior=n.prior;
   next=n.next; 
}
Node::~Node()//析构函数
{
   cout<<"Node destructor is running..."<<endl;
}
//linklist.h
#include"node.h"//需要使用链表结点类
class Linklist
{
private:
   Node head;//头结点
   Node* pcurrent;//当前结点指针
public:
   Linklist(int,char);//链表类构造函数
   Linklist(Linklist&);//链表深拷贝构造函数
   ~Linklist();//链表析构函数
   bool Locate(int);//根据整数查找结点
   bool Locate(char);//根据字符查找结点
   bool Insert(int =0,char ='0');//在当前结点之后插入结点
   bool Delete();//删除当前结点
   void Show();//显示链表所有数据
   void Destroy();//清除整个链表
};
#include"linklist.h"
#include<iostream>

using namespace std;
Linklist::Linklist(int i,char c):head(i,c)//类名::构造函数名(参数表):成员对象名1(参数表),链表类构造函数,调用head对象的构造函数重载1
{
   cout<<"Linklist constructor is running..."<<endl;
   pcurrent=&head;
}
bool Linklist::Locate(int i)
{
   Node* ptemp=&head;
   while(ptemp!=NULL)
   {
      if(ptemp->readi()==i)
      {
         pcurrent=ptemp;//将当前结点指针指向找到的结点
         return true;
      }
      ptemp=ptemp->readn();//查找下一个结点
   }
   return false;
}
bool Linklist::Locate(char c)
{
   Node* ptemp=&head;
   while(ptemp!=NULL)
   {
      if(ptemp->readc()==c)
      {
         pcurrent=ptemp;
         return true;
      }
      ptemp=ptemp->readn();
   }
   return false;
}
bool Linklist::Insert(int i,char c)
{
   if(pcurrent!=NULL)
   {
      Node* temp=new Node(i,c,pcurrent,pcurrent->readn());//调用Node类构造函数重载2
      if (pcurrent->readn()!=NULL)
      {
         pcurrent->readn()->setp(temp);
      }
      pcurrent->setn(temp);
      return true;
   }
   else
   {
      return false;
   }
}
bool Linklist::Delete()
{
   if(pcurrent!=NULL&&pcurrent!=&head)//head结点不能删除
   {
      Node* temp=pcurrent;
      if(temp->readn()!=NULL)
      {
         temp->readn()->setp(pcurrent->readp());
      }
      temp->readp()->setn(pcurrent->readn());//先连
      pcurrent=temp->readp();
      delete temp;//后断
      return true;
   }
   else
   {
      return false;
   }
}
void Linklist::Show()
{
   Node* ptemp=&head;
   while(ptemp!=NULL)//链表的遍历
   {
      cout<<ptemp->readi()<<'\t'<<ptemp->readc()<<endl;
      ptemp=ptemp->readn();
   }
}
void Linklist::Destroy()
{
   Node* ptemp1=head.readn();
   while(ptemp1!=NULL)
   {
      Node* ptemp2=ptemp1->readn();
      delete ptemp1;
      ptemp1=ptemp2;
   }
   head.setn(NULL);//头结点之后没有其他结点
}
Linklist::Linklist(Linklist &l):head(l.head)
{
   cout<<"Linklist Deep cloner running..."<<endl;
   pcurrent=&head;
   Node* ptemp1=l.head.readn();//该指针用于指向原链表中被复制的结点
   while(ptemp1!=NULL)
   {
      Node* ptemp2=new Node(ptemp1->readi(),ptemp1->readc(),pcurrent,NULL);//新建结点,并复制idata和cdata,思考为何这里不能直接用Node的拷贝构造函数?
      pcurrent->setn(ptemp2);
      pcurrent=pcurrent->readn();//指向表尾结点
      ptemp1=ptemp1->readn();//指向下一个被复制结点
   }
}
Linklist::~Linklist()
{
   cout<<"Linklist destructor is running..."<<endl;
   Destroy();//一个成员函数调用另一个成员函数不需要带上对象名
}
//main.cpp
#include "Linklist.h"
#include <iostream>

using namespace std;
int main()
{
   int tempi;
   char tempc;
   cout<<"请输入一个整数和一个字符:"<<endl;
   cin>>tempi>>tempc;
   Linklist a(tempi,tempc);
   a.Locate(tempi);
   a.Insert(1,'C');
   a.Insert(2,'B'); 
   a.Insert(3,'F');
   cout<<"After Insert"<<endl;
   a.Show();
   a.Locate('B');
   a.Delete();
   cout<<"After Delete"<<endl;
   a.Show();
   Linklist b(a);//创建一个链表b,并且将链表a复制到链表b
   cout<<"This is Linklist b"<<endl;
   b.Show();
   a.Destroy();
   cout<<"After Destroy"<<endl;
   a.Show();
   cout<<"This is Linklist b"<<endl;
   b.Show();//链表a被Destroy之后察看链表b的内容
   return 0;
}

运行结果:

请输入一个整数和一个字符:

4 G

Node constructor is running...

Linklist constructor is running...

Node constructor is running...

Node constructor is running...

Node constructor is running...

After Insert

4 G

3 F

2 B

1 C

Node destructor is running...

After Delete

4 G

3 F

1 C

Linklist Deep cloner running...

Node constructor is running...

Node constructor is running...

This is Linklist b

4 G

3 F

1 C

Node destructor is running...

Node destructor is running...

After Destroy

4 G

This is Linklist b

4 G

3 F

1 C

Linklist destructor is running...

Node destructor is running...

Node destructor is running...

Node destructor is running...

Linklist destructor is running...

Node destructor is running...

        在After Destroy之前的两条Node destructor运行是因为调用了a.Destroy(),最后的6条destructor是因为程序运行结束使得对象自动消亡。可见析构函数是在使用delete语句删除动态生成的对象或程序结束对象消亡时自动被调用的。

        从最后的2条destructor输出我们发现,当一个对象的成员数据还是对象时,析构函数的运行顺序恰好与构造函数的运行顺序相反:一个大对象先调用析构函数,瓦解成若干成员数据,然后各个成员数据再调用各自的析构函数。这体现出构造函数与析构函数的对称性。


链表类:

//node.h同程序
//linklist.h
#include "node.h"//需要使用链表结点类
#include <iostream>
using namespace std;
class Linklist
{
   public:
   Linklist(int i,char c);//链表类构造函数
   bool Locate(int i);//根据整数查找结点
   bool Locate(char c);//根据字符查找结点
   bool Insert(int i=0,char c='0');//在当前结点之后插入结点
   bool Delete();//删除当前结点
   void Show();//显示链表所有数据
   void Destroy();//清除整个链表
   private:
   Node head;//头结点
   Node * pcurrent;//当前结点指针
};
Linklist::Linklist(int i,char c):head(i,c)//类名::构造函数名(参数表):成员对象名1(参数表),链表类构造函数,调用head对象的构造函数重载1,详见Node.h文件
{
   cout<<"Linklist constructor is running..."<<endl;
   pcurrent=&head;
}
bool Linklist::Locate(int i)
{
   Node * ptemp=&head;
   while(ptemp!=NULL)
   {
      if(ptemp->readi()==i)
      {
         pcurrent=ptemp;//将当前结点指针指向找到的结点
         return true;
      }
      ptemp=ptemp->readn();//查找下一个结点
   }
   return false;
}
bool Linklist::Locate(char c)
{
   Node * ptemp=&head;
   while(ptemp!=NULL)
   {
      if(ptemp->readc()==c)
      {
         pcurrent=ptemp;
         return true;
      }
      ptemp=ptemp->readn();
   }
   return false;
}
bool Linklist::Insert(int i,char c)
{
   if(pcurrent!=NULL)
   {
      Node * temp=new Node(i,c,pcurrent,pcurrent->readn());//调用Node类构造函数重载2
      if (pcurrent->readn()!=NULL)
      {
         pcurrent->readn()->setp(temp);
      }
      pcurrent->setn(temp);
      return true;
   }
   else
   {
      return false;
    }
}
bool Linklist::Delete()
{
   if(pcurrent!=NULL && pcurrent!=&head)//head结点不能删除
   {
      Node * temp=pcurrent;
      if (temp->readn()!=NULL)
      {
         temp->readn()->setp(pcurrent->readp());
      }
      temp->readp()->setn(pcurrent->readn());//先连
      pcurrent=temp->readp();
      delete temp;//后断
      return true;
   }
   else
   {
      return false;
   }
}
void Linklist::Show()
{
   Node * ptemp=&head;
   while (ptemp!=NULL)//链表的遍历
   {
      cout <<ptemp->readi() <<'\t' <<ptemp->readc() <<endl;
      ptemp=ptemp->readn();
   }
}
void Linklist::Destroy()
{
   Node * ptemp1=head.readn();
   while (ptemp1!=NULL)
   {
      Node * ptemp2=ptemp1->readn();
      delete ptemp1;
      ptemp1=ptemp2;
   }
   head.setn(NULL);//头结点之后没有其他结点
}
//main.cpp
#include "Linklist.h"
#include <iostream>
using namespace std;
int main()
{
   int tempi;
   char tempc;
   cout <<"请输入一个整数和一个字符:" <<endl;
   cin >>tempi >>tempc;
   Linklist a(tempi,tempc);//创建一个链表,头结点数据由tempi和tempc确定
   a.Locate(tempi);
   a.Insert(1,'C'); 
   a.Insert(2,'B');
   a.Insert(3,'F');
   cout <<"After Insert" <<endl;
   a.Show();
   a.Locate('B');
   a.Delete();
   cout <<"After Delete" <<endl;
   a.Show();
   a.Destroy();
   cout <<"After Destroy" <<endl;
   a.Show();
   return 0;
}

运行结果:

请输入一个整数和一个字符:

4 G

Node constructor is running...

Linklist constructor is running...

Node constructor is running...

Node constructor is running...

Node constructor is running...

After Insert

4 G

3 F

2 B

1 C

After Delete

4 G

3 F

1 C

After Destroy

4 G

        根据程序的运行结果,可以发现头结点的构造函数比链表的构造函数优先运行。这也不难理解:构造函数的目的是要初始化成员数据,初始化成员数据的时候这个成员数据是必须存在的。所以当一个成员数据是一个对象的时候,应当先产生这个成员对象,于是就先调用了成员对象的构造函数。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值