自考新教材-p212

源程序:

#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()

 

posted @ 2020-02-04 17:29  bobo哥  阅读(137)  评论(0编辑  收藏  举报