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)
{}
上面两段代码先后顺序颠倒了............................................................................................
————郭仔