任务:类和对象
析构函数
~类名(){
函数体
}
类体内析构
~类名();
类体外定义析构
类名::~类名(){
函数体
}
#include<iostream.h>
#include<string.h>
class G{
char *s;
public:
G(char *p)
{
int n=strlen(p);
s=new char[n+1];
strcpy(s,p);
cout<<"调用了构造函数\n";
}
~G()
{
cout<<"调用了析构函数\n";
delete []s;
}
void print(){cout<<s<<endl;}
};
void main()
{
G g1("C++ Program.");
g1.print();
}