#includeusing namespace std; class Date { public: Date(); Date(int,int,int); ~Date(); void set(int y, int m, int d);//调整日期 void display();//显示日期 private: int year_; int month_; int day_; }; Date::Date(int year , int month , int day ) { year_ = year; month_ = month; day_ = day; cout << "构造函数初始化成功n"; } Date::Date() { year_ = 2022; month_ = 2; day_ = 2; cout << "默认构造函数初始化n"; } void Date::set(int y, int m, int d) { year_ = y; day_ = d; month_ = m; } Date::~Date() { cout << "nBye ! "; } void Date::display() { cout << year_ << "年" << month_ << "月" << day_ << "日" << endl; } class Time { public: Time(); Time(int h,int m,int s); void increment();//时间增加一秒 void display();//显示时间值,显示格式为“2022 年 2 月 22 日 2 点 2 分 2 秒" bool equal(Time& other_time);//比较是否与某个时间相等 bool less_than(Time& other_time);//比较是否早于某个时间 ~Time(); private: Date date0; int hour0; int minute0; int second0; }; Time::Time() { hour0 = 2; second0 = 2; minute0 = 2; //Date(); cout << "默认构造函数初始化n"; } Time::~Time() { cout << "nend of time !"; } Time::Time(int h, int m, int s) { hour0 = h; minute0 = m; second0 = s; cout << "构造函数初始化n"; } void Time::increment() { second0++; cout << "时间增加一秒n"; } void Time::display() { date0.display(); cout << hour0 << "点" << minute0 << "分" << second0 << "秒" << endl; } bool Time::equal(Time &other_time) { cout << "我不想写了n"; return 1; } bool Time::less_than(Time& other_time) { cout << "不写了n"; return 1; } int main() { Date date0;//初始化列表默认构造函数 date0.display(); Date date1(2021, 10, 7);//调用构造函数 date1.display(); date1.set(2022, 7, 18);//set函数 date1.display(); Time time; time.display(); time.increment(); time.display(); Time time2(12,5,2); time2.display(); return 0; }
默认构造函数一般不接受参数,如果有参数必须提供默认值
若默认构造函数含参且与构造函数含参相同时可能会报错
题目中要求列表化构造意义不明,若列表化默认构造函数会造成与构造函数参数相同的后果



