继承下的构造函数

来源:互联网 发布:剑灵捏脸数据图怎么用 编辑:程序博客网 时间:2024/05/29 17:10
首先调用的是对象基本部分的构造函数,然后调用派生类的适当构造函数。
 
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){}; //单参数构造函数
}; 
原创粉丝点击