源程序:
#include<iostream>
using namespace std;
class Base
{
private:
int Y;
public:
Base(int y=0)
{
Y=y;
cout<<"Base("<<y<<")"<<endl;
}
~Base()
{
cout<<"~Base()"<<endl;
}
void print()
{
cout<<Y<<" ";
}
};
class Derived:public Base
{
private:
int Z;
public:
Derived(int y,int z):Base(y)
{
Z=z;
cout<<"Derived("<<y<<","<<z<<")"<<endl;
}
~Derived()
{
cout<<"~Derived()"<<endl;
}
void print()
{
Base::print();
cout<<Z<<endl;
}
};
int main()
{
Derived d(10,20);
d.print();
return 0;
}
运行结果:
析构过程没有显示出来,最后应该显示:
~Derived()
~Base()
原文:https://www.cnblogs.com/duanqibo/p/12260196.html