class Fish:public Animal
{
public:
Fish()
{
cout<<"this is Fish"<<endl;
}
~Fish()
{
cout<<"this is free Fish"<<endl;
}
};
C++继承中父类的构造函数先于子类的构造函数调用;
子类的析构函数的调用顺序先于父类的析构函数调用;
class Animal
{
public:
Animal()
{
cout<<"this is animal"<<endl;
}
~Animal()
{
cout<<"this is free animal"<<endl;
}
};
void main()
{
Fish fish;
}
如果父类中的构造函数含有两个参数,则子类中声明构造函数需要注意
如:
Animal中的构造函数是
Animal(int i,int j)
{}
则子类的构造函数:
Fish():Animal(100,200)
{}
上面两段代码先后顺序颠倒了............................................................................................
————郭仔
本文通过一个具体的C++示例介绍了类继承中的构造函数和析构函数的调用顺序,并展示了如何在子类构造函数中调用带有参数的父类构造函数。
535

被折叠的 条评论
为什么被折叠?



