C++类成员初始化方法

1.Initializer(初始化器)

class Time

{

  public:

    Time();

  private:

    int hour;

    int minute;

 

};

//调用构造函数时,会为hour,minute赋初始值0

Time::Time():hour(0),minute(0)

{

}

 

2.在构造函数里进行赋值

class Time

{

  public:

    Time(int h,int m)

              {

      hour = h;

      minute = m;

              }

  private:

    int hour;

    int minute;

 

};

 

posted @ 2020-09-13 22:49  inss!w!  阅读(834)  评论(0编辑  收藏  举报