问题
填空题
类time可以输出12小时或24小时制的时间,现需要设计一个缺省构造函数,缺省值为0时0分0秒。该缺省构造函数为 【14】 。
# include<iostream. h>
class Time
int h.m,s;
public:
Time(int hour=0 .int min=0 ,int sec=0) settime(hour,min,sec);
void settime(int hour,int min,int sec) h=hour; m=min s=sec;
void show24()
cout<<(h<1O"0":"")<<h<<":"<<(m<10"0":"")<<
m<<":"MM(s<10"0":"")<<s<<end1
void show12( )
int temp=(h==‖h==12) 12:h%12;
cout<<(temp<10"0":"")<<temp<<":"<<(m<10"0":"")
<<m<<":"<<(s<10 "0":"")<<s<<((temp>0 && h<12)"
AM":"PM")<<end1
;
void main()
Time t(15,43,21)
t. show12():
t. show24()
答案
参考答案:Time(){h=0;m=0;s=0;)
解析: 注意缺省构造函数为无参构造函数,且必须为私有数据成员赋初值。