首先调用的是对象基本部分的构造函数,然后调用派生类的适当构造函数。
class Base
{
protected:
int a;
public:
Base() //默认构造函数
{
a = 0;
}
Base(int c) //单参数构造函数
{
a = c;
}
};
class Derived:public Base
{
public:
Derived():Base(){}; //先用Base构造对象,再用Derived():Base()里的语句默认构造函数
Derived(int c):Base(c){}; //单参数构造函数
};
class Base
{
protected:
int a;
public:
Base() //默认构造函数
{
a = 0;
}
Base(int c) //单参数构造函数
{
a = c;
}
};
class Derived:public Base
{
public:
Derived():Base(){}; //先用Base构造对象,再用Derived():Base()里的语句默认构造函数
Derived(int c):Base(c){}; //单参数构造函数
};